Skip to content

Commit

Permalink
Schematics: Add and import feature key for reducer state (#2017)
Browse files Browse the repository at this point in the history
Closes #1996
  • Loading branch information
John Crowson authored and brandonroberts committed Jul 18, 2019
1 parent 3cc4f3d commit 136c5eb
Show file tree
Hide file tree
Showing 40 changed files with 285 additions and 83 deletions.
2 changes: 2 additions & 0 deletions modules/data/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
group,
capitalize,
featurePath,
pluralize,
} from './utility/strings';

export {
Expand Down Expand Up @@ -64,6 +65,7 @@ export const stringUtils = {
group,
capitalize,
featurePath,
pluralize,
};

export { updatePackage } from './utility/update';
Expand Down
24 changes: 18 additions & 6 deletions modules/data/schematics-core/utility/ngrx-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ export function addReducerToStateInterface(
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
state + ': from' + stringUtils.classify(options.name) + '.State;';
const keyInsert = `[from${stringUtils.classify(
options.name
)}.${stringUtils.camelize(state)}FeatureKey]: from${stringUtils.classify(
options.name
)}.State;`;
const expr = node as any;
let position;
let toInsert;
Expand Down Expand Up @@ -144,6 +147,7 @@ export function addReducerToActionReducerMap(

return { initializer: variable.initializer, type };
})
.filter(initWithType => initWithType.type !== undefined)
.find(({ type }) => type.typeName.text === 'ActionReducerMap');

if (!actionReducerMap || !actionReducerMap.initializer) {
Expand All @@ -156,8 +160,11 @@ export function addReducerToActionReducerMap(
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
state + ': from' + stringUtils.classify(options.name) + '.reducer,';
const keyInsert = `[from${stringUtils.classify(
options.name
)}.${stringUtils.camelize(state)}FeatureKey]: from${stringUtils.classify(
options.name
)}.reducer,`;
const expr = node as any;
let position;
let toInsert;
Expand Down Expand Up @@ -227,12 +234,17 @@ export function addReducerImportToNgModule(options: any): Rule {
relativePath,
true
);
const state = options.plural
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);
const [storeNgModuleImport] = addImportToModule(
source,
modulePath,
`StoreModule.forFeature('${stringUtils.camelize(
`StoreModule.forFeature(from${stringUtils.classify(
options.name
)}', from${stringUtils.classify(options.name)}.reducer)`,
)}.${state}FeatureKey, from${stringUtils.classify(
options.name
)}.reducer)`,
relativePath
);
const changes = [...commonImports, reducerImport, storeNgModuleImport];
Expand Down
2 changes: 2 additions & 0 deletions modules/effects/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
group,
capitalize,
featurePath,
pluralize,
} from './utility/strings';

export {
Expand Down Expand Up @@ -64,6 +65,7 @@ export const stringUtils = {
group,
capitalize,
featurePath,
pluralize,
};

export { updatePackage } from './utility/update';
Expand Down
24 changes: 18 additions & 6 deletions modules/effects/schematics-core/utility/ngrx-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ export function addReducerToStateInterface(
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
state + ': from' + stringUtils.classify(options.name) + '.State;';
const keyInsert = `[from${stringUtils.classify(
options.name
)}.${stringUtils.camelize(state)}FeatureKey]: from${stringUtils.classify(
options.name
)}.State;`;
const expr = node as any;
let position;
let toInsert;
Expand Down Expand Up @@ -144,6 +147,7 @@ export function addReducerToActionReducerMap(

return { initializer: variable.initializer, type };
})
.filter(initWithType => initWithType.type !== undefined)
.find(({ type }) => type.typeName.text === 'ActionReducerMap');

if (!actionReducerMap || !actionReducerMap.initializer) {
Expand All @@ -156,8 +160,11 @@ export function addReducerToActionReducerMap(
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
state + ': from' + stringUtils.classify(options.name) + '.reducer,';
const keyInsert = `[from${stringUtils.classify(
options.name
)}.${stringUtils.camelize(state)}FeatureKey]: from${stringUtils.classify(
options.name
)}.reducer,`;
const expr = node as any;
let position;
let toInsert;
Expand Down Expand Up @@ -227,12 +234,17 @@ export function addReducerImportToNgModule(options: any): Rule {
relativePath,
true
);
const state = options.plural
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);
const [storeNgModuleImport] = addImportToModule(
source,
modulePath,
`StoreModule.forFeature('${stringUtils.camelize(
`StoreModule.forFeature(from${stringUtils.classify(
options.name
)}', from${stringUtils.classify(options.name)}.reducer)`,
)}.${state}FeatureKey, from${stringUtils.classify(
options.name
)}.reducer)`,
relativePath
);
const changes = [...commonImports, reducerImport, storeNgModuleImport];
Expand Down
2 changes: 2 additions & 0 deletions modules/entity/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
group,
capitalize,
featurePath,
pluralize,
} from './utility/strings';

export {
Expand Down Expand Up @@ -64,6 +65,7 @@ export const stringUtils = {
group,
capitalize,
featurePath,
pluralize,
};

export { updatePackage } from './utility/update';
Expand Down
24 changes: 18 additions & 6 deletions modules/entity/schematics-core/utility/ngrx-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ export function addReducerToStateInterface(
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
state + ': from' + stringUtils.classify(options.name) + '.State;';
const keyInsert = `[from${stringUtils.classify(
options.name
)}.${stringUtils.camelize(state)}FeatureKey]: from${stringUtils.classify(
options.name
)}.State;`;
const expr = node as any;
let position;
let toInsert;
Expand Down Expand Up @@ -144,6 +147,7 @@ export function addReducerToActionReducerMap(

return { initializer: variable.initializer, type };
})
.filter(initWithType => initWithType.type !== undefined)
.find(({ type }) => type.typeName.text === 'ActionReducerMap');

if (!actionReducerMap || !actionReducerMap.initializer) {
Expand All @@ -156,8 +160,11 @@ export function addReducerToActionReducerMap(
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
state + ': from' + stringUtils.classify(options.name) + '.reducer,';
const keyInsert = `[from${stringUtils.classify(
options.name
)}.${stringUtils.camelize(state)}FeatureKey]: from${stringUtils.classify(
options.name
)}.reducer,`;
const expr = node as any;
let position;
let toInsert;
Expand Down Expand Up @@ -227,12 +234,17 @@ export function addReducerImportToNgModule(options: any): Rule {
relativePath,
true
);
const state = options.plural
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);
const [storeNgModuleImport] = addImportToModule(
source,
modulePath,
`StoreModule.forFeature('${stringUtils.camelize(
`StoreModule.forFeature(from${stringUtils.classify(
options.name
)}', from${stringUtils.classify(options.name)}.reducer)`,
)}.${state}FeatureKey, from${stringUtils.classify(
options.name
)}.reducer)`,
relativePath
);
const changes = [...commonImports, reducerImport, storeNgModuleImport];
Expand Down
2 changes: 2 additions & 0 deletions modules/router-store/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
group,
capitalize,
featurePath,
pluralize,
} from './utility/strings';

export {
Expand Down Expand Up @@ -64,6 +65,7 @@ export const stringUtils = {
group,
capitalize,
featurePath,
pluralize,
};

export { updatePackage } from './utility/update';
Expand Down
24 changes: 18 additions & 6 deletions modules/router-store/schematics-core/utility/ngrx-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ export function addReducerToStateInterface(
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
state + ': from' + stringUtils.classify(options.name) + '.State;';
const keyInsert = `[from${stringUtils.classify(
options.name
)}.${stringUtils.camelize(state)}FeatureKey]: from${stringUtils.classify(
options.name
)}.State;`;
const expr = node as any;
let position;
let toInsert;
Expand Down Expand Up @@ -144,6 +147,7 @@ export function addReducerToActionReducerMap(

return { initializer: variable.initializer, type };
})
.filter(initWithType => initWithType.type !== undefined)
.find(({ type }) => type.typeName.text === 'ActionReducerMap');

if (!actionReducerMap || !actionReducerMap.initializer) {
Expand All @@ -156,8 +160,11 @@ export function addReducerToActionReducerMap(
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
state + ': from' + stringUtils.classify(options.name) + '.reducer,';
const keyInsert = `[from${stringUtils.classify(
options.name
)}.${stringUtils.camelize(state)}FeatureKey]: from${stringUtils.classify(
options.name
)}.reducer,`;
const expr = node as any;
let position;
let toInsert;
Expand Down Expand Up @@ -227,12 +234,17 @@ export function addReducerImportToNgModule(options: any): Rule {
relativePath,
true
);
const state = options.plural
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);
const [storeNgModuleImport] = addImportToModule(
source,
modulePath,
`StoreModule.forFeature('${stringUtils.camelize(
`StoreModule.forFeature(from${stringUtils.classify(
options.name
)}', from${stringUtils.classify(options.name)}.reducer)`,
)}.${state}FeatureKey, from${stringUtils.classify(
options.name
)}.reducer)`,
relativePath
);
const changes = [...commonImports, reducerImport, storeNgModuleImport];
Expand Down
2 changes: 2 additions & 0 deletions modules/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
group,
capitalize,
featurePath,
pluralize,
} from './utility/strings';

export {
Expand Down Expand Up @@ -64,6 +65,7 @@ export const stringUtils = {
group,
capitalize,
featurePath,
pluralize,
};

export { updatePackage } from './utility/update';
Expand Down
24 changes: 18 additions & 6 deletions modules/schematics-core/utility/ngrx-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ export function addReducerToStateInterface(
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
state + ': from' + stringUtils.classify(options.name) + '.State;';
const keyInsert = `[from${stringUtils.classify(
options.name
)}.${stringUtils.camelize(state)}FeatureKey]: from${stringUtils.classify(
options.name
)}.State;`;
const expr = node as any;
let position;
let toInsert;
Expand Down Expand Up @@ -144,6 +147,7 @@ export function addReducerToActionReducerMap(

return { initializer: variable.initializer, type };
})
.filter(initWithType => initWithType.type !== undefined)
.find(({ type }) => type.typeName.text === 'ActionReducerMap');

if (!actionReducerMap || !actionReducerMap.initializer) {
Expand All @@ -156,8 +160,11 @@ export function addReducerToActionReducerMap(
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);

const keyInsert =
state + ': from' + stringUtils.classify(options.name) + '.reducer,';
const keyInsert = `[from${stringUtils.classify(
options.name
)}.${stringUtils.camelize(state)}FeatureKey]: from${stringUtils.classify(
options.name
)}.reducer,`;
const expr = node as any;
let position;
let toInsert;
Expand Down Expand Up @@ -227,12 +234,17 @@ export function addReducerImportToNgModule(options: any): Rule {
relativePath,
true
);
const state = options.plural
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);
const [storeNgModuleImport] = addImportToModule(
source,
modulePath,
`StoreModule.forFeature('${stringUtils.camelize(
`StoreModule.forFeature(from${stringUtils.classify(
options.name
)}', from${stringUtils.classify(options.name)}.reducer)`,
)}.${state}FeatureKey, from${stringUtils.classify(
options.name
)}.reducer)`,
relativePath
);
const changes = [...commonImports, reducerImport, storeNgModuleImport];
Expand Down
2 changes: 2 additions & 0 deletions modules/schematics/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
group,
capitalize,
featurePath,
pluralize,
} from './utility/strings';

export {
Expand Down Expand Up @@ -64,6 +65,7 @@ export const stringUtils = {
group,
capitalize,
featurePath,
pluralize,
};

export { updatePackage } from './utility/update';
Expand Down
Loading

0 comments on commit 136c5eb

Please sign in to comment.