Skip to content

Commit

Permalink
Merge pull request #9148 from ember-cli/async-await-npm-task
Browse files Browse the repository at this point in the history
Convert npm-task task to async/await syntax
  • Loading branch information
locks committed Apr 9, 2020
2 parents c412bd6 + 0b10f91 commit 1ab33a6
Showing 1 changed file with 81 additions and 84 deletions.
165 changes: 81 additions & 84 deletions lib/tasks/npm-task.js
Expand Up @@ -42,56 +42,54 @@ class NpmTask extends Task {
return isYarnProject(this.project.root);
}

checkYarn() {
return this.yarn(['--version'])
.then((result) => {
let version = result.stdout;
logger.info('yarn --version: %s', version);
})
.catch((error) => {
logger.error('yarn --version failed: %s', error);
throw error;
});
async checkYarn() {
try {
let result = await this.yarn(['--version']);
let version = result.stdout;
logger.info('yarn --version: %s', version);
} catch (error) {
logger.error('yarn --version failed: %s', error);
throw error;
}
}

checkNpmVersion() {
return this.npm(['--version'])
.then((result) => {
let version = result.stdout;
logger.info('npm --version: %s', version);

let ok = semver.satisfies(version, this.versionConstraints);
if (!ok) {
logger.warn('npm --version is outside of version constraint: %s', this.versionConstraints);

let below = semver.ltr(version, this.versionConstraints);
if (below) {
throw new SilentError(
'Ember CLI is now using the global npm, but your npm version is outdated.\n' +
'Please update your global npm version by running: npm install -g npm'
);
}

this.ui.writeWarnLine(
'Ember CLI is using the global npm, but your npm version has not yet been ' +
'verified to work with the current Ember CLI release.'
);
}
async checkNpmVersion() {
try {
let result = await this.npm(['--version']);
let version = result.stdout;
logger.info('npm --version: %s', version);

return { npmVersion: version };
})
.catch((error) => {
logger.error('npm --version failed: %s', error);
let ok = semver.satisfies(version, this.versionConstraints);
if (!ok) {
logger.warn('npm --version is outside of version constraint: %s', this.versionConstraints);

if (error.code === 'ENOENT') {
let below = semver.ltr(version, this.versionConstraints);
if (below) {
throw new SilentError(
'Ember CLI is now using the global npm, but was not able to find it.\n' +
'Please install npm using the instructions at https://github.com/npm/npm'
'Ember CLI is now using the global npm, but your npm version is outdated.\n' +
'Please update your global npm version by running: npm install -g npm'
);
}

throw error;
});
this.ui.writeWarnLine(
'Ember CLI is using the global npm, but your npm version has not yet been ' +
'verified to work with the current Ember CLI release.'
);
}

return { npmVersion: version };
} catch (error) {
logger.error('npm --version failed: %s', error);

if (error.code === 'ENOENT') {
throw new SilentError(
'Ember CLI is now using the global npm, but was not able to find it.\n' +
'Please install npm using the instructions at https://github.com/npm/npm'
);
}

throw error;
}
}

/**
Expand All @@ -108,7 +106,7 @@ class NpmTask extends Task {
* @method findPackageManager
* @return {Promise}
*/
findPackageManager() {
async findPackageManager() {
if (this.useYarn === true) {
logger.info('yarn requested -> trying yarn');

Expand All @@ -132,56 +130,55 @@ class NpmTask extends Task {
}

logger.info('yarn.lock found -> trying yarn');
return this.checkYarn()
.then(() => {
logger.info('yarn found -> using yarn');
this.useYarn = true;
})
.catch(() => {
logger.info('yarn not found -> using npm');
return this.checkNpmVersion();
});
try {
await this.checkYarn();

logger.info('yarn found -> using yarn');
this.useYarn = true;
} catch (_err) {
logger.info('yarn not found -> using npm');
return this.checkNpmVersion();
}
}

run(options) {
async run(options) {
this.useYarn = options.useYarn;

return this.findPackageManager().then((result) => {
let ui = this.ui;
let startMessage = this.formatStartMessage(options.packages);
let completeMessage = this.formatCompleteMessage(options.packages);
let result = await this.findPackageManager();
let ui = this.ui;
let startMessage = this.formatStartMessage(options.packages);
let completeMessage = this.formatCompleteMessage(options.packages);

const prependEmoji = require('../../lib/utilities/prepend-emoji');
const prependEmoji = require('../../lib/utilities/prepend-emoji');

ui.writeLine('');
ui.writeLine(prependEmoji('🚧', 'Installing packages... This might take a couple of minutes.'));
ui.startProgress(chalk.green(startMessage));
ui.writeLine('');
ui.writeLine(prependEmoji('🚧', 'Installing packages... This might take a couple of minutes.'));
ui.startProgress(chalk.green(startMessage));

let promise;
if (this.useYarn) {
let args = this.toYarnArgs(this.command, options);
promise = this.yarn(args);
} else {
let args = this.toNpmArgs(this.command, options);
promise = this.npm(args);

// as of 2018-10-09 npm 5 and 6 _break_ the hierarchy of `node_modules`
// after a `npm install foo` (deletes files/folders other than
// what was directly installed) in some circumstances, see:
//
// * https://github.com/npm/npm/issues/16853
// * https://github.com/npm/npm/issues/17379
//
// this ensures that we run a full `npm install` **after** any `npm
// install foo` runs to ensure that we have a fully functional
// node_modules hierarchy
if (result.npmVersion && semver.lt(result.npmVersion, '5.7.1')) {
promise = promise.then(() => this.npm(['install']));
}
let promise;
if (this.useYarn) {
let args = this.toYarnArgs(this.command, options);
promise = this.yarn(args);
} else {
let args = this.toNpmArgs(this.command, options);
promise = this.npm(args);

// as of 2018-10-09 npm 5 and 6 _break_ the hierarchy of `node_modules`
// after a `npm install foo` (deletes files/folders other than
// what was directly installed) in some circumstances, see:
//
// * https://github.com/npm/npm/issues/16853
// * https://github.com/npm/npm/issues/17379
//
// this ensures that we run a full `npm install` **after** any `npm
// install foo` runs to ensure that we have a fully functional
// node_modules hierarchy
if (result.npmVersion && semver.lt(result.npmVersion, '5.7.1')) {
promise = promise.then(() => this.npm(['install']));
}
}

return promise.finally(() => ui.stopProgress()).then(() => ui.writeLine(chalk.green(completeMessage)));
});
return promise.finally(() => ui.stopProgress()).then(() => ui.writeLine(chalk.green(completeMessage)));
}

toNpmArgs(command, options) {
Expand Down

0 comments on commit 1ab33a6

Please sign in to comment.