From 15bc0df50eeef30f631b78cf2a82e6cbc9d28487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Basi=C5=84ski?= <74178853+mateuszbasinski@users.noreply.github.com> Date: Wed, 26 May 2021 15:14:44 +0200 Subject: [PATCH] feat(schematics): add the ability to create actions with the prefix (#3025) --- ..._name@dasherize__.actions.spec.ts.template | 4 ++-- .../__name@dasherize__.actions.ts.template | 12 +++++----- ..._name@dasherize__.actions.spec.ts.template | 2 +- .../__name@dasherize__.actions.ts.template | 22 ++++++++--------- modules/schematics/src/action/index.spec.ts | 24 +++++++++++++++++++ modules/schematics/src/action/index.ts | 13 +++++----- modules/schematics/src/action/schema.json | 6 +++++ modules/schematics/src/action/schema.ts | 5 ++++ .../content/guide/schematics/action.md | 6 +++++ 9 files changed, 67 insertions(+), 27 deletions(-) diff --git a/modules/schematics/src/action/creator-files/__name@dasherize@if-flat__/__name@dasherize__.actions.spec.ts.template b/modules/schematics/src/action/creator-files/__name@dasherize@if-flat__/__name@dasherize__.actions.spec.ts.template index 54ee181146..b255498ffd 100644 --- a/modules/schematics/src/action/creator-files/__name@dasherize@if-flat__/__name@dasherize__.actions.spec.ts.template +++ b/modules/schematics/src/action/creator-files/__name@dasherize@if-flat__/__name@dasherize__.actions.spec.ts.template @@ -1,7 +1,7 @@ import * as from<%= classify(name) %> from './<%= dasherize(name) %>.actions'; -describe('load<%= classify(name) %>s', () => { +describe('<%= prefix %><%= classify(name) %>s', () => { it('should return an action', () => { - expect(from<%= classify(name) %>.load<%= classify(name) %>s().type).toBe('[<%= classify(name) %>] Load <%= classify(name) %>s'); + expect(from<%= classify(name) %>.<%= prefix %><%= classify(name) %>s().type).toBe('[<%= classify(name) %>] <%= classify(prefix) %> <%= classify(name) %>s'); }); }); diff --git a/modules/schematics/src/action/creator-files/__name@dasherize@if-flat__/__name@dasherize__.actions.ts.template b/modules/schematics/src/action/creator-files/__name@dasherize@if-flat__/__name@dasherize__.actions.ts.template index ba5ff92ce7..fefe45376d 100644 --- a/modules/schematics/src/action/creator-files/__name@dasherize@if-flat__/__name@dasherize__.actions.ts.template +++ b/modules/schematics/src/action/creator-files/__name@dasherize@if-flat__/__name@dasherize__.actions.ts.template @@ -1,15 +1,15 @@ import { createAction, props } from '@ngrx/store'; -export const load<%= classify(name) %>s = createAction( - '[<%= classify(name) %>] Load <%= classify(name) %>s' +export const <%= prefix %><%= classify(name) %>s = createAction( + '[<%= classify(name) %>] <%= classify(prefix) %> <%= classify(name) %>s' ); -<% if (api) { %>export const load<%= classify(name) %>sSuccess = createAction( - '[<%= classify(name) %>] Load <%= classify(name) %>s Success', +<% if (api) { %>export const <%= prefix %><%= classify(name) %>sSuccess = createAction( + '[<%= classify(name) %>] <%= classify(prefix) %> <%= classify(name) %>s Success', props<{ data: any }>() );<% } %> -<% if (api) { %>export const load<%= classify(name) %>sFailure = createAction( - '[<%= classify(name) %>] Load <%= classify(name) %>s Failure', +<% if (api) { %>export const <%= prefix %><%= classify(name) %>sFailure = createAction( + '[<%= classify(name) %>] <%= classify(prefix) %> <%= classify(name) %>s Failure', props<{ error: any }>() );<% } %> diff --git a/modules/schematics/src/action/files/__name@dasherize@if-flat__/__name@dasherize__.actions.spec.ts.template b/modules/schematics/src/action/files/__name@dasherize@if-flat__/__name@dasherize__.actions.spec.ts.template index 0d100f89a5..5abcf4591a 100644 --- a/modules/schematics/src/action/files/__name@dasherize@if-flat__/__name@dasherize__.actions.spec.ts.template +++ b/modules/schematics/src/action/files/__name@dasherize@if-flat__/__name@dasherize__.actions.spec.ts.template @@ -2,6 +2,6 @@ import * as <%= classify(name) %>Actions from './<%= dasherize(name) %>.actions' describe('<%= classify(name) %>', () => { it('should create an instance', () => { - expect(new <%= classify(name)%>Actions.Load<%= classify(name) %>s()).toBeTruthy(); + expect(new <%= classify(name)%>Actions.<%= prefix %><%= classify(name) %>s()).toBeTruthy(); }); }); diff --git a/modules/schematics/src/action/files/__name@dasherize@if-flat__/__name@dasherize__.actions.ts.template b/modules/schematics/src/action/files/__name@dasherize@if-flat__/__name@dasherize__.actions.ts.template index 98cd8c8a93..92afc4d7a6 100644 --- a/modules/schematics/src/action/files/__name@dasherize@if-flat__/__name@dasherize__.actions.ts.template +++ b/modules/schematics/src/action/files/__name@dasherize@if-flat__/__name@dasherize__.actions.ts.template @@ -1,24 +1,24 @@ import { Action } from '@ngrx/store'; export enum <%= classify(name) %>ActionTypes { - Load<%= classify(name) %>s = '[<%= classify(name) %>] Load <%= classify(name) %>s', - <% if (api) { %>Load<%= classify(name) %>sSuccess = '[<%= classify(name) %>] Load <%= classify(name) %>s Success',<% } %> - <% if (api) { %>Load<%= classify(name) %>sFailure = '[<%= classify(name) %>] Load <%= classify(name) %>s Failure',<% } %> + <%= prefix %><%= classify(name) %>s = '[<%= classify(name) %>] <%= prefix %> <%= classify(name) %>s', + <% if (api) { %><%= prefix %><%= classify(name) %>sSuccess = '[<%= classify(name) %>] <%= prefix %> <%= classify(name) %>s Success',<% } %> + <% if (api) { %><%= prefix %><%= classify(name) %>sFailure = '[<%= classify(name) %>] <%= prefix %> <%= classify(name) %>s Failure',<% } %> } -export class Load<%= classify(name) %>s implements Action { - readonly type = <%= classify(name) %>ActionTypes.Load<%= classify(name) %>s; +export class <%= prefix %><%= classify(name) %>s implements Action { + readonly type = <%= classify(name) %>ActionTypes.<%= prefix %><%= classify(name) %>s; } <% if (api) { %> -export class Load<%= classify(name) %>sSuccess implements Action { - readonly type = <%= classify(name) %>ActionTypes.Load<%= classify(name) %>sSuccess; +export class <%= prefix %><%= classify(name) %>sSuccess implements Action { + readonly type = <%= classify(name) %>ActionTypes.<%= prefix %><%= classify(name) %>sSuccess; constructor(public payload: { data: any }) { } } -export class Load<%= classify(name) %>sFailure implements Action { - readonly type = <%= classify(name) %>ActionTypes.Load<%= classify(name) %>sFailure; +export class <%= prefix %><%= classify(name) %>sFailure implements Action { + readonly type = <%= classify(name) %>ActionTypes.<%= prefix %><%= classify(name) %>sFailure; constructor(public payload: { error: any }) { } } <% } %> -<% if (api) { %>export type <%= classify(name) %>Actions = Load<%= classify(name) %>s | Load<%= classify(name) %>sSuccess | Load<%= classify(name) %>sFailure;<% } %> -<% if (!api) { %>export type <%= classify(name) %>Actions = Load<%= classify(name) %>s;<% } %> +<% if (api) { %>export type <%= classify(name) %>Actions = <%= prefix %><%= classify(name) %>s | <%= prefix %><%= classify(name) %>sSuccess | <%= prefix %><%= classify(name) %>sFailure;<% } %> +<% if (!api) { %>export type <%= classify(name) %>Actions = <%= prefix %><%= classify(name) %>s;<% } %> diff --git a/modules/schematics/src/action/index.spec.ts b/modules/schematics/src/action/index.spec.ts index 6b9ffd2ae0..48c01d5bb2 100644 --- a/modules/schematics/src/action/index.spec.ts +++ b/modules/schematics/src/action/index.spec.ts @@ -10,6 +10,7 @@ import { defaultWorkspaceOptions, defaultAppOptions, } from '@ngrx/schematics-core/testing'; +import { capitalize } from '../../schematics-core/utility/strings'; describe('Action Schematic', () => { const schematicRunner = new SchematicTestRunner( @@ -18,6 +19,7 @@ describe('Action Schematic', () => { ); const defaultOptions: ActionOptions = { name: 'foo', + prefix: 'load', project: 'bar', group: false, flat: true, @@ -173,6 +175,28 @@ describe('Action Schematic', () => { expect(fileContent).toMatch(/\[Foo\] Load Foos Failure/); expect(fileContent).toMatch(/props<{ error: any }>\(\)/); }); + + it.each(['load', 'delete', 'update'])( + 'should create a action with prefix', + async (prefix) => { + const tree = await schematicRunner + .runSchematicAsync( + 'action', + { ...creatorDefaultOptions, prefix: prefix }, + appTree + ) + .toPromise(); + const fileContent = tree.readContent( + `${projectPath}/src/app/foo.actions.ts` + ); + expect(fileContent).toMatch( + new RegExp(`export const ${prefix}Foos = createAction`) + ); + expect(fileContent).toMatch( + new RegExp(`'\\[Foo] ${capitalize(prefix)} Foos'`) + ); + } + ); }); describe('api', () => { diff --git a/modules/schematics/src/action/index.ts b/modules/schematics/src/action/index.ts index 171b83a00c..2d59ba3493 100644 --- a/modules/schematics/src/action/index.ts +++ b/modules/schematics/src/action/index.ts @@ -1,6 +1,5 @@ import { Rule, - SchematicsException, apply, applyTemplates, branchAndMerge, @@ -9,22 +8,22 @@ import { mergeWith, move, noop, - template, url, Tree, SchematicContext, } from '@angular-devkit/schematics'; import { Schema as ActionOptions } from './schema'; -import { - getProjectPath, - stringUtils, - parseName, -} from '@ngrx/schematics/schematics-core'; +import { getProjectPath, stringUtils, parseName } from '../../schematics-core'; +import { capitalize, camelize } from '../../schematics-core/utility/strings'; export default function (options: ActionOptions): Rule { return (host: Tree, context: SchematicContext) => { options.path = getProjectPath(host, options); + options.prefix = options.creators + ? camelize(options.prefix || 'load') + : capitalize(options.prefix || 'load'); + const parsedPath = parseName(options.path, options.name); options.name = parsedPath.name; options.path = parsedPath.path; diff --git a/modules/schematics/src/action/schema.json b/modules/schematics/src/action/schema.json index e3d43b9ea8..887268461d 100644 --- a/modules/schematics/src/action/schema.json +++ b/modules/schematics/src/action/schema.json @@ -13,6 +13,12 @@ }, "x-prompt": "What should be the name of the action?" }, + "prefix": { + "description": "The prefix of the action.", + "type": "string", + "default": "load", + "x-prompt": "What should be the prefix of the action?" + }, "path": { "type": "string", "format": "path", diff --git a/modules/schematics/src/action/schema.ts b/modules/schematics/src/action/schema.ts index bf0cf121a7..dc1c1a9169 100644 --- a/modules/schematics/src/action/schema.ts +++ b/modules/schematics/src/action/schema.ts @@ -4,6 +4,11 @@ export interface Schema { */ name: string; + /** + * The prefix for the actions. + */ + prefix: string; + /** * The path to create the component. */ diff --git a/projects/ngrx.io/content/guide/schematics/action.md b/projects/ngrx.io/content/guide/schematics/action.md index af8e3950fb..446a34d3fe 100644 --- a/projects/ngrx.io/content/guide/schematics/action.md +++ b/projects/ngrx.io/content/guide/schematics/action.md @@ -60,6 +60,12 @@ Generate a spec file alongside the action file. - Type: `boolean` - Default: `false` +Specify the prefix for the actions. + +- `--prefix` + - Type: `string` + - Default: `load` + ## Examples Generate a `User` actions file with an associated spec file.