Skip to content

Commit 75b61e7

Browse files
committed
feat(tracking): Check for Third party NPM replacements and make use of those if set by default
1 parent 39fa206 commit 75b61e7

File tree

5 files changed

+77
-4
lines changed

5 files changed

+77
-4
lines changed

packages/@angular/cli/commands/init.run.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import * as chalk from 'chalk';
2+
import {checkYarnOrCNPM} from '../utilities/check-package-manager';
3+
import {CliConfig} from '../models/config';
24
import LinkCli from '../tasks/link-cli';
35
import NpmInstall from '../tasks/npm-install';
46
import { validateProjectName } from '../utilities/validate-project-name';
57

8+
69
const Promise = require('../ember-cli/lib/ext/promise');
710
const SilentError = require('silent-error');
811
const normalizeBlueprint = require('../ember-cli/lib/utilities/normalize-blueprint-option');
912
const GitInit = require('../tasks/git-init');
13+
const config = CliConfig.fromProject();
1014

1115

1216
export default function initRun(commandOptions: any, rawArgs: string[]) {
@@ -31,9 +35,14 @@ export default function initRun(commandOptions: any, rawArgs: string[]) {
3135

3236
let npmInstall: any;
3337
if (!commandOptions.skipNpm) {
38+
let packageManager = 'npm';
39+
if (config && config.get('packageManager')) {
40+
packageManager = config.get('packageManager');
41+
}
3442
npmInstall = new NpmInstall({
3543
ui: this.ui,
36-
project: this.project
44+
project: this.project,
45+
packageManager
3746
});
3847
}
3948

@@ -93,6 +102,7 @@ export default function initRun(commandOptions: any, rawArgs: string[]) {
93102
return linkCli.run();
94103
}
95104
})
105+
.then(checkYarnOrCNPM)
96106
.then(() => {
97107
this.ui.writeLine(chalk.green(`Project '${packageName}' successfully created.`));
98108
});

packages/@angular/cli/lib/config/schema.json

+4
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@
292292
},
293293
"additionalProperties": false
294294
},
295+
"packageManager": {
296+
"enum": [ "npm", "cnpm", "yarn", "default" ],
297+
"default": "npm"
298+
},
295299
"warnings": {
296300
"description": "Allow people to disable console warnings.",
297301
"type": "object",

packages/@angular/cli/tasks/npm-install.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ import {exec} from 'child_process';
66
export default Task.extend({
77
run: function() {
88
const ui = this.ui;
9+
const packageManager = this.packageManager;
910

1011
return new Promise(function(resolve, reject) {
11-
ui.writeLine(chalk.green('Installing packages for tooling via npm.'));
12-
exec('npm install',
12+
ui.writeLine(chalk.green(`Installing packages for tooling via ${packageManager}.`));
13+
exec(`${packageManager} install`,
1314
(err: NodeJS.ErrnoException, stdout: string, stderr: string) => {
1415
if (err) {
1516
ui.writeLine(stderr);
1617
ui.writeLine(chalk.red('Package install failed, see above.'));
1718
reject();
1819
} else {
19-
ui.writeLine(chalk.green('Installed packages for tooling via npm.'));
20+
ui.writeLine(chalk.green(`Installed packages for tooling via ${packageManager}.`));
2021
resolve();
2122
}
2223
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as chalk from 'chalk';
2+
import {exec} from 'child_process';
3+
4+
import {CliConfig} from '../models/config';
5+
6+
const Promise = require('../ember-cli/lib/ext/promise');
7+
const config = CliConfig.fromProject();
8+
9+
const execPromise = Promise.denodeify(exec);
10+
11+
let packageManager = 'npm';
12+
if (config && config.get('packageManager')) {
13+
packageManager = config.get('packageManager');
14+
}
15+
16+
export function checkYarnOrCNPM() {
17+
return Promise
18+
.all([checkYarn(), checkCNPM()])
19+
.then((data: Array<boolean>) => {
20+
if (packageManager === 'npm') {
21+
const [isYarnInstalled, isCNPMInstalled] = data;
22+
if (isYarnInstalled && isCNPMInstalled) {
23+
console.log(chalk.yellow('You can `ng set --global packageManager=yarn` ' +
24+
'or `ng set --global packageManager=cnpm`.'));
25+
} else if (isYarnInstalled) {
26+
console.log(chalk.yellow('You can `ng set --global packageManager=yarn`.'));
27+
} else if (isCNPMInstalled) {
28+
console.log(chalk.yellow('You can `ng set --global packageManager=cnpm`.'));
29+
}
30+
}
31+
});
32+
}
33+
34+
function checkYarn() {
35+
return execPromise('yarn --version')
36+
.then(() => true, () => false);
37+
}
38+
39+
function checkCNPM() {
40+
return execPromise('cnpm --version')
41+
.then(() => true, () => false);
42+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {ng} from '../../../utils/process';
2+
import {getGlobalVariable} from '../../../utils/env';
3+
4+
const yarnRegEx = /You can `ng set --global packageManager=yarn`./;
5+
6+
export default function() {
7+
return Promise.resolve()
8+
.then(() => process.chdir(getGlobalVariable('tmp-root')))
9+
.then(() => ng('new', 'foo'))
10+
.then((stdout) => {
11+
// assuming yarn is installed and checking for message with yarn
12+
if (!stdout.toString().match(yarnRegEx)) {
13+
throw new Error('Should display message to use yarn packageManager');
14+
}
15+
});
16+
}

0 commit comments

Comments
 (0)