Skip to content

Commit

Permalink
refactor(Schematics): Use wildcard for feature store imports (#719)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts authored and MikeRyanDev committed Jan 15, 2018
1 parent 95ff6c8 commit b597172
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions modules/schematics/src/feature/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default function(options: FeatureOptions): Rule {
return (host: Tree, context: SchematicContext) => {
return chain([
schematic('action', {
flat: options.flat,
name: options.name,
path: options.path,
sourceDir: options.sourceDir,
Expand Down
10 changes: 9 additions & 1 deletion modules/schematics/src/store/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,15 @@ describe('Store Schematic', () => {
const tree = schematicRunner.runSchematic('store', options, appTree);
const content = getFileContent(tree, '/src/app/app.module.ts');
expect(content).toMatch(
/StoreModule\.forFeature\('foo', reducers, { metaReducers }\)/
/StoreModule\.forFeature\('foo', fromFoo\.reducers, { metaReducers: fromFoo.metaReducers }\)/
);
});

it('should use a wildcard for a feature import ', () => {
const options = { ...defaultOptions, root: false, module: 'app.module.ts' };

const tree = schematicRunner.runSchematic('store', options, appTree);
const content = getFileContent(tree, '/src/app/app.module.ts');
expect(content).toMatch(/import \* as fromFoo from '\.\/reducers';/);
});
});
21 changes: 19 additions & 2 deletions modules/schematics/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,30 @@ function addImportToNgModule(options: ServiceOptions): Rule {
? `StoreModule.forRoot(reducers, { metaReducers })`
: `StoreModule.forFeature('${stringUtils.camelize(
options.name
)}', reducers, { metaReducers })`,
)}', from${stringUtils.classify(
options.name
)}.reducers, { metaReducers: from${stringUtils.classify(
options.name
)}.metaReducers })`,
relativePath
).shift();

let commonImports = [
insertImport(source, modulePath, 'StoreModule', '@ngrx/store'),
insertImport(source, modulePath, 'reducers, metaReducers', relativePath),
options.root
? insertImport(
source,
modulePath,
'reducers, metaReducers',
relativePath
)
: insertImport(
source,
modulePath,
`* as from${stringUtils.classify(options.name)}`,
relativePath,
true
),
storeNgModuleImport,
];
let rootImports: (Change | undefined)[] = [];
Expand Down

0 comments on commit b597172

Please sign in to comment.