Skip to content

Commit bd2ce9c

Browse files
kyliaualxhub
authored andcommittedApr 15, 2019
fix(bazel): Install packages after ng add when invoked independently (#29852)
PR Closes #29573 PR Close #29852
1 parent 6a8cca7 commit bd2ce9c

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed
 

‎packages/bazel/src/schematics/ng-add/index.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import {JsonAstObject, parseJsonAst} from '@angular-devkit/core';
1212
import {Rule, SchematicContext, SchematicsException, Tree, apply, applyTemplates, chain, mergeWith, url} from '@angular-devkit/schematics';
13+
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';
1314
import {getWorkspacePath} from '@schematics/angular/utility/config';
1415
import {findPropertyInAstObject, insertPropertyInAstObjectInOrder} from '@schematics/angular/utility/json-utils';
1516
import {validateProjectName} from '@schematics/angular/utility/validation';
@@ -54,7 +55,10 @@ function addDevDependenciesToPackageJson(options: Schema) {
5455
};
5556

5657
const recorder = host.beginUpdate(packageJson);
57-
for (const packageName of Object.keys(devDependencies)) {
58+
const depsToInstall = Object.keys(devDependencies).filter((name) => {
59+
return !findPropertyInAstObject(devDeps, name);
60+
});
61+
for (const packageName of depsToInstall) {
5862
const version = devDependencies[packageName];
5963
const indent = 4;
6064
insertPropertyInAstObjectInOrder(recorder, devDeps, packageName, version, indent);
@@ -346,6 +350,17 @@ function addPostinstallToGenerateNgSummaries() {
346350
};
347351
}
348352

353+
/**
354+
* Schedule a task to perform npm / yarn install.
355+
*/
356+
function installNodeModules(options: Schema): Rule {
357+
return (host: Tree, context: SchematicContext) => {
358+
if (!options.skipInstall) {
359+
context.addTask(new NodePackageInstallTask());
360+
}
361+
};
362+
}
363+
349364
export default function(options: Schema): Rule {
350365
return (host: Tree) => {
351366
validateProjectName(options.name);
@@ -360,6 +375,7 @@ export default function(options: Schema): Rule {
360375
updateGitignore(),
361376
updateTsconfigJson(),
362377
upgradeRxjs(),
378+
installNodeModules(options),
363379
]);
364380
};
365381
}

‎packages/bazel/src/schematics/ng-add/index_spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ describe('ng-add schematic', () => {
100100
expect(devDeps).toContain('@bazel/karma');
101101
});
102102

103+
it('should not add an existing dev dependency', () => {
104+
expect(host.files).toContain('/package.json');
105+
const packageJson = JSON.parse(host.readContent('/package.json'));
106+
packageJson.devDependencies['@angular/bazel'] = '4.2.42';
107+
host.overwrite('/package.json', JSON.stringify(packageJson));
108+
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
109+
const content = host.readContent('/package.json');
110+
// It is possible that a dep gets added twice if the package already exists.
111+
expect(content.match(/@angular\/bazel/g) !.length).toEqual(1);
112+
const json = JSON.parse(content);
113+
expect(json.devDependencies['@angular/bazel']).toBe('4.2.42');
114+
});
115+
103116
it('should not create Bazel workspace file', () => {
104117
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
105118
const {files} = host;
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
// THIS FILE IS AUTOMATICALLY GENERATED. TO UPDATE THIS FILE YOU NEED TO CHANGE
3-
// THE CORRESPONDING JSON SCHEMA FILE. See README.md.
2+
// THIS FILE IS AUTOMATICALLY GENERATED. TO UPDATE THIS FILE YOU NEED TO CHANGE THE
3+
// CORRESPONDING JSON SCHEMA FILE, THEN RUN devkit-admin build (or bazel build ...).
44

55
// tslint:disable:no-global-tslint-disable
66
// tslint:disable
@@ -10,4 +10,8 @@ export interface Schema {
1010
* The name of the project.
1111
*/
1212
name: string;
13+
/**
14+
* When true, does not install dependency packages.
15+
*/
16+
skipInstall?: boolean;
1317
}

‎packages/bazel/src/schematics/ng-add/schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
"$source": "argv",
1313
"index": 0
1414
}
15+
},
16+
"skipInstall": {
17+
"description": "When true, does not install dependency packages.",
18+
"type": "boolean",
19+
"default": false
1520
}
1621
},
1722
"required": [

‎packages/bazel/src/schematics/ng-new/index.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ export default function(options: Schema): Rule {
1818

1919
return chain([
2020
externalSchematic('@schematics/angular', 'ng-new', options),
21-
schematic('ng-add', options, {
22-
scope: options.name,
23-
}),
21+
schematic(
22+
'ng-add', {
23+
name: options.name,
24+
// skip install since `ng-new` above will schedule the task
25+
skipInstall: true,
26+
},
27+
{
28+
scope: options.name,
29+
}),
2430
]);
2531
};
2632
}

0 commit comments

Comments
 (0)
Please sign in to comment.