Skip to content

Commit 12202a7

Browse files
brandonrobertswesleygrimes
authored andcommitted
feat(store): add support for minimal setup option for ng-add
1 parent e839568 commit 12202a7

File tree

4 files changed

+63
-9
lines changed

4 files changed

+63
-9
lines changed

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,19 @@ describe('Store ng-add Schematic', () => {
5757
).toBeGreaterThanOrEqual(0);
5858
});
5959

60-
it('should be provided by default', () => {
61-
const options = { ...defaultOptions };
60+
it('should skip the initial store setup files if the minimal flag is provided', () => {
61+
const options = { ...defaultOptions, minimal: true };
6262

6363
const tree = schematicRunner.runSchematic('ng-add', options, appTree);
6464
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);
65-
expect(content).toMatch(
65+
const files = tree.files;
66+
67+
expect(content).not.toMatch(
6668
/import { reducers, metaReducers } from '\.\/reducers';/
6769
);
68-
expect(content).toMatch(
69-
/StoreModule.forRoot\(reducers, { metaReducers }\)/
70-
);
70+
expect(content).toMatch(/StoreModule.forRoot\({}/);
71+
72+
expect(files.indexOf(`${projectPath}/src/app/reducers/index.ts`)).toBe(-1);
7173
});
7274

7375
it('should import into a specified module', () => {
@@ -128,4 +130,15 @@ describe('Store ng-add Schematic', () => {
128130

129131
expect(content).toMatch(/export interface AppState {/);
130132
});
133+
134+
it('should add runtime checks by default', () => {
135+
const options = { ...defaultOptions, module: 'app.module.ts' };
136+
137+
const tree = schematicRunner.runSchematic('ng-add', options, appTree);
138+
const content = tree.readContent(`${projectPath}/src/app/app.module.ts`);
139+
140+
expect(content).toMatch(/runtimeChecks: {/);
141+
expect(content).toMatch(/strictStateImmutability: true,/);
142+
expect(content).toMatch(/strictActionImmutability: true/);
143+
});
131144
});

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
url,
1313
noop,
1414
move,
15+
filter,
1516
} from '@angular-devkit/schematics';
1617
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
1718
import {
@@ -55,20 +56,49 @@ function addImportToNgModule(options: RootStoreOptions): Rule {
5556
true
5657
);
5758

59+
const storeModuleReducers = options.minimal ? `{}` : `reducers`;
60+
61+
const storeModuleConfig = options.minimal
62+
? `{
63+
runtimeChecks: {
64+
strictStateImmutability: true,
65+
strictActionImmutability: true
66+
}
67+
}`
68+
: `{
69+
metaReducers,
70+
runtimeChecks: {
71+
strictStateImmutability: true,
72+
strictActionImmutability: true
73+
}
74+
}`;
75+
const storeModuleSetup = `StoreModule.forRoot(${storeModuleReducers}, ${storeModuleConfig})`;
76+
5877
const statePath = `/${options.path}/${options.statePath}`;
5978
const relativePath = buildRelativePath(modulePath, statePath);
6079
const [storeNgModuleImport] = addImportToModule(
6180
source,
6281
modulePath,
63-
'StoreModule.forRoot(reducers, { metaReducers })',
82+
storeModuleSetup,
6483
relativePath
6584
);
6685

67-
const changes = [
86+
let changes = [
6887
insertImport(source, modulePath, 'StoreModule', '@ngrx/store'),
69-
insertImport(source, modulePath, 'reducers, metaReducers', relativePath),
7088
storeNgModuleImport,
7189
];
90+
91+
if (!options.minimal) {
92+
changes = changes.concat([
93+
insertImport(
94+
source,
95+
modulePath,
96+
'reducers, metaReducers',
97+
relativePath
98+
),
99+
]);
100+
}
101+
72102
const recorder = host.beginUpdate(modulePath);
73103

74104
for (const change of changes) {
@@ -122,6 +152,7 @@ export default function(options: RootStoreOptions): Rule {
122152
}
123153

124154
const templateSource = apply(url('./files'), [
155+
filter(_ => (options.minimal ? false : true)),
125156
applyTemplates({
126157
...stringUtils,
127158
...options,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
"default": "State",
3838
"description": "Specifies the interface for the state.",
3939
"alias": "si"
40+
},
41+
"minimal": {
42+
"type": "boolean",
43+
"default": false,
44+
"description":
45+
"Setup state management without registering initial reducers."
4046
}
4147
},
4248
"required": []

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ export interface Schema {
55
module?: string;
66
statePath?: string;
77
stateInterface?: string;
8+
/**
9+
* Setup state management without registering initial reducers.
10+
*/
11+
minimal?: boolean;
812
}

0 commit comments

Comments
 (0)