Skip to content

Commit c0ab56c

Browse files
committed
fix(schematics): make store location selection more robust
1 parent c02d4d2 commit c0ab56c

File tree

4 files changed

+67
-38
lines changed

4 files changed

+67
-38
lines changed

e2e/test-schematics.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ npx ng g store-group training
1919
stat src/app/core/store/training/training-store.ts
2020
grep "TrainingStoreModule" src/app/core/store/core-store.module.ts
2121

22-
npx ng g store core/store/training/warehouses --entity warehouse
22+
npx ng g store training/warehouses --entity warehouse
2323
stat src/app/core/store/training/warehouses/warehouses.actions.ts
2424
stat src/app/core/store/training/warehouses/warehouses.effects.ts
2525
stat src/app/core/store/training/warehouses/warehouses.reducer.ts

schematics/src/store/factory.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ function determineStoreLocation(host, options) {
1717
extension = requestDestination.match(regex)[1];
1818
}
1919
let feature = options.feature;
20-
const regex2 = /store\/([a-z][a-z0-9-]+)\//;
21-
const requestDestination2 = core_1.normalize(`${options.path}/${options.name}`);
22-
if (regex2.test(requestDestination2)) {
23-
feature = requestDestination2.match(regex2)[1];
20+
if (!extension && !feature) {
21+
const nameWOStore = options.name.replace(/.*store\//, '');
22+
if (nameWOStore.includes('/')) {
23+
const pathFragments = nameWOStore.split('/');
24+
feature = pathFragments[pathFragments.length - 2];
25+
}
2426
}
2527
let parent;
2628
let path = options.path;
@@ -39,7 +41,7 @@ function determineStoreLocation(host, options) {
3941
else {
4042
throw new Error('cannot add feature store in extension');
4143
}
42-
return Object.assign({}, options, { parentStorePath: `${path}${parent}`, extension,
44+
return Object.assign({}, options, { parentStorePath: `${path}${parent}`, name: options.name.split('/').pop(), extension,
4345
feature,
4446
path,
4547
parent });

schematics/src/store/factory.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ export function determineStoreLocation(
4141
}
4242

4343
let feature = options.feature;
44-
const regex2 = /store\/([a-z][a-z0-9-]+)\//;
45-
const requestDestination2 = normalize(`${options.path}/${options.name}`);
46-
if (regex2.test(requestDestination2)) {
47-
feature = requestDestination2.match(regex2)[1];
44+
if (!extension && !feature) {
45+
const nameWOStore = options.name.replace(/.*store\//, '');
46+
if (nameWOStore.includes('/')) {
47+
const pathFragments = nameWOStore.split('/');
48+
feature = pathFragments[pathFragments.length - 2];
49+
}
4850
}
4951

5052
let parent: string;
@@ -66,6 +68,7 @@ export function determineStoreLocation(
6668
return {
6769
...options,
6870
parentStorePath: `${path}${parent}`,
71+
name: options.name.split('/').pop(),
6972
extension,
7073
feature,
7174
path,

schematics/src/store/factory_spec.ts

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
createSchematicRunner,
99
} from '../utils/testHelper';
1010

11+
import { determineStoreLocation } from './factory';
1112
import { PwaStoreOptionsSchema as Options } from './schema';
1213

1314
describe('Store Schematic', () => {
@@ -67,34 +68,9 @@ export interface CoreState {
6768
export const getCoreState: Selector<CoreState, CoreState> = state => state;
6869
`
6970
);
70-
appTree.create(
71-
'/projects/bar/src/app/core/store/bar/bar-store.module.ts',
72-
`import { NgModule } from '@angular/core';
73-
import { EffectsModule } from '@ngrx/effects';
74-
import { ActionReducerMap, StoreModule } from '@ngrx/store';
75-
76-
import { BarState } from './bar-store';
77-
78-
export const barReducers: ActionReducerMap<BarState> = {};
79-
80-
export const barEffects = [];
81-
82-
@NgModule({
83-
imports: [
84-
EffectsModule.forRoot(barEffects),
85-
StoreModule.forRoot(barReducers),
86-
],
87-
})
88-
export class BarStoreModule {}
89-
`
90-
);
91-
appTree.create(
92-
'/projects/bar/src/app/core/store/bar/bar-store.ts',
93-
`import { Selector } from '@ngrx/store';
94-
95-
export interface BarState {}
96-
`
97-
);
71+
appTree = await schematicRunner
72+
.runSchematicAsync('store-group', { ...defaultOptions, name: 'bar' }, appTree)
73+
.toPromise();
9874
});
9975

10076
it('should create a store in core store by default', async () => {
@@ -203,4 +179,52 @@ export interface BarState {}
203179
done();
204180
});
205181
});
182+
183+
describe('determineStoreLocation', () => {
184+
it('should handle simple stores', () => {
185+
const config = {
186+
extension: undefined,
187+
feature: undefined,
188+
name: 'foobar',
189+
parent: 'core',
190+
parentStorePath: 'projects/bar/src/app/core/store/core',
191+
path: 'projects/bar/src/app/core/store/',
192+
project: 'bar',
193+
};
194+
195+
expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'foobar' })).toEqual(config);
196+
expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'core/store/foobar' })).toEqual(config);
197+
});
198+
199+
it('should handle feature stores', () => {
200+
const config = {
201+
extension: undefined,
202+
feature: 'bar',
203+
name: 'foobar',
204+
parent: 'bar',
205+
parentStorePath: 'projects/bar/src/app/core/store/bar/bar',
206+
path: 'projects/bar/src/app/core/store/bar/',
207+
project: 'bar',
208+
};
209+
210+
expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'foobar', feature: 'bar' })).toEqual(config);
211+
expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'bar/foobar' })).toEqual(config);
212+
expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'core/store/bar/foobar' })).toEqual(config);
213+
});
214+
215+
it('should handle extension stores', () => {
216+
const config = {
217+
extension: 'bar',
218+
feature: undefined,
219+
name: 'foobar',
220+
parent: 'bar',
221+
parentStorePath: 'projects/bar/src/app/extensions/bar/store/bar',
222+
path: 'projects/bar/src/app/extensions/bar/store/',
223+
project: 'bar',
224+
};
225+
226+
expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'foobar', extension: 'bar' })).toEqual(config);
227+
expect(determineStoreLocation(appTree, { ...defaultOptions, name: 'extensions/bar/foobar' })).toEqual(config);
228+
});
229+
});
206230
});

0 commit comments

Comments
 (0)