Skip to content

Commit 8becd56

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

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

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

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,60 @@ 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+
console.log(item, itemIndex);
8+
if (itemIndex !== -1) {
9+
array.splice(itemIndex, 1);
10+
}
11+
}
512

613
export default Task.extend({
714
run: function() {
815
const ui = this.ui;
16+
let PATH: string;
917

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

0 commit comments

Comments
 (0)