Skip to content

Commit

Permalink
feat(tracking): Check for Third party NPM replacements and make use o…
Browse files Browse the repository at this point in the history
…f those if set by default
  • Loading branch information
sumitarora committed Feb 6, 2017
1 parent 73d5628 commit 3d7f5a0
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 4 deletions.
12 changes: 11 additions & 1 deletion packages/@angular/cli/commands/init.run.ts
Original file line number Diff line number Diff line change
@@ -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';


const Promise = require('../ember-cli/lib/ext/promise');
const SilentError = require('silent-error');
const validProjectName = require('../ember-cli/lib/utilities/valid-project-name');
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[]) {
Expand All @@ -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
});
}

Expand Down Expand Up @@ -96,6 +105,7 @@ export default function initRun(commandOptions: any, rawArgs: string[]) {
return linkCli.run();
}
})
.then(checkYarnOrCNPM)
.then(() => {
this.ui.writeLine(chalk.green(`Project '${packageName}' successfully created.`));
});
Expand Down
4 changes: 4 additions & 0 deletions packages/@angular/cli/lib/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@
},
"additionalProperties": false
},
"packageManager": {
"enum": [ "npm", "cnpm", "yarn" ],
"default": "npm"
},
"warnings": {
"description": "Allow people to disable console warnings.",
"type": "object",
Expand Down
7 changes: 4 additions & 3 deletions packages/@angular/cli/tasks/npm-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
Expand Down
42 changes: 42 additions & 0 deletions packages/@angular/cli/utilities/check-package-manager.ts
Original file line number Diff line number Diff line change
@@ -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<boolean>) => {
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);
}
16 changes: 16 additions & 0 deletions tests/e2e/tests/commands/new/check-yarn.ts
Original file line number Diff line number Diff line change
@@ -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');
}
});
}

0 comments on commit 3d7f5a0

Please sign in to comment.