Skip to content

Commit 5fcdd3b

Browse files
timdeschryverbrandonroberts
authored andcommitted
perf: fine tune schematics to only commit changes (#1925)
1 parent 82216a5 commit 5fcdd3b

44 files changed

Lines changed: 398 additions & 220 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/data/schematics-core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export {
3333
ReplaceChange,
3434
createReplaceChange,
3535
createChangeRecorder,
36+
commitChanges,
3637
} from './utility/change';
3738

3839
export { AppConfig, getWorkspace, getWorkspacePath } from './utility/config';

modules/data/schematics-core/utility/ast-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ export function replaceImport(
687687
})
688688
.filter(({ hit }) => hit)
689689
.map(({ specifier, text }) =>
690-
createReplaceChange(sourceFile, path, specifier!, text!, importToBe)
690+
createReplaceChange(sourceFile, specifier!, text!, importToBe)
691691
);
692692

693693
return changes;

modules/data/schematics-core/utility/change.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,16 @@ export class ReplaceChange implements Change {
136136

137137
export function createReplaceChange(
138138
sourceFile: ts.SourceFile,
139-
path: Path,
140139
node: ts.Node,
141140
oldText: string,
142141
newText: string
143142
): ReplaceChange {
144-
return new ReplaceChange(path, node.getStart(sourceFile), oldText, newText);
143+
return new ReplaceChange(
144+
sourceFile.fileName,
145+
node.getStart(sourceFile),
146+
oldText,
147+
newText
148+
);
145149
}
146150

147151
export function createChangeRecorder(
@@ -162,3 +166,13 @@ export function createChangeRecorder(
162166
}
163167
return recorder;
164168
}
169+
170+
export function commitChanges(tree: Tree, path: string, changes: Change[]) {
171+
if (changes.length === 0) {
172+
return false;
173+
}
174+
175+
const recorder = createChangeRecorder(tree, path, changes);
176+
tree.commitUpdate(recorder);
177+
return true;
178+
}
Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as ts from 'typescript';
2-
import { Tree } from '@angular-devkit/schematics';
2+
import { Tree, DirEntry } from '@angular-devkit/schematics';
33

44
export function visitTSSourceFiles<Result = void>(
55
tree: Tree,
@@ -10,24 +10,35 @@ export function visitTSSourceFiles<Result = void>(
1010
) => Result | undefined
1111
): Result | undefined {
1212
let result: Result | undefined = undefined;
13+
for (const sourceFile of visit(tree.root)) {
14+
result = visitor(sourceFile, tree, result);
15+
}
1316

14-
tree.visit(path => {
15-
if (!path.endsWith('.ts')) {
16-
return;
17-
}
18-
19-
const sourceFile = ts.createSourceFile(
20-
path,
21-
tree.read(path)!.toString(),
22-
ts.ScriptTarget.Latest
23-
);
17+
return result;
18+
}
2419

25-
if (sourceFile.isDeclarationFile) {
26-
return;
20+
function* visit(directory: DirEntry): IterableIterator<ts.SourceFile> {
21+
for (const path of directory.subfiles) {
22+
if (path.endsWith('.ts') && !path.endsWith('.d.ts')) {
23+
const entry = directory.file(path);
24+
if (entry) {
25+
const content = entry.content;
26+
const source = ts.createSourceFile(
27+
entry.path,
28+
content.toString().replace(/^\uFEFF/, ''),
29+
ts.ScriptTarget.Latest,
30+
true
31+
);
32+
yield source;
33+
}
2734
}
35+
}
2836

29-
result = visitor(sourceFile, tree, result);
30-
});
37+
for (const path of directory.subdirs) {
38+
if (path === 'node_modules') {
39+
continue;
40+
}
3141

32-
return result;
42+
yield* visit(directory.dir(path));
43+
}
3344
}

modules/data/schematics/ng-add/index.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
createChangeRecorder,
2323
} from '@ngrx/data/schematics-core';
2424
import { Schema as EntityDataOptions } from './schema';
25-
import { Path } from '@angular-devkit/core';
2625

2726
function addNgRxDataToPackageJson() {
2827
return (host: Tree, context: SchematicContext) => {
@@ -133,9 +132,9 @@ function renameNgrxDataModule(options: EntityDataOptions) {
133132
}
134133

135134
const changes = [
136-
...findNgrxDataImports(sourceFile, path, ngrxDataImports),
137-
...findNgrxDataImportDeclarations(sourceFile, path, ngrxDataImports),
138-
...findNgrxDataReplacements(sourceFile, path),
135+
...findNgrxDataImports(sourceFile, ngrxDataImports),
136+
...findNgrxDataImportDeclarations(sourceFile, ngrxDataImports),
137+
...findNgrxDataReplacements(sourceFile),
139138
];
140139

141140
if (changes.length === 0) {
@@ -150,13 +149,11 @@ function renameNgrxDataModule(options: EntityDataOptions) {
150149

151150
function findNgrxDataImports(
152151
sourceFile: ts.SourceFile,
153-
path: Path,
154152
imports: ts.ImportDeclaration[]
155153
) {
156154
const changes = imports.map(specifier =>
157155
createReplaceChange(
158156
sourceFile,
159-
path,
160157
specifier.moduleSpecifier,
161158
"'ngrx-data'",
162159
"'@ngrx/data'"
@@ -168,7 +165,6 @@ function findNgrxDataImports(
168165

169166
function findNgrxDataImportDeclarations(
170167
sourceFile: ts.SourceFile,
171-
path: Path,
172168
imports: ts.ImportDeclaration[]
173169
) {
174170
const changes = imports
@@ -198,7 +194,6 @@ function findNgrxDataImportDeclarations(
198194
.map(({ specifier, text }) =>
199195
createReplaceChange(
200196
sourceFile,
201-
path,
202197
specifier!,
203198
text!,
204199
(renames as any)[text!]
@@ -208,7 +203,7 @@ function findNgrxDataImportDeclarations(
208203
return changes;
209204
}
210205

211-
function findNgrxDataReplacements(sourceFile: ts.SourceFile, path: Path) {
206+
function findNgrxDataReplacements(sourceFile: ts.SourceFile) {
212207
const renameKeys = Object.keys(renames);
213208
let changes: ReplaceChange[] = [];
214209
ts.forEachChild(sourceFile, node => find(node, changes));
@@ -252,7 +247,6 @@ function findNgrxDataReplacements(sourceFile: ts.SourceFile, path: Path) {
252247
changes.push(
253248
createReplaceChange(
254249
sourceFile,
255-
path,
256250
change.node,
257251
change.text,
258252
(renames as any)[change.text]

modules/data/tsconfig-build.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"noEmitOnError": false,
1212
"noImplicitAny": true,
1313
"noImplicitReturns": true,
14+
"downlevelIteration": true,
1415
"outDir": "../../dist/packages/data",
1516
"paths": {
1617
"@ngrx/store": ["../../dist/packages/store"],

modules/effects/schematics-core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export {
3333
ReplaceChange,
3434
createReplaceChange,
3535
createChangeRecorder,
36+
commitChanges,
3637
} from './utility/change';
3738

3839
export { AppConfig, getWorkspace, getWorkspacePath } from './utility/config';

modules/effects/schematics-core/utility/ast-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ export function replaceImport(
687687
})
688688
.filter(({ hit }) => hit)
689689
.map(({ specifier, text }) =>
690-
createReplaceChange(sourceFile, path, specifier!, text!, importToBe)
690+
createReplaceChange(sourceFile, specifier!, text!, importToBe)
691691
);
692692

693693
return changes;

modules/effects/schematics-core/utility/change.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,16 @@ export class ReplaceChange implements Change {
136136

137137
export function createReplaceChange(
138138
sourceFile: ts.SourceFile,
139-
path: Path,
140139
node: ts.Node,
141140
oldText: string,
142141
newText: string
143142
): ReplaceChange {
144-
return new ReplaceChange(path, node.getStart(sourceFile), oldText, newText);
143+
return new ReplaceChange(
144+
sourceFile.fileName,
145+
node.getStart(sourceFile),
146+
oldText,
147+
newText
148+
);
145149
}
146150

147151
export function createChangeRecorder(
@@ -162,3 +166,13 @@ export function createChangeRecorder(
162166
}
163167
return recorder;
164168
}
169+
170+
export function commitChanges(tree: Tree, path: string, changes: Change[]) {
171+
if (changes.length === 0) {
172+
return false;
173+
}
174+
175+
const recorder = createChangeRecorder(tree, path, changes);
176+
tree.commitUpdate(recorder);
177+
return true;
178+
}
Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as ts from 'typescript';
2-
import { Tree } from '@angular-devkit/schematics';
2+
import { Tree, DirEntry } from '@angular-devkit/schematics';
33

44
export function visitTSSourceFiles<Result = void>(
55
tree: Tree,
@@ -10,24 +10,35 @@ export function visitTSSourceFiles<Result = void>(
1010
) => Result | undefined
1111
): Result | undefined {
1212
let result: Result | undefined = undefined;
13+
for (const sourceFile of visit(tree.root)) {
14+
result = visitor(sourceFile, tree, result);
15+
}
1316

14-
tree.visit(path => {
15-
if (!path.endsWith('.ts')) {
16-
return;
17-
}
18-
19-
const sourceFile = ts.createSourceFile(
20-
path,
21-
tree.read(path)!.toString(),
22-
ts.ScriptTarget.Latest
23-
);
17+
return result;
18+
}
2419

25-
if (sourceFile.isDeclarationFile) {
26-
return;
20+
function* visit(directory: DirEntry): IterableIterator<ts.SourceFile> {
21+
for (const path of directory.subfiles) {
22+
if (path.endsWith('.ts') && !path.endsWith('.d.ts')) {
23+
const entry = directory.file(path);
24+
if (entry) {
25+
const content = entry.content;
26+
const source = ts.createSourceFile(
27+
entry.path,
28+
content.toString().replace(/^\uFEFF/, ''),
29+
ts.ScriptTarget.Latest,
30+
true
31+
);
32+
yield source;
33+
}
2734
}
35+
}
2836

29-
result = visitor(sourceFile, tree, result);
30-
});
37+
for (const path of directory.subdirs) {
38+
if (path === 'node_modules') {
39+
continue;
40+
}
3141

32-
return result;
42+
yield* visit(directory.dir(path));
43+
}
3344
}

0 commit comments

Comments
 (0)