Skip to content

Commit b05baa5

Browse files
lacolacoAndrewKushnir
authored andcommitted
fix(bazel): Add /bazel-out to .gitignore (angular#27874)
PR Close angular#27874
1 parent ad6569c commit b05baa5

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,30 @@ function overwriteMainAndIndex(options: Schema) {
7777
};
7878
}
7979

80+
function overwriteGitignore(options: Schema) {
81+
return (host: Tree) => {
82+
const gitignore = `${options.name}/.gitignore`;
83+
if (!host.exists(gitignore)) {
84+
return host;
85+
}
86+
const gitIgnoreContent = host.read(gitignore);
87+
if (!gitIgnoreContent) {
88+
throw new Error('Failed to read .gitignore content');
89+
}
90+
91+
if (gitIgnoreContent.includes('/bazel-out\n')) {
92+
return host;
93+
}
94+
const lines = gitIgnoreContent.toString().split(/\n/g);
95+
const recorder = host.beginUpdate(gitignore);
96+
const compileOutput = lines.findIndex((line: string) => line === '# compiled output');
97+
recorder.insertRight(compileOutput, '\n/bazel-out');
98+
host.commitUpdate(recorder);
99+
100+
return host;
101+
};
102+
}
103+
80104
function replacePropertyInAstObject(
81105
recorder: UpdateRecorder, node: JsonAstObject, propertyName: string, value: JsonValue,
82106
indent: number) {
@@ -176,6 +200,7 @@ export default function(options: Schema): Rule {
176200
addDevDependenciesToPackageJson(options),
177201
schematic('bazel-workspace', options),
178202
overwriteMainAndIndex(options),
203+
overwriteGitignore(options),
179204
updateWorkspaceFileToUseBazelBuilder(options),
180205
]);
181206
};

packages/bazel/src/schematics/ng-new/index_spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ describe('Ng-new Schematic', () => {
8080
expect(content).toMatch('<script src="/bundle.min.js"></script>');
8181
});
8282

83+
it('should overwrite .gitignore for bazel-out directory', () => {
84+
const options = {...defaultOptions};
85+
const host = schematicRunner.runSchematic('ng-new', options);
86+
const {files} = host;
87+
expect(files).toContain('/demo/.gitignore');
88+
const content = host.readContent('/demo/.gitignore');
89+
expect(content).toMatch('/bazel-out');
90+
});
91+
8392
it('should update angular.json to use Bazel builder', () => {
8493
const options = {...defaultOptions};
8594
const host = schematicRunner.runSchematic('ng-new', options);

0 commit comments

Comments
 (0)