Skip to content

Commit

Permalink
fix(data): search for replacements in all files when using ng-add (#1971
Browse files Browse the repository at this point in the history
)
  • Loading branch information
timdeschryver authored and brandonroberts committed Jul 7, 2019
1 parent fe7e6a0 commit 30ce2c6
Showing 1 changed file with 8 additions and 33 deletions.
41 changes: 8 additions & 33 deletions modules/data/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import {
platformVersion,
findModuleFromOptions,
insertImport,
InsertChange,
getProjectPath,
parseName,
addImportToModule,
createReplaceChange,
ReplaceChange,
createChangeRecorder,
visitTSSourceFiles,
commitChanges,
} from '@ngrx/data/schematics-core';
import { Schema as EntityDataOptions } from './schema';

Expand Down Expand Up @@ -68,13 +68,7 @@ function addEntityDataToNgModule(options: EntityDataOptions): Rule {
);

const changes = [effectsModuleImport, dateEntityNgModuleImport];
const recorder = host.beginUpdate(modulePath);
for (const change of changes) {
if (change instanceof InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
}
}
host.commitUpdate(recorder);
commitChanges(host, source.fileName, changes);

return host;
};
Expand Down Expand Up @@ -103,23 +97,9 @@ function removeAngularNgRxDataFromPackageJson() {
};
}

function renameNgrxDataModule(options: EntityDataOptions) {
return (host: Tree, context: SchematicContext) => {
host.visit(path => {
if (!path.endsWith('.ts')) {
return;
}

const sourceFile = ts.createSourceFile(
path,
host.read(path)!.toString(),
ts.ScriptTarget.Latest
);

if (sourceFile.isDeclarationFile) {
return;
}

function renameNgrxDataModule() {
return (host: Tree) => {
visitTSSourceFiles(host, sourceFile => {
const ngrxDataImports = sourceFile.statements
.filter(ts.isImportDeclaration)
.filter(
Expand All @@ -137,12 +117,7 @@ function renameNgrxDataModule(options: EntityDataOptions) {
...findNgrxDataReplacements(sourceFile),
];

if (changes.length === 0) {
return;
}

const recorder = createChangeRecorder(host, path, changes);
host.commitUpdate(recorder);
commitChanges(host, sourceFile.fileName, changes);
});
};
}
Expand Down Expand Up @@ -290,7 +265,7 @@ export default function(options: EntityDataOptions): Rule {
options.migrateNgrxData
? chain([
removeAngularNgRxDataFromPackageJson(),
renameNgrxDataModule(options),
renameNgrxDataModule(),
])
: addEntityDataToNgModule(options),
])(host, context);
Expand Down

0 comments on commit 30ce2c6

Please sign in to comment.