Skip to content

Commit c979ecb

Browse files
committed
fix: exclude local node_modules from PATH when launch 'npm install'
1 parent 93da512 commit c979ecb

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

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

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,59 @@ const Task = require('ember-cli/lib/models/task');
22
import * as chalk from 'chalk';
33
import {exec} from 'child_process';
44

5+
function removeFromArray(array: any[], item: any) {
6+
const itemIndex = array.indexOf(item);
7+
if (itemIndex !== -1) {
8+
array.splice(itemIndex, 1);
9+
}
10+
}
511

612
export default Task.extend({
713
run: function() {
814
const ui = this.ui;
15+
let PATH: string;
916

1017
return new Promise(function(resolve, reject) {
1118
ui.writeLine(chalk.green('Installing packages for tooling via npm.'));
12-
exec('npm install',
13-
(err: NodeJS.ErrnoException, stdout: string, stderr: string) => {
14-
if (err) {
15-
ui.writeLine(stderr);
16-
ui.writeLine(chalk.red('Package install failed, see above.'));
17-
reject();
18-
} else {
19-
ui.writeLine(chalk.green('Installed packages for tooling via npm.'));
20-
resolve();
21-
}
19+
// remove from PATH:
20+
// - absolute path to projet's node_modules
21+
// - relative path to projet's node_modules (linux and windows)
22+
exec('npm bin', (err: NodeJS.ErrnoException, stdout: string, stderr: string) => {
23+
if (err) {
24+
ui.writeLine(stderr);
25+
ui.writeLine(chalk.red('Package install failed, see above.'));
26+
reject();
27+
} else {
28+
const absoluteProjectNodeModulesPath = stdout.replace(/(\r|\n)/, '');
29+
const pathArr = process.env.PATH.split(':');
30+
removeFromArray(pathArr, absoluteProjectNodeModulesPath);
31+
removeFromArray(pathArr, 'node_modules/.bin');
32+
removeFromArray(pathArr, 'node_modules\.bin');
33+
PATH = pathArr.join(':');
34+
resolve();
35+
}
36+
});
37+
})
38+
.then(() => {
39+
return new Promise(function(resolve, reject) {
40+
const options = {
41+
env: {
42+
PATH: PATH
43+
}
44+
};
45+
exec(
46+
'npm install',
47+
options,
48+
(err: NodeJS.ErrnoException, stdout: string, stderr: string) => {
49+
if (err) {
50+
ui.writeLine(stderr);
51+
ui.writeLine(chalk.red('Package install failed, see above.'));
52+
reject();
53+
} else {
54+
ui.writeLine(chalk.green('Installed packages for tooling via npm.'));
55+
resolve();
56+
}
57+
});
2258
});
2359
});
2460
}

0 commit comments

Comments
 (0)