diff --git a/packages/bazel/src/schematics/ng-add/index.ts b/packages/bazel/src/schematics/ng-add/index.ts index e71beda97a..2f0f120cde 100755 --- a/packages/bazel/src/schematics/ng-add/index.ts +++ b/packages/bazel/src/schematics/ng-add/index.ts @@ -11,7 +11,7 @@ import {JsonAstObject, parseJsonAst} from '@angular-devkit/core'; import {Rule, SchematicContext, SchematicsException, Tree, apply, applyTemplates, chain, mergeWith, url} from '@angular-devkit/schematics'; 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 {validateProjectName} from '@schematics/angular/utility/validation'; @@ -118,7 +118,7 @@ function updateGitignore() { */ function updateAngularJsonToUseBazelBuilder(options: Schema): Rule { return (host: Tree, context: SchematicContext) => { - const {name} = options; + const name = options.name !; const workspacePath = getWorkspacePath(host); if (!workspacePath) { throw new Error('Could not find angular.json'); @@ -374,6 +374,10 @@ function installNodeModules(options: Schema): Rule { export default function(options: Schema): Rule { 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); return chain([ diff --git a/packages/bazel/src/schematics/ng-add/index_spec.ts b/packages/bazel/src/schematics/ng-add/index_spec.ts index b01fc7a14d..bac2d182ba 100644 --- a/packages/bazel/src/schematics/ng-add/index_spec.ts +++ b/packages/bazel/src/schematics/ng-add/index_spec.ts @@ -55,6 +55,7 @@ describe('ng-add schematic', () => { }, }, }, + defaultProject: 'demo', })); schematicRunner = 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'); }); + 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', () => { expect(host.files).toContain('/tsconfig.json'); const original = host.readContent('/tsconfig.json'); diff --git a/packages/bazel/src/schematics/ng-add/schema.d.ts b/packages/bazel/src/schematics/ng-add/schema.d.ts index b9a512607f..a064607654 100644 --- a/packages/bazel/src/schematics/ng-add/schema.d.ts +++ b/packages/bazel/src/schematics/ng-add/schema.d.ts @@ -9,7 +9,7 @@ export interface Schema { /** * The name of the project. */ - name: string; + name?: string; /** * When true, does not install dependency packages. */ diff --git a/packages/bazel/src/schematics/ng-add/schema.json b/packages/bazel/src/schematics/ng-add/schema.json index 637c946540..0638b228f1 100755 --- a/packages/bazel/src/schematics/ng-add/schema.json +++ b/packages/bazel/src/schematics/ng-add/schema.json @@ -20,6 +20,5 @@ } }, "required": [ - "name" ] }