diff --git a/packages/@angular/cli/commands/init.run.ts b/packages/@angular/cli/commands/init.run.ts index c243ab4b76c4..ddacee8d19a8 100644 --- a/packages/@angular/cli/commands/init.run.ts +++ b/packages/@angular/cli/commands/init.run.ts @@ -1,12 +1,16 @@ import * as chalk from 'chalk'; +import {checkYarnOrCNPM} from '../utilities/check-package-manager'; +import {CliConfig} from '../models/config'; import LinkCli from '../tasks/link-cli'; import NpmInstall from '../tasks/npm-install'; import { validateProjectName } from '../utilities/validate-project-name'; + const Promise = require('../ember-cli/lib/ext/promise'); const SilentError = require('silent-error'); const normalizeBlueprint = require('../ember-cli/lib/utilities/normalize-blueprint-option'); const GitInit = require('../tasks/git-init'); +const config = CliConfig.fromProject(); export default function initRun(commandOptions: any, rawArgs: string[]) { @@ -31,9 +35,14 @@ export default function initRun(commandOptions: any, rawArgs: string[]) { let npmInstall: any; if (!commandOptions.skipNpm) { + let packageManager = 'npm'; + if (config && config.get('packageManager')) { + packageManager = config.get('packageManager'); + } npmInstall = new NpmInstall({ ui: this.ui, - project: this.project + project: this.project, + packageManager }); } @@ -93,6 +102,7 @@ export default function initRun(commandOptions: any, rawArgs: string[]) { return linkCli.run(); } }) + .then(checkYarnOrCNPM) .then(() => { this.ui.writeLine(chalk.green(`Project '${packageName}' successfully created.`)); }); diff --git a/packages/@angular/cli/lib/config/schema.json b/packages/@angular/cli/lib/config/schema.json index 9c1d753aa09b..2877901d7511 100644 --- a/packages/@angular/cli/lib/config/schema.json +++ b/packages/@angular/cli/lib/config/schema.json @@ -292,6 +292,10 @@ }, "additionalProperties": false }, + "packageManager": { + "enum": [ "npm", "cnpm", "yarn", "default" ], + "default": "npm" + }, "warnings": { "description": "Allow people to disable console warnings.", "type": "object", diff --git a/packages/@angular/cli/tasks/npm-install.ts b/packages/@angular/cli/tasks/npm-install.ts index 02e3f5d1cc27..13b8b09c8b38 100644 --- a/packages/@angular/cli/tasks/npm-install.ts +++ b/packages/@angular/cli/tasks/npm-install.ts @@ -6,17 +6,18 @@ import {exec} from 'child_process'; export default Task.extend({ run: function() { const ui = this.ui; + const packageManager = this.packageManager; return new Promise(function(resolve, reject) { - ui.writeLine(chalk.green('Installing packages for tooling via npm.')); - exec('npm install', + ui.writeLine(chalk.green(`Installing packages for tooling via ${packageManager}.`)); + exec(`${packageManager} install`, (err: NodeJS.ErrnoException, stdout: string, stderr: string) => { if (err) { ui.writeLine(stderr); ui.writeLine(chalk.red('Package install failed, see above.')); reject(); } else { - ui.writeLine(chalk.green('Installed packages for tooling via npm.')); + ui.writeLine(chalk.green(`Installed packages for tooling via ${packageManager}.`)); resolve(); } }); diff --git a/packages/@angular/cli/utilities/check-package-manager.ts b/packages/@angular/cli/utilities/check-package-manager.ts new file mode 100644 index 000000000000..d1dcaefd8b58 --- /dev/null +++ b/packages/@angular/cli/utilities/check-package-manager.ts @@ -0,0 +1,42 @@ +import * as chalk from 'chalk'; +import {exec} from 'child_process'; + +import {CliConfig} from '../models/config'; + +const Promise = require('../ember-cli/lib/ext/promise'); +const config = CliConfig.fromProject(); + +const execPromise = Promise.denodeify(exec); + +let packageManager = 'npm'; +if (config && config.get('packageManager')) { + packageManager = config.get('packageManager'); +} + +export function checkYarnOrCNPM() { + return Promise + .all([checkYarn(), checkCNPM()]) + .then((data: Array) => { + if (packageManager === 'npm') { + const [isYarnInstalled, isCNPMInstalled] = data; + if (isYarnInstalled && isCNPMInstalled) { + console.log(chalk.yellow('You can `ng set --global packageManager=yarn` ' + + 'or `ng set --global packageManager=cnpm`.')); + } else if (isYarnInstalled) { + console.log(chalk.yellow('You can `ng set --global packageManager=yarn`.')); + } else if (isCNPMInstalled) { + console.log(chalk.yellow('You can `ng set --global packageManager=cnpm`.')); + } + } + }); +} + +function checkYarn() { + return execPromise('yarn --version') + .then(() => true, () => false); +} + +function checkCNPM() { + return execPromise('cnpm --version') + .then(() => true, () => false); +} diff --git a/tests/e2e/tests/commands/new/check-yarn.ts b/tests/e2e/tests/commands/new/check-yarn.ts new file mode 100644 index 000000000000..7078be1fef4b --- /dev/null +++ b/tests/e2e/tests/commands/new/check-yarn.ts @@ -0,0 +1,16 @@ +import {ng} from '../../../utils/process'; +import {getGlobalVariable} from '../../../utils/env'; + +const yarnRegEx = /You can `ng set --global packageManager=yarn`./; + +export default function() { + return Promise.resolve() + .then(() => process.chdir(getGlobalVariable('tmp-root'))) + .then(() => ng('new', 'foo')) + .then((stdout) => { + // assuming yarn is installed and checking for message with yarn + if (!stdout.toString().match(yarnRegEx)) { + throw new Error('Should display message to use yarn packageManager'); + } + }); +}