Skip to content

Commit

Permalink
refactor(yarn-install): convert to async/await and support proxy (#1057)
Browse files Browse the repository at this point in the history
  • Loading branch information
acburdine authored Nov 8, 2019
1 parent b12b4c7 commit 61599f8
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions lib/tasks/yarn-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,56 @@ const cliPackage = require('../../package.json');
const packageInfo = require('package-json');
const {prerelease, satisfies} = require('semver');

const errors = require('../errors');
const {CliError, SystemError} = require('../errors');
const yarn = require('../utils/yarn');
const getProxyAgent = require('../utils/get-proxy-agent');

const subTasks = {
dist: ctx => packageInfo('ghost', {
version: ctx.version,
agent: getProxyAgent()
}).then(({dist, engines = {}}) => {
async dist(ctx) {
const {dist, engines = {}} = await packageInfo('ghost', {
version: ctx.version,
agent: getProxyAgent()
});

const skipNodeVersionCheck = (process.env.GHOST_NODE_VERSION_CHECK === 'false');
const isPrerelease = Boolean(prerelease(cliPackage.version));

if (!skipNodeVersionCheck && engines.node && !satisfies(process.versions.node, engines.node)) {
return Promise.reject(new errors.SystemError(`Ghost v${ctx.version} is not compatible with the current Node version.`));
throw new SystemError(`Ghost v${ctx.version} is not compatible with the current Node version.`);
}

if (engines.cli && !isPrerelease && !satisfies(cliPackage.version, engines.cli)) {
return Promise.reject(new errors.SystemError(`Ghost v${ctx.version} is not compatible with this version of the CLI.`));
throw new SystemError(`Ghost v${ctx.version} is not compatible with this version of the CLI.`);
}

ctx.shasum = dist.shasum;
ctx.tarball = dist.tarball;
}),
download: ctx => download(ctx.tarball).then((data) => {
ctx.shasum = dist.shasum; // eslint-disable-line require-atomic-updates
ctx.tarball = dist.tarball; // eslint-disable-line require-atomic-updates
},

async download(ctx) {
const data = await download(ctx.tarball, {agent: getProxyAgent()});

if (shasum(data) !== ctx.shasum) {
// shasums don't match - this is not good
return Promise.reject(new errors.CliError('Ghost download integrity compromised.' +
'Cancelling install because of potential security issues'));
throw new CliError('Ghost download integrity compromised.' +
'Cancelling install because of potential security issues');
}

fs.ensureDirSync(ctx.installPath);
return decompress(data, ctx.installPath, {
map: (file) => {
file.path = file.path.replace('package/', '');
return file;
}
}).catch((error) => {

try {
await decompress(data, ctx.installPath, {
map: (file) => {
file.path = file.path.replace('package/', '');
return file;
}
});
} catch (error) {
// Clean up the install folder since the decompress failed
fs.removeSync(ctx.installPath);
return Promise.reject(error);
});
})
throw error;
}
}
};

module.exports = function yarnInstall(ui, zipFile) {
Expand Down

0 comments on commit 61599f8

Please sign in to comment.