fix(bazel): make name param in ng add optional (#30074)

PR Close #30074
This commit is contained in:
Keen Yee Liau 2019-04-23 14:52:26 -07:00 committed by Andrew Kushnir
parent 2905bf5548
commit 0b5f480eca
4 changed files with 17 additions and 4 deletions

View File

@ -11,7 +11,7 @@
import {JsonAstObject, parseJsonAst} from '@angular-devkit/core'; import {JsonAstObject, parseJsonAst} from '@angular-devkit/core';
import {Rule, SchematicContext, SchematicsException, Tree, apply, applyTemplates, chain, mergeWith, url} from '@angular-devkit/schematics'; import {Rule, SchematicContext, SchematicsException, Tree, apply, applyTemplates, chain, mergeWith, url} from '@angular-devkit/schematics';
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks'; import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';
import {getWorkspacePath} from '@schematics/angular/utility/config'; import {getWorkspace, getWorkspacePath} from '@schematics/angular/utility/config';
import {findPropertyInAstObject, insertPropertyInAstObjectInOrder} from '@schematics/angular/utility/json-utils'; import {findPropertyInAstObject, insertPropertyInAstObjectInOrder} from '@schematics/angular/utility/json-utils';
import {validateProjectName} from '@schematics/angular/utility/validation'; import {validateProjectName} from '@schematics/angular/utility/validation';
@ -118,7 +118,7 @@ function updateGitignore() {
*/ */
function updateAngularJsonToUseBazelBuilder(options: Schema): Rule { function updateAngularJsonToUseBazelBuilder(options: Schema): Rule {
return (host: Tree, context: SchematicContext) => { return (host: Tree, context: SchematicContext) => {
const {name} = options; const name = options.name !;
const workspacePath = getWorkspacePath(host); const workspacePath = getWorkspacePath(host);
if (!workspacePath) { if (!workspacePath) {
throw new Error('Could not find angular.json'); throw new Error('Could not find angular.json');
@ -374,6 +374,10 @@ function installNodeModules(options: Schema): Rule {
export default function(options: Schema): Rule { export default function(options: Schema): Rule {
return (host: Tree) => { return (host: Tree) => {
options.name = options.name || getWorkspace(host).defaultProject;
if (!options.name) {
throw new Error('Please specify a project using "--name project-name"');
}
validateProjectName(options.name); validateProjectName(options.name);
return chain([ return chain([

View File

@ -55,6 +55,7 @@ describe('ng-add schematic', () => {
}, },
}, },
}, },
defaultProject: 'demo',
})); }));
schematicRunner = schematicRunner =
new SchematicTestRunner('@angular/bazel', require.resolve('../collection.json')); new SchematicTestRunner('@angular/bazel', require.resolve('../collection.json'));
@ -202,6 +203,15 @@ describe('ng-add schematic', () => {
expect(lint.builder).toBe('@angular-devkit/build-angular:tslint'); expect(lint.builder).toBe('@angular-devkit/build-angular:tslint');
}); });
it('should get defaultProject if name is not provided', () => {
const options = {};
host = schematicRunner.runSchematic('ng-add', options, host);
const content = host.readContent('/angular.json');
const json = JSON.parse(content);
const builder = json.projects.demo.architect.build.builder;
expect(builder).toBe('@angular/bazel:build');
});
it('should create a backup for original tsconfig.json', () => { it('should create a backup for original tsconfig.json', () => {
expect(host.files).toContain('/tsconfig.json'); expect(host.files).toContain('/tsconfig.json');
const original = host.readContent('/tsconfig.json'); const original = host.readContent('/tsconfig.json');

View File

@ -9,7 +9,7 @@ export interface Schema {
/** /**
* The name of the project. * The name of the project.
*/ */
name: string; name?: string;
/** /**
* When true, does not install dependency packages. * When true, does not install dependency packages.
*/ */

View File

@ -20,6 +20,5 @@
} }
}, },
"required": [ "required": [
"name"
] ]
} }