From e21caa0143862059d4b1089ccf3a7e3799cef0f2 Mon Sep 17 00:00:00 2001 From: Thomas Burleson Date: Thu, 1 Mar 2018 13:32:13 -0600 Subject: [PATCH] fix(schematics): add store-freeze support Add support for `ngrx-store-freeze` to root modules. Refs #174. --- package.json | 4 ++-- .../application/files/__directory__/package.json | 3 ++- packages/schematics/src/collection/ngrx/index.ts | 16 +++++++++++----- .../schematics/src/collection/ngrx/ngrx.spec.ts | 11 +++++++++-- .../schematics/src/collection/workspace/index.ts | 4 ++++ packages/shared/lib-versions.ts | 2 ++ 6 files changed, 30 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index f0d3cc3bf5534..a33c22da4cc52 100644 --- a/package.json +++ b/package.json @@ -12,8 +12,8 @@ "package": "./scripts/package.sh", "release": "./scripts/release.sh", "copy": "./scripts/copy.sh", - "test:schematics": "yarn build && ./scripts/test_schematics.sh", - "test:nx": "yarn build && ./scripts/test_nx.sh", + "test:schematics": "yarn linknpm && ./scripts/test_schematics.sh", + "test:nx": "yarn linknpm && ./scripts/test_nx.sh", "test": "yarn linknpm && ./scripts/test_nx.sh && ./scripts/test_schematics.sh", "checkformat": "echo 1", "publish_npm": "./scripts/publish.sh", diff --git a/packages/schematics/src/collection/application/files/__directory__/package.json b/packages/schematics/src/collection/application/files/__directory__/package.json index 83df523c15c08..748e02540dcdc 100644 --- a/packages/schematics/src/collection/application/files/__directory__/package.json +++ b/packages/schematics/src/collection/application/files/__directory__/package.json @@ -41,7 +41,8 @@ "@ngrx/effects": "<%= ngrxVersion %>", "@ngrx/router-store": "<%= routerStoreVersion %>", "@ngrx/store": "<%= ngrxVersion %>", - "@ngrx/store-devtools": "<%= ngrxVersion %>" + "@ngrx/store-devtools": "<%= ngrxVersion %>", + "ngrx-store-freeze": "<%= ngrxStoreFreezeVersion %>" }, "devDependencies": { "@angular/cli": "<%= angularCliVersion %>", diff --git a/packages/schematics/src/collection/ngrx/index.ts b/packages/schematics/src/collection/ngrx/index.ts index 7e0530ca5ab1e..d6ba1261e2c8a 100644 --- a/packages/schematics/src/collection/ngrx/index.ts +++ b/packages/schematics/src/collection/ngrx/index.ts @@ -17,7 +17,7 @@ import * as ts from 'typescript'; import {addImportToModule, addProviderToModule, insert} from '../../../../shared/ast-utils'; import {insertImport} from '@schematics/angular/utility/route-utils'; import {Schema} from './schema'; -import {ngrxVersion, routerStoreVersion} from '../../../../shared/lib-versions'; +import {ngrxVersion, routerStoreVersion, ngrxStoreFreezeVersion} from '../../../../shared/lib-versions'; import {serializeJson} from '../../../../shared/fileutils'; import {wrapIntoFormat} from '../../../../shared/tasks'; @@ -43,7 +43,8 @@ function addImportsToModule(name: string, options: Schema): Rule { insertImport(source, modulePath, 'StoreDevtoolsModule', '@ngrx/store-devtools'), insertImport(source, modulePath, 'environment', '../environments/environment'), insertImport(source, modulePath, 'StoreRouterConnectingModule', '@ngrx/router-store'), - ...addImportToModule(source, modulePath, `StoreModule.forRoot({})`), + insertImport(source, modulePath, 'storeFreeze', 'ngrx-store-freeze'), + ...addImportToModule(source, modulePath, `StoreModule.forRoot({},{metaReducers: !environment.production ? [storeFreeze] : []})`), ...addImportToModule(source, modulePath, `EffectsModule.forRoot([])`), ...addImportToModule(source, modulePath, `!environment.production ? StoreDevtoolsModule.instrument() : []`), ...addImportToModule(source, modulePath, `StoreRouterConnectingModule`) @@ -73,12 +74,14 @@ function addImportsToModule(name: string, options: Schema): Rule { insertImport(source, modulePath, 'StoreDevtoolsModule', '@ngrx/store-devtools'), insertImport(source, modulePath, 'environment', '../environments/environment'), insertImport(source, modulePath, 'StoreRouterConnectingModule', '@ngrx/router-store'), + insertImport(source, modulePath, 'storeFreeze', 'ngrx-store-freeze'), ...addImportToModule( source, modulePath, - `StoreModule.forRoot({${toPropertyName(name)}: ${reducerName}}, {initialState: {${toPropertyName( - name - )}: ${initName}}})` + `StoreModule.forRoot({${toPropertyName(name)}: ${reducerName}}, { + initialState: {${toPropertyName(name)}: ${initName}}, + metaReducers: !environment.production ? [storeFreeze] : [] + })` ), ...addImportToModule(source, modulePath, `EffectsModule.forRoot([${effectsName}])`), ...addImportToModule(source, modulePath, `!environment.production ? StoreDevtoolsModule.instrument() : []`), @@ -123,6 +126,9 @@ function addNgRxToPackageJson() { if (!json['dependencies']['@ngrx/store-devtools']) { json['dependencies']['@ngrx/store-devtools'] = ngrxVersion; } + if (!json['dependencies']['ngrx-store-freeze']) { + json['dependencies']['ngrx-store-freeze'] = ngrxStoreFreezeVersion; + } host.overwrite('package.json', serializeJson(json)); return host; }; diff --git a/packages/schematics/src/collection/ngrx/ngrx.spec.ts b/packages/schematics/src/collection/ngrx/ngrx.spec.ts index 1f0504744d29d..020a79d3e82b7 100644 --- a/packages/schematics/src/collection/ngrx/ngrx.spec.ts +++ b/packages/schematics/src/collection/ngrx/ngrx.spec.ts @@ -27,7 +27,7 @@ describe('ngrx', () => { ); const appModule = getFileContent(tree, '/apps/myapp/src/app/app.module.ts'); - expect(appModule).toContain('StoreModule.forRoot'); + expect(appModule).toContain('StoreModule.forRoot({},{metaReducers: !environment.production ? [storeFreeze] : []})'); expect(appModule).toContain('EffectsModule.forRoot'); expect(tree.exists('apps/myapp/src/app/+state')).toBeFalsy(); @@ -47,6 +47,8 @@ describe('ngrx', () => { const appModule = getFileContent(tree, '/apps/myapp/src/app/app.module.ts'); expect(appModule).toContain('StoreModule.forRoot'); expect(appModule).toContain('EffectsModule.forRoot'); + expect(appModule).toContain('!environment.production ? [storeFreeze] : []'); + expect(tree.exists(`/apps/myapp/src/app/+state/state.actions.ts`)).toBeTruthy(); expect(tree.exists(`/apps/myapp/src/app/+state/state.effects.ts`)).toBeTruthy(); @@ -70,6 +72,7 @@ describe('ngrx', () => { const appModule = getFileContent(tree, '/apps/myapp/src/app/app.module.ts'); expect(appModule).toContain('StoreModule.forFeature'); expect(appModule).toContain('EffectsModule.forFeature'); + expect(appModule).not.toContain('!environment.production ? [storeFreeze] : []'); expect(tree.exists(`/apps/myapp/src/app/+state/state.actions.ts`)).toBeTruthy(); }); @@ -88,6 +91,7 @@ describe('ngrx', () => { const appModule = getFileContent(tree, '/apps/myapp/src/app/app.module.ts'); expect(appModule).toContain('StoreModule.forFeature'); expect(appModule).toContain('EffectsModule.forFeature'); + expect(appModule).not.toContain('!environment.production ? [storeFreeze] : []'); expect(tree.exists(`/apps/myapp/src/app/myCustomState/state.actions.ts`)).toBeTruthy(); }); @@ -105,6 +109,7 @@ describe('ngrx', () => { const appModule = getFileContent(tree, '/apps/myapp/src/app/app.module.ts'); expect(appModule).not.toContain('StoreModule'); + expect(appModule).not.toContain('!environment.production ? [storeFreeze] : []'); expect(tree.exists(`/apps/myapp/src/app/+state/state.actions.ts`)).toBeTruthy(); }); @@ -118,11 +123,13 @@ describe('ngrx', () => { }, appTree ); - const packageJson = JSON.parse(getFileContent(tree, '/package.json')); + expect(packageJson.dependencies['@ngrx/store']).toBeDefined(); expect(packageJson.dependencies['@ngrx/router-store']).toBeDefined(); expect(packageJson.dependencies['@ngrx/effects']).toBeDefined(); + expect(packageJson.dependencies['ngrx-store-freeze']).toBeDefined(); + }); it('should error when no module is provided', () => { diff --git a/packages/schematics/src/collection/workspace/index.ts b/packages/schematics/src/collection/workspace/index.ts index 7aa71676766da..e6524e150ddc9 100644 --- a/packages/schematics/src/collection/workspace/index.ts +++ b/packages/schematics/src/collection/workspace/index.ts @@ -7,6 +7,7 @@ import { angularCliVersion, latestMigration, ngrxVersion, + ngrxStoreFreezeVersion, nxVersion, prettierVersion, routerStoreVersion, schematicsVersion, } from '../../../../shared/lib-versions'; @@ -44,6 +45,9 @@ function updatePackageJson() { if (!packageJson.dependencies['@ngrx/store-devtools']) { packageJson.dependencies['@ngrx/store-devtools'] = ngrxVersion; } + if (!packageJson.dependencies['ngrx-store-freeze']) { + packageJson.dependencies['ngrx-store-freeze'] = ngrxStoreFreezeVersion; + } if (!packageJson.devDependencies['@nrwl/schematics']) { packageJson.devDependencies['@nrwl/schematics'] = schematicsVersion; } diff --git a/packages/shared/lib-versions.ts b/packages/shared/lib-versions.ts index d8bfeb57ca5c7..950cc39b2d269 100644 --- a/packages/shared/lib-versions.ts +++ b/packages/shared/lib-versions.ts @@ -2,6 +2,7 @@ export const angularCliVersion = '1.7.1'; export const angularVersion = '5.2.6'; export const angularJsVersion = '1.6.6'; export const ngrxVersion = '5.1.0'; +export const ngrxStoreFreezeVersion = '^0.2.1'; export const routerStoreVersion = '5.0.1'; export const nxVersion = '*'; export const schematicsVersion = '*'; @@ -19,6 +20,7 @@ export const libVersions = { angularCliVersion, angularJsVersion, ngrxVersion, + ngrxStoreFreezeVersion, nxVersion, schematicsVersion, prettierVersion,