From c0ab56cf67f1628646b7c3043ad90e9b467f6c84 Mon Sep 17 00:00:00 2001 From: Danilo Hoffmann Date: Wed, 19 Feb 2020 00:59:48 +0100 Subject: [PATCH] fix(schematics): make store location selection more robust --- e2e/test-schematics.sh | 2 +- schematics/src/store/factory.js | 12 +++-- schematics/src/store/factory.ts | 11 ++-- schematics/src/store/factory_spec.ts | 80 ++++++++++++++++++---------- 4 files changed, 67 insertions(+), 38 deletions(-) diff --git a/e2e/test-schematics.sh b/e2e/test-schematics.sh index ba8cbdbe5c..080698ed72 100644 --- a/e2e/test-schematics.sh +++ b/e2e/test-schematics.sh @@ -19,7 +19,7 @@ npx ng g store-group training stat src/app/core/store/training/training-store.ts grep "TrainingStoreModule" src/app/core/store/core-store.module.ts -npx ng g store core/store/training/warehouses --entity warehouse +npx ng g store training/warehouses --entity warehouse stat src/app/core/store/training/warehouses/warehouses.actions.ts stat src/app/core/store/training/warehouses/warehouses.effects.ts stat src/app/core/store/training/warehouses/warehouses.reducer.ts diff --git a/schematics/src/store/factory.js b/schematics/src/store/factory.js index 762efb1694..1e1792d507 100644 --- a/schematics/src/store/factory.js +++ b/schematics/src/store/factory.js @@ -17,10 +17,12 @@ function determineStoreLocation(host, options) { extension = requestDestination.match(regex)[1]; } let feature = options.feature; - const regex2 = /store\/([a-z][a-z0-9-]+)\//; - const requestDestination2 = core_1.normalize(`${options.path}/${options.name}`); - if (regex2.test(requestDestination2)) { - feature = requestDestination2.match(regex2)[1]; + if (!extension && !feature) { + const nameWOStore = options.name.replace(/.*store\//, ''); + if (nameWOStore.includes('/')) { + const pathFragments = nameWOStore.split('/'); + feature = pathFragments[pathFragments.length - 2]; + } } let parent; let path = options.path; @@ -39,7 +41,7 @@ function determineStoreLocation(host, options) { else { throw new Error('cannot add feature store in extension'); } - return Object.assign({}, options, { parentStorePath: `${path}${parent}`, extension, + return Object.assign({}, options, { parentStorePath: `${path}${parent}`, name: options.name.split('/').pop(), extension, feature, path, parent }); diff --git a/schematics/src/store/factory.ts b/schematics/src/store/factory.ts index f8bb73ee9b..0e5d2aaa42 100644 --- a/schematics/src/store/factory.ts +++ b/schematics/src/store/factory.ts @@ -41,10 +41,12 @@ export function determineStoreLocation( } let feature = options.feature; - const regex2 = /store\/([a-z][a-z0-9-]+)\//; - const requestDestination2 = normalize(`${options.path}/${options.name}`); - if (regex2.test(requestDestination2)) { - feature = requestDestination2.match(regex2)[1]; + if (!extension && !feature) { + const nameWOStore = options.name.replace(/.*store\//, ''); + if (nameWOStore.includes('/')) { + const pathFragments = nameWOStore.split('/'); + feature = pathFragments[pathFragments.length - 2]; + } } let parent: string; @@ -66,6 +68,7 @@ export function determineStoreLocation( return { ...options, parentStorePath: `${path}${parent}`, + name: options.name.split('/').pop(), extension, feature, path, diff --git a/schematics/src/store/factory_spec.ts b/schematics/src/store/factory_spec.ts index 3e91b2391e..caf27e21d1 100644 --- a/schematics/src/store/factory_spec.ts +++ b/schematics/src/store/factory_spec.ts @@ -8,6 +8,7 @@ import { createSchematicRunner, } from '../utils/testHelper'; +import { determineStoreLocation } from './factory'; import { PwaStoreOptionsSchema as Options } from './schema'; describe('Store Schematic', () => { @@ -67,34 +68,9 @@ export interface CoreState { export const getCoreState: Selector = state => state; ` ); - appTree.create( - '/projects/bar/src/app/core/store/bar/bar-store.module.ts', - `import { NgModule } from '@angular/core'; -import { EffectsModule } from '@ngrx/effects'; -import { ActionReducerMap, StoreModule } from '@ngrx/store'; - -import { BarState } from './bar-store'; - -export const barReducers: ActionReducerMap = {}; - -export const barEffects = []; - -@NgModule({ - imports: [ - EffectsModule.forRoot(barEffects), - StoreModule.forRoot(barReducers), - ], -}) -export class BarStoreModule {} -` - ); - appTree.create( - '/projects/bar/src/app/core/store/bar/bar-store.ts', - `import { Selector } from '@ngrx/store'; - -export interface BarState {} -` - ); + appTree = await schematicRunner + .runSchematicAsync('store-group', { ...defaultOptions, name: 'bar' }, appTree) + .toPromise(); }); it('should create a store in core store by default', async () => { @@ -203,4 +179,52 @@ export interface BarState {} done(); }); }); + + describe('determineStoreLocation', () => { + it('should handle simple stores', () => { + const config = { + extension: undefined, + feature: undefined, + name: 'foobar', + parent: 'core', + parentStorePath: 'projects/bar/src/app/core/store/core', + path: 'projects/bar/src/app/core/store/', + project: 'bar', + }; + + expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'foobar' })).toEqual(config); + expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'core/store/foobar' })).toEqual(config); + }); + + it('should handle feature stores', () => { + const config = { + extension: undefined, + feature: 'bar', + name: 'foobar', + parent: 'bar', + parentStorePath: 'projects/bar/src/app/core/store/bar/bar', + path: 'projects/bar/src/app/core/store/bar/', + project: 'bar', + }; + + expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'foobar', feature: 'bar' })).toEqual(config); + expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'bar/foobar' })).toEqual(config); + expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'core/store/bar/foobar' })).toEqual(config); + }); + + it('should handle extension stores', () => { + const config = { + extension: 'bar', + feature: undefined, + name: 'foobar', + parent: 'bar', + parentStorePath: 'projects/bar/src/app/extensions/bar/store/bar', + path: 'projects/bar/src/app/extensions/bar/store/', + project: 'bar', + }; + + expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'foobar', extension: 'bar' })).toEqual(config); + expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'extensions/bar/foobar' })).toEqual(config); + }); + }); });