Skip to content

Commit

Permalink
fix(Store): Fix import bug with ng-add and added defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts authored and MikeRyanDev committed May 23, 2018
1 parent eb1d5b3 commit ff7dc72
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 31 deletions.
12 changes: 7 additions & 5 deletions modules/store/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ describe('Store ng-add Schematic', () => {
path.join(__dirname, '../collection.json')
);
const defaultOptions: RootStoreOptions = {
name: 'foo',
skipPackageJson: false,
project: 'bar',
module: undefined,
module: 'app',
};

const projectPath = getTestProjectPath();
Expand Down Expand Up @@ -58,18 +57,21 @@ describe('Store ng-add Schematic', () => {
).toBeGreaterThanOrEqual(0);
});

it('should not be provided by default', () => {
it('should be provided by default', () => {
const options = { ...defaultOptions };

const tree = schematicRunner.runSchematic('ng-add', options, appTree);
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);
expect(content).not.toMatch(
expect(content).toMatch(
/import { reducers, metaReducers } from '\.\/reducers';/
);
expect(content).toMatch(
/StoreModule.forRoot\(reducers, { metaReducers }\)/
);
});

it('should import into a specified module', () => {
const options = { ...defaultOptions, module: 'app.module.ts' };
const options = { ...defaultOptions };

const tree = schematicRunner.runSchematic('ng-add', options, appTree);
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);
Expand Down
29 changes: 14 additions & 15 deletions modules/store/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,19 @@ function addImportToNgModule(options: RootStoreOptions): Rule {
true
);

const statePath = `${options.path}/${options.statePath}`;
const statePath = `/${options.path}/${options.statePath}`;
const relativePath = buildRelativePath(modulePath, statePath);
const srcPath = dirname(options.path as Path);
const environmentsPath = buildRelativePath(
statePath,
`/${srcPath}/environments/environment`
const [storeNgModuleImport] = addImportToModule(
source,
modulePath,
'StoreModule.forRoot(reducers, { metaReducers })',
relativePath
);

const changes = [
insertImport(source, modulePath, 'StoreModule', '@ngrx/store'),
insertImport(source, modulePath, 'reducers, metaReducers', relativePath),
addImportToModule(
source,
modulePath,
'StoreModule.forRoot(reducers, { metaReducers })',
relativePath
),
storeNgModuleImport,
];
const recorder = host.beginUpdate(modulePath);

Expand Down Expand Up @@ -103,8 +99,7 @@ export default function(options: RootStoreOptions): Rule {
return (host: Tree, context: SchematicContext) => {
options.path = getProjectPath(host, options);

const parsedPath = parseName(options.path, options.name);
options.name = parsedPath.name;
const parsedPath = parseName(options.path, '');
options.path = parsedPath.path;

const statePath = `/${options.path}/${options.statePath}/index.ts`;
Expand All @@ -115,7 +110,11 @@ export default function(options: RootStoreOptions): Rule {
);

if (options.module) {
options.module = findModuleFromOptions(host, options);
options.module = findModuleFromOptions(host, {
name: '',
module: options.module,
path: options.path,
});
}

if (options.stateInterface && options.stateInterface !== 'State') {
Expand All @@ -132,7 +131,6 @@ export default function(options: RootStoreOptions): Rule {
]);

return chain([
options && options.skipPackageJson ? noop() : addNgRxStoreToPackageJson(),
branchAndMerge(
chain([
filter(
Expand All @@ -144,6 +142,7 @@ export default function(options: RootStoreOptions): Rule {
mergeWith(templateSource),
])
),
options && options.skipPackageJson ? noop() : addNgRxStoreToPackageJson(),
])(host, context);
};
}
12 changes: 2 additions & 10 deletions modules/store/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@
"title": "NgRx Root State Management Options Schema",
"type": "object",
"properties": {
"name": {
"description": "The name of the state.",
"type": "string",
"$default": {
"$source": "argv",
"index": 0
}
},
"skipPackageJson": {
"type": "boolean",
"default": false,
Expand All @@ -21,12 +13,12 @@
"path": {
"type": "string",
"format": "path",
"description": "The path to create the component.",
"description": "The path to create the state.",
"visible": false
},
"module": {
"type": "string",
"default": "",
"default": "app",
"description": "Allows specification of the declaring module.",
"alias": "m",
"subtype": "filepath"
Expand Down
1 change: 0 additions & 1 deletion modules/store/schematics/ng-add/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export interface Schema {
name: string;
skipPackageJson?: boolean;
path?: string;
project?: string;
Expand Down

0 comments on commit ff7dc72

Please sign in to comment.