Skip to content

Commit ff7dc72

Browse files
brandonrobertsMikeRyanDev
authored andcommitted
fix(Store): Fix import bug with ng-add and added defaults
1 parent eb1d5b3 commit ff7dc72

File tree

4 files changed

+23
-31
lines changed

4 files changed

+23
-31
lines changed

modules/store/schematics/ng-add/index.spec.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ describe('Store ng-add Schematic', () => {
1616
path.join(__dirname, '../collection.json')
1717
);
1818
const defaultOptions: RootStoreOptions = {
19-
name: 'foo',
2019
skipPackageJson: false,
2120
project: 'bar',
22-
module: undefined,
21+
module: 'app',
2322
};
2423

2524
const projectPath = getTestProjectPath();
@@ -58,18 +57,21 @@ describe('Store ng-add Schematic', () => {
5857
).toBeGreaterThanOrEqual(0);
5958
});
6059

61-
it('should not be provided by default', () => {
60+
it('should be provided by default', () => {
6261
const options = { ...defaultOptions };
6362

6463
const tree = schematicRunner.runSchematic('ng-add', options, appTree);
6564
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);
66-
expect(content).not.toMatch(
65+
expect(content).toMatch(
6766
/import { reducers, metaReducers } from '\.\/reducers';/
6867
);
68+
expect(content).toMatch(
69+
/StoreModule.forRoot\(reducers, { metaReducers }\)/
70+
);
6971
});
7072

7173
it('should import into a specified module', () => {
72-
const options = { ...defaultOptions, module: 'app.module.ts' };
74+
const options = { ...defaultOptions };
7375

7476
const tree = schematicRunner.runSchematic('ng-add', options, appTree);
7577
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,19 @@ function addImportToNgModule(options: RootStoreOptions): Rule {
5555
true
5656
);
5757

58-
const statePath = `${options.path}/${options.statePath}`;
58+
const statePath = `/${options.path}/${options.statePath}`;
5959
const relativePath = buildRelativePath(modulePath, statePath);
60-
const srcPath = dirname(options.path as Path);
61-
const environmentsPath = buildRelativePath(
62-
statePath,
63-
`/${srcPath}/environments/environment`
60+
const [storeNgModuleImport] = addImportToModule(
61+
source,
62+
modulePath,
63+
'StoreModule.forRoot(reducers, { metaReducers })',
64+
relativePath
6465
);
6566

6667
const changes = [
6768
insertImport(source, modulePath, 'StoreModule', '@ngrx/store'),
6869
insertImport(source, modulePath, 'reducers, metaReducers', relativePath),
69-
addImportToModule(
70-
source,
71-
modulePath,
72-
'StoreModule.forRoot(reducers, { metaReducers })',
73-
relativePath
74-
),
70+
storeNgModuleImport,
7571
];
7672
const recorder = host.beginUpdate(modulePath);
7773

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

106-
const parsedPath = parseName(options.path, options.name);
107-
options.name = parsedPath.name;
102+
const parsedPath = parseName(options.path, '');
108103
options.path = parsedPath.path;
109104

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

117112
if (options.module) {
118-
options.module = findModuleFromOptions(host, options);
113+
options.module = findModuleFromOptions(host, {
114+
name: '',
115+
module: options.module,
116+
path: options.path,
117+
});
119118
}
120119

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

134133
return chain([
135-
options && options.skipPackageJson ? noop() : addNgRxStoreToPackageJson(),
136134
branchAndMerge(
137135
chain([
138136
filter(
@@ -144,6 +142,7 @@ export default function(options: RootStoreOptions): Rule {
144142
mergeWith(templateSource),
145143
])
146144
),
145+
options && options.skipPackageJson ? noop() : addNgRxStoreToPackageJson(),
147146
])(host, context);
148147
};
149148
}

modules/store/schematics/ng-add/schema.json

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44
"title": "NgRx Root State Management Options Schema",
55
"type": "object",
66
"properties": {
7-
"name": {
8-
"description": "The name of the state.",
9-
"type": "string",
10-
"$default": {
11-
"$source": "argv",
12-
"index": 0
13-
}
14-
},
157
"skipPackageJson": {
168
"type": "boolean",
179
"default": false,
@@ -21,12 +13,12 @@
2113
"path": {
2214
"type": "string",
2315
"format": "path",
24-
"description": "The path to create the component.",
16+
"description": "The path to create the state.",
2517
"visible": false
2618
},
2719
"module": {
2820
"type": "string",
29-
"default": "",
21+
"default": "app",
3022
"description": "Allows specification of the declaring module.",
3123
"alias": "m",
3224
"subtype": "filepath"

modules/store/schematics/ng-add/schema.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export interface Schema {
2-
name: string;
32
skipPackageJson?: boolean;
43
path?: string;
54
project?: string;

0 commit comments

Comments
 (0)