Skip to content

Commit 30ce2c6

Browse files
timdeschryverbrandonroberts
authored andcommitted
fix(data): search for replacements in all files when using ng-add (#1971)
1 parent fe7e6a0 commit 30ce2c6

File tree

1 file changed

+8
-33
lines changed

1 file changed

+8
-33
lines changed

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

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import {
1313
platformVersion,
1414
findModuleFromOptions,
1515
insertImport,
16-
InsertChange,
1716
getProjectPath,
1817
parseName,
1918
addImportToModule,
2019
createReplaceChange,
2120
ReplaceChange,
22-
createChangeRecorder,
21+
visitTSSourceFiles,
22+
commitChanges,
2323
} from '@ngrx/data/schematics-core';
2424
import { Schema as EntityDataOptions } from './schema';
2525

@@ -68,13 +68,7 @@ function addEntityDataToNgModule(options: EntityDataOptions): Rule {
6868
);
6969

7070
const changes = [effectsModuleImport, dateEntityNgModuleImport];
71-
const recorder = host.beginUpdate(modulePath);
72-
for (const change of changes) {
73-
if (change instanceof InsertChange) {
74-
recorder.insertLeft(change.pos, change.toAdd);
75-
}
76-
}
77-
host.commitUpdate(recorder);
71+
commitChanges(host, source.fileName, changes);
7872

7973
return host;
8074
};
@@ -103,23 +97,9 @@ function removeAngularNgRxDataFromPackageJson() {
10397
};
10498
}
10599

106-
function renameNgrxDataModule(options: EntityDataOptions) {
107-
return (host: Tree, context: SchematicContext) => {
108-
host.visit(path => {
109-
if (!path.endsWith('.ts')) {
110-
return;
111-
}
112-
113-
const sourceFile = ts.createSourceFile(
114-
path,
115-
host.read(path)!.toString(),
116-
ts.ScriptTarget.Latest
117-
);
118-
119-
if (sourceFile.isDeclarationFile) {
120-
return;
121-
}
122-
100+
function renameNgrxDataModule() {
101+
return (host: Tree) => {
102+
visitTSSourceFiles(host, sourceFile => {
123103
const ngrxDataImports = sourceFile.statements
124104
.filter(ts.isImportDeclaration)
125105
.filter(
@@ -137,12 +117,7 @@ function renameNgrxDataModule(options: EntityDataOptions) {
137117
...findNgrxDataReplacements(sourceFile),
138118
];
139119

140-
if (changes.length === 0) {
141-
return;
142-
}
143-
144-
const recorder = createChangeRecorder(host, path, changes);
145-
host.commitUpdate(recorder);
120+
commitChanges(host, sourceFile.fileName, changes);
146121
});
147122
};
148123
}
@@ -290,7 +265,7 @@ export default function(options: EntityDataOptions): Rule {
290265
options.migrateNgrxData
291266
? chain([
292267
removeAngularNgRxDataFromPackageJson(),
293-
renameNgrxDataModule(options),
268+
renameNgrxDataModule(),
294269
])
295270
: addEntityDataToNgModule(options),
296271
])(host, context);

0 commit comments

Comments
 (0)