From 12202a71a07889d9ef407a98ab4541014993da54 Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Wed, 3 Jul 2019 15:42:22 -0500 Subject: [PATCH] feat(store): add support for minimal setup option for ng-add --- modules/store/schematics/ng-add/index.spec.ts | 25 ++++++++++--- modules/store/schematics/ng-add/index.ts | 37 +++++++++++++++++-- modules/store/schematics/ng-add/schema.json | 6 +++ modules/store/schematics/ng-add/schema.ts | 4 ++ 4 files changed, 63 insertions(+), 9 deletions(-) diff --git a/modules/store/schematics/ng-add/index.spec.ts b/modules/store/schematics/ng-add/index.spec.ts index 6cafbbbfbf..2fc11752e1 100644 --- a/modules/store/schematics/ng-add/index.spec.ts +++ b/modules/store/schematics/ng-add/index.spec.ts @@ -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', () => { @@ -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/); + }); }); diff --git a/modules/store/schematics/ng-add/index.ts b/modules/store/schematics/ng-add/index.ts index c12caa1a4b..5d5819247a 100644 --- a/modules/store/schematics/ng-add/index.ts +++ b/modules/store/schematics/ng-add/index.ts @@ -12,6 +12,7 @@ import { url, noop, move, + filter, } from '@angular-devkit/schematics'; import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks'; import { @@ -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) { @@ -122,6 +152,7 @@ export default function(options: RootStoreOptions): Rule { } const templateSource = apply(url('./files'), [ + filter(_ => (options.minimal ? false : true)), applyTemplates({ ...stringUtils, ...options, diff --git a/modules/store/schematics/ng-add/schema.json b/modules/store/schematics/ng-add/schema.json index 4ff987db09..1655b30a92 100644 --- a/modules/store/schematics/ng-add/schema.json +++ b/modules/store/schematics/ng-add/schema.json @@ -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": [] diff --git a/modules/store/schematics/ng-add/schema.ts b/modules/store/schematics/ng-add/schema.ts index f9fe5c7201..ad427bff43 100644 --- a/modules/store/schematics/ng-add/schema.ts +++ b/modules/store/schematics/ng-add/schema.ts @@ -5,4 +5,8 @@ export interface Schema { module?: string; statePath?: string; stateInterface?: string; + /** + * Setup state management without registering initial reducers. + */ + minimal?: boolean; }