Skip to content

Commit

Permalink
fix(schematics): add store-freeze support
Browse files Browse the repository at this point in the history
Add support for `ngrx-store-freeze` to root modules.

Refs #174.
  • Loading branch information
ThomasBurleson authored and vsavkin committed Mar 9, 2018
1 parent 04f8e2f commit e21caa0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>",
Expand Down
16 changes: 11 additions & 5 deletions packages/schematics/src/collection/ngrx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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`)
Expand Down Expand Up @@ -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() : []`),
Expand Down Expand Up @@ -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;
};
Expand Down
11 changes: 9 additions & 2 deletions packages/schematics/src/collection/ngrx/ngrx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
});
Expand All @@ -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();
});
Expand All @@ -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();
});
Expand All @@ -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', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/schematics/src/collection/workspace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
angularCliVersion,
latestMigration,
ngrxVersion,
ngrxStoreFreezeVersion,
nxVersion,
prettierVersion, routerStoreVersion, schematicsVersion,
} from '../../../../shared/lib-versions';
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/lib-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '*';
Expand All @@ -19,6 +20,7 @@ export const libVersions = {
angularCliVersion,
angularJsVersion,
ngrxVersion,
ngrxStoreFreezeVersion,
nxVersion,
schematicsVersion,
prettierVersion,
Expand Down

0 comments on commit e21caa0

Please sign in to comment.