Skip to content

Commit 7ff8c53

Browse files
alan-agius4filipesilva
authored andcommitted
feat(@schematics/angular): add /.angular/cache to .gitignore
With this change we add `/.angular/cache` to `.gitignore`. This folder will primary be used to store the build disk cache artifacts.
1 parent 5904afd commit 7ff8c53

File tree

6 files changed

+187
-0
lines changed

6 files changed

+187
-0
lines changed

packages/angular_devkit/build_angular/test/hello-world-lib/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
!.vscode/extensions.json
2626

2727
# misc
28+
/.angular/cache
2829
/.sass-cache
2930
/connect.lock
3031
/coverage

packages/angular_devkit/build_webpack/test/basic-app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
!.vscode/extensions.json
2626

2727
# misc
28+
/.angular/cache
2829
/.sass-cache
2930
/connect.lock
3031
/coverage

packages/schematics/angular/migrations/migration-collection.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@
139139
"version": "13.0.0",
140140
"factory": "./update-13/drop-ie-polyfills",
141141
"description": "Remove polyfills required only for Internet Explorer which is no longer supported."
142+
},
143+
"update-gitignore": {
144+
"version": "13.0.0",
145+
"factory": "./update-13/update-gitignore",
146+
"description": "Updating '.gitignore' to include '.angular/cache'."
142147
}
143148
}
144149
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { Rule } from '@angular-devkit/schematics';
10+
11+
export default function (): Rule {
12+
return (tree, context) => {
13+
const gitIgnoreEntry = '/.angular/cache';
14+
const gitIgnorePath = '.gitignore';
15+
16+
const contents = tree.read(gitIgnorePath)?.toString();
17+
if (!contents) {
18+
context.logger.warn(`Could not find '${gitIgnorePath}'.`);
19+
20+
return;
21+
}
22+
23+
if (contents.includes(gitIgnoreEntry)) {
24+
// The migration has run already.
25+
return;
26+
}
27+
28+
// Try to insert the new entry in the misc section.
29+
const recorder = tree.beginUpdate(gitIgnorePath);
30+
let idx = contents.indexOf('# misc');
31+
if (idx < 0) {
32+
idx = 0;
33+
} else {
34+
switch (contents[idx + 6]) {
35+
case '\n':
36+
idx += 7;
37+
break;
38+
case '\r':
39+
idx += 8;
40+
break;
41+
default:
42+
// the word is something else.
43+
idx = 0;
44+
break;
45+
}
46+
}
47+
48+
recorder.insertLeft(idx, `${gitIgnoreEntry}\n`);
49+
tree.commitUpdate(recorder);
50+
};
51+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { tags } from '@angular-devkit/core';
10+
import { EmptyTree } from '@angular-devkit/schematics';
11+
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
12+
13+
describe('Migration to update "gitignore".', () => {
14+
const schematicName = 'update-gitignore';
15+
const schematicRunner = new SchematicTestRunner(
16+
'migrations',
17+
require.resolve('../migration-collection.json'),
18+
);
19+
20+
let tree: UnitTestTree;
21+
beforeEach(() => {
22+
tree = new UnitTestTree(new EmptyTree());
23+
});
24+
25+
it(`should not modify '.gitignore' if '.angular/cache' is already present.`, async () => {
26+
const input = tags.stripIndents`
27+
# dependencies
28+
/node_modules
29+
30+
# profiling files
31+
chrome-profiler-events*.json
32+
33+
# misc
34+
/.angular/cache
35+
/.sass-cache
36+
/connect.lock
37+
/coverage
38+
39+
# System Files
40+
.DS_Store
41+
Thumbs.db
42+
`;
43+
44+
tree.create('.gitignore', input);
45+
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
46+
expect(newTree.readContent('.gitignore')).toBe(input);
47+
});
48+
49+
it(`should insert '.angular/cache' in '# misc' section when it exists.`, async () => {
50+
const input = tags.stripIndents`
51+
# dependencies
52+
/node_modules
53+
54+
# profiling files
55+
chrome-profiler-events*.json
56+
57+
# misc
58+
/.sass-cache
59+
/connect.lock
60+
/coverage
61+
62+
# System Files
63+
.DS_Store
64+
Thumbs.db
65+
`;
66+
67+
const output = tags.stripIndents`
68+
# dependencies
69+
/node_modules
70+
71+
# profiling files
72+
chrome-profiler-events*.json
73+
74+
# misc
75+
/.angular/cache
76+
/.sass-cache
77+
/connect.lock
78+
/coverage
79+
80+
# System Files
81+
.DS_Store
82+
Thumbs.db
83+
`;
84+
85+
tree.create('.gitignore', input);
86+
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
87+
expect(newTree.readContent('.gitignore')).toBe(output);
88+
});
89+
90+
it(`should insert '.angular/cache' at the top when '# misc' section does not exist.`, async () => {
91+
const input = tags.stripIndents`
92+
# dependencies
93+
/node_modules
94+
95+
# profiling files
96+
chrome-profiler-events*.json
97+
98+
# miscs
99+
/connect.lock
100+
/coverage
101+
102+
# System Files
103+
.DS_Store
104+
Thumbs.db
105+
`;
106+
107+
const output = tags.stripIndents`
108+
/.angular/cache
109+
# dependencies
110+
/node_modules
111+
112+
# profiling files
113+
chrome-profiler-events*.json
114+
115+
# miscs
116+
/connect.lock
117+
/coverage
118+
119+
# System Files
120+
.DS_Store
121+
Thumbs.db
122+
`;
123+
124+
tree.create('.gitignore', input);
125+
const newTree = await schematicRunner.runSchematicAsync(schematicName, {}, tree).toPromise();
126+
expect(newTree.readContent('.gitignore')).toBe(output);
127+
});
128+
});

packages/schematics/angular/workspace/files/__dot__gitignore.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ chrome-profiler-events*.json
3131
.history/*
3232

3333
# misc
34+
/.angular/cache
3435
/.sass-cache
3536
/connect.lock
3637
/coverage

0 commit comments

Comments
 (0)