Skip to content

Commit

Permalink
feat(store): add support for minimal setup option for ng-add
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts authored and wesleygrimes committed Jul 9, 2019
1 parent e839568 commit 12202a7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
25 changes: 19 additions & 6 deletions modules/store/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,19 @@ describe('Store ng-add Schematic', () => {
).toBeGreaterThanOrEqual(0);
});

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

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

expect(content).not.toMatch(
/import { reducers, metaReducers } from '\.\/reducers';/
);
expect(content).toMatch(
/StoreModule.forRoot\(reducers, { metaReducers }\)/
);
expect(content).toMatch(/StoreModule.forRoot\({}/);

expect(files.indexOf(`${projectPath}/src/app/reducers/index.ts`)).toBe(-1);
});

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

expect(content).toMatch(/export interface AppState {/);
});

it('should add runtime checks by default', () => {
const options = { ...defaultOptions, module: 'app.module.ts' };

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

expect(content).toMatch(/runtimeChecks: {/);
expect(content).toMatch(/strictStateImmutability: true,/);
expect(content).toMatch(/strictActionImmutability: true/);
});
});
37 changes: 34 additions & 3 deletions modules/store/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
url,
noop,
move,
filter,
} from '@angular-devkit/schematics';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import {
Expand Down Expand Up @@ -55,20 +56,49 @@ function addImportToNgModule(options: RootStoreOptions): Rule {
true
);

const storeModuleReducers = options.minimal ? `{}` : `reducers`;

const storeModuleConfig = options.minimal
? `{
runtimeChecks: {
strictStateImmutability: true,
strictActionImmutability: true
}
}`
: `{
metaReducers,
runtimeChecks: {
strictStateImmutability: true,
strictActionImmutability: true
}
}`;
const storeModuleSetup = `StoreModule.forRoot(${storeModuleReducers}, ${storeModuleConfig})`;

const statePath = `/${options.path}/${options.statePath}`;
const relativePath = buildRelativePath(modulePath, statePath);
const [storeNgModuleImport] = addImportToModule(
source,
modulePath,
'StoreModule.forRoot(reducers, { metaReducers })',
storeModuleSetup,
relativePath
);

const changes = [
let changes = [
insertImport(source, modulePath, 'StoreModule', '@ngrx/store'),
insertImport(source, modulePath, 'reducers, metaReducers', relativePath),
storeNgModuleImport,
];

if (!options.minimal) {
changes = changes.concat([
insertImport(
source,
modulePath,
'reducers, metaReducers',
relativePath
),
]);
}

const recorder = host.beginUpdate(modulePath);

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

const templateSource = apply(url('./files'), [
filter(_ => (options.minimal ? false : true)),
applyTemplates({
...stringUtils,
...options,
Expand Down
6 changes: 6 additions & 0 deletions modules/store/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
"default": "State",
"description": "Specifies the interface for the state.",
"alias": "si"
},
"minimal": {
"type": "boolean",
"default": false,
"description":
"Setup state management without registering initial reducers."
}
},
"required": []
Expand Down
4 changes: 4 additions & 0 deletions modules/store/schematics/ng-add/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ export interface Schema {
module?: string;
statePath?: string;
stateInterface?: string;
/**
* Setup state management without registering initial reducers.
*/
minimal?: boolean;
}

0 comments on commit 12202a7

Please sign in to comment.