diff --git a/packages/bazel/src/schematics/ng-new/index.ts b/packages/bazel/src/schematics/ng-new/index.ts index 3dbbc27bf8..cec8950d2f 100755 --- a/packages/bazel/src/schematics/ng-new/index.ts +++ b/packages/bazel/src/schematics/ng-new/index.ts @@ -77,6 +77,30 @@ function overwriteMainAndIndex(options: Schema) { }; } +function overwriteGitignore(options: Schema) { + return (host: Tree) => { + const gitignore = `${options.name}/.gitignore`; + if (!host.exists(gitignore)) { + return host; + } + const gitIgnoreContent = host.read(gitignore); + if (!gitIgnoreContent) { + throw new Error('Failed to read .gitignore content'); + } + + if (gitIgnoreContent.includes('/bazel-out\n')) { + return host; + } + const lines = gitIgnoreContent.toString().split(/\n/g); + const recorder = host.beginUpdate(gitignore); + const compileOutput = lines.findIndex((line: string) => line === '# compiled output'); + recorder.insertRight(compileOutput, '\n/bazel-out'); + host.commitUpdate(recorder); + + return host; + }; +} + function replacePropertyInAstObject( recorder: UpdateRecorder, node: JsonAstObject, propertyName: string, value: JsonValue, indent: number) { @@ -176,6 +200,7 @@ export default function(options: Schema): Rule { addDevDependenciesToPackageJson(options), schematic('bazel-workspace', options), overwriteMainAndIndex(options), + overwriteGitignore(options), updateWorkspaceFileToUseBazelBuilder(options), ]); }; diff --git a/packages/bazel/src/schematics/ng-new/index_spec.ts b/packages/bazel/src/schematics/ng-new/index_spec.ts index f28811ddc8..bec88e763c 100644 --- a/packages/bazel/src/schematics/ng-new/index_spec.ts +++ b/packages/bazel/src/schematics/ng-new/index_spec.ts @@ -80,6 +80,15 @@ describe('Ng-new Schematic', () => { expect(content).toMatch(''); }); + it('should overwrite .gitignore for bazel-out directory', () => { + const options = {...defaultOptions}; + const host = schematicRunner.runSchematic('ng-new', options); + const {files} = host; + expect(files).toContain('/demo/.gitignore'); + const content = host.readContent('/demo/.gitignore'); + expect(content).toMatch('/bazel-out'); + }); + it('should update angular.json to use Bazel builder', () => { const options = {...defaultOptions}; const host = schematicRunner.runSchematic('ng-new', options);