Skip to content

Commit 67619c9

Browse files
committed
fixed npm install handle peer dependencies case
1 parent 59a27c5 commit 67619c9

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

lib/node-package-manager.ts

+22-11
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,15 @@ export class NodePackageManager implements INodePackageManager {
2525
try {
2626
let subDirsBefore:Array<string> = this.$fs.exists(path.join(pathToSave, "node_modules")).wait() ? this.$fs.readDirectory(path.join(pathToSave, "node_modules")).wait() : [];
2727

28-
let flags = this.getFlagsString(config);
29-
this.$childProcess.exec(`npm install ${packageName} ${flags}`, { cwd: pathToSave }).wait();
28+
let flags = this.getFlagsString(config, true);
29+
let params = ["install", packageName];
30+
for(let index in flags) {
31+
params.push(flags[index]);
32+
}
33+
let res = this.$childProcess.spawnFromEvent("npm", params, "close", {}, { throwError: false }).wait();
34+
if(res.stderr) {
35+
throw {stderr: res.stderr};
36+
}
3037

3138
let subDirsAfter = this.$fs.readDirectory(path.join(pathToSave, "node_modules")).wait();
3239

@@ -43,7 +50,7 @@ export class NodePackageManager implements INodePackageManager {
4350

4451
return diff;
4552
} catch (err) {
46-
if (err.code === "EPEERINVALID" || err.code === 1) {
53+
if (err.stderr && err.stderr.indexOf("EPEERINVALID") !== -1) {
4754
// Not installed peer dependencies are treated by npm 2 as errors, but npm 3 treats them as warnings.
4855
// We'll show them as warnings and let the user install them in case they are needed.
4956
// The strucutre of the error object in such case is:
@@ -63,7 +70,7 @@ export class NodePackageManager implements INodePackageManager {
6370
// '@ngrx/[email protected]': '^2.0.0',
6471
// '@ngrx/[email protected]': '^2.0.0',
6572
// '[email protected]': '~2.0.0' } }
66-
this.$logger.warn(err.message);
73+
this.$logger.warn(err.stderr);
6774
} else {
6875
// All other errors should be handled by the caller code.
6976
throw err;
@@ -72,7 +79,7 @@ export class NodePackageManager implements INodePackageManager {
7279
}
7380

7481
public uninstall(packageName: string, config?: any, path?: string): IFuture<any> {
75-
let flags = this.getFlagsString(config);
82+
let flags = this.getFlagsString(config, false);
7683
return this.$childProcess.exec(`npm uninstall ${packageName} ${flags}`, { cwd: path });
7784
}
7885

@@ -82,23 +89,27 @@ export class NodePackageManager implements INodePackageManager {
8289
}
8390

8491
public view(packageName: string, config: any): IFuture<any> {
85-
let flags = this.getFlagsString(config);
92+
let flags = this.getFlagsString(config, false);
8693
let viewResult = this.$childProcess.exec(`npm view ${packageName} ${flags}`).wait();
8794
return JSON.parse(viewResult);
8895
}
8996

90-
private getFlagsString(config: any) : string{
91-
let flagsString = "";
97+
private getFlagsString(config: any, asArray: boolean) : any{
98+
let array:Array<string> = [];
9299
for(let flag in config) {
93100
if(config[flag]) {
94101
if(flag==="dist-tags" || flag==="versions") {
95-
flagsString += ` ${flag}`;
102+
array.push(` ${flag}`)
96103
continue;
97104
}
98-
flagsString += ` --${flag}`;
105+
array.push(`--${flag}`)
99106
}
100107
}
101-
return flagsString;
108+
if(asArray) {
109+
return array;
110+
}
111+
112+
return array.join(" ");
102113
}
103114
}
104115
$injector.register("npm", NodePackageManager);

0 commit comments

Comments
 (0)