From af39fd2b5d378285b24b37259446a53186dffb73 Mon Sep 17 00:00:00 2001 From: Rafa AS Date: Thu, 20 Dec 2018 13:32:17 +0100 Subject: [PATCH] feat(schematics): add project flag support to specify apps or libs (#1477) Now its possible to specify the project when generate actions, effects, entities, features, reducers and stores. The new flag is --project and his alias -p Upgrade the documentation to add the new flag and his alias Closes #1455 --- .../testing/create-workspace.ts | 4 +- modules/schematics/src/action/index.spec.ts | 21 +++++++++- modules/schematics/src/action/schema.json | 6 +++ .../schematics/src/container/index.spec.ts | 2 - modules/schematics/src/container/schema.json | 3 +- modules/schematics/src/effect/index.spec.ts | 25 +++++++++++- modules/schematics/src/effect/schema.json | 6 +++ modules/schematics/src/entity/index.spec.ts | 28 ++++++++++++- modules/schematics/src/entity/schema.json | 6 +++ modules/schematics/src/feature/index.spec.ts | 40 +++++++++++++++---- modules/schematics/src/feature/schema.json | 8 +++- modules/schematics/src/reducer/index.spec.ts | 22 +++++++++- modules/schematics/src/reducer/schema.json | 6 +++ modules/schematics/src/store/index.spec.ts | 22 +++++++++- modules/schematics/src/store/schema.json | 6 +++ .../content/guide/schematics/action.md | 6 +++ .../content/guide/schematics/effect.md | 6 +++ .../content/guide/schematics/entity.md | 6 +++ .../content/guide/schematics/feature.md | 6 +++ .../content/guide/schematics/reducer.md | 6 +++ .../ngrx.io/content/guide/schematics/store.md | 6 +++ 21 files changed, 218 insertions(+), 23 deletions(-) diff --git a/modules/schematics-core/testing/create-workspace.ts b/modules/schematics-core/testing/create-workspace.ts index ec34f25f14..0bebb4d576 100644 --- a/modules/schematics-core/testing/create-workspace.ts +++ b/modules/schematics-core/testing/create-workspace.ts @@ -3,13 +3,13 @@ import { SchematicTestRunner, } from '@angular-devkit/schematics/testing'; -const defaultWorkspaceOptions = { +export const defaultWorkspaceOptions = { name: 'workspace', newProjectRoot: 'projects', version: '6.0.0', }; -const defaultAppOptions = { +export const defaultAppOptions = { name: 'bar', inlineStyle: false, inlineTemplate: false, diff --git a/modules/schematics/src/action/index.spec.ts b/modules/schematics/src/action/index.spec.ts index fc178c06ca..f44c364741 100644 --- a/modules/schematics/src/action/index.spec.ts +++ b/modules/schematics/src/action/index.spec.ts @@ -7,6 +7,8 @@ import { Schema as ActionOptions } from './schema'; import { getTestProjectPath, createWorkspace, + defaultWorkspaceOptions, + defaultAppOptions, } from '../../../schematics-core/testing'; describe('Action Schematic', () => { @@ -16,7 +18,6 @@ describe('Action Schematic', () => { ); const defaultOptions: ActionOptions = { name: 'foo', - // path: 'app', project: 'bar', spec: false, group: false, @@ -31,6 +32,24 @@ describe('Action Schematic', () => { appTree = createWorkspace(schematicRunner, appTree); }); + it('should create an action to specified project if provided', () => { + const options = { + ...defaultOptions, + project: 'baz', + }; + + const specifiedProjectPath = getTestProjectPath(defaultWorkspaceOptions, { + ...defaultAppOptions, + name: 'baz', + }); + + const tree = schematicRunner.runSchematic('action', options, appTree); + const files = tree.files; + expect( + files.indexOf(`${specifiedProjectPath}/src/lib/foo.actions.ts`) + ).toBeGreaterThanOrEqual(0); + }); + it('should create one file', () => { const tree = schematicRunner.runSchematic( 'action', diff --git a/modules/schematics/src/action/schema.json b/modules/schematics/src/action/schema.json index b54d5f3a7e..b31c08e20b 100644 --- a/modules/schematics/src/action/schema.json +++ b/modules/schematics/src/action/schema.json @@ -18,6 +18,12 @@ "description": "The path to create the component.", "visible": false }, + "project": { + "type": "string", + "description": "The name of the project.", + "visible": false, + "aliases": ["p"] + }, "spec": { "type": "boolean", "description": "Specifies if a spec file is generated.", diff --git a/modules/schematics/src/container/index.spec.ts b/modules/schematics/src/container/index.spec.ts index dede70314f..60f0c356ff 100644 --- a/modules/schematics/src/container/index.spec.ts +++ b/modules/schematics/src/container/index.spec.ts @@ -4,7 +4,6 @@ import { } from '@angular-devkit/schematics/testing'; import * as path from 'path'; import { Schema as ContainerOptions } from './schema'; -import {} from '../../schematics-core'; import { getTestProjectPath, createWorkspace, @@ -19,7 +18,6 @@ describe('Container Schematic', () => { const defaultOptions: ContainerOptions = { name: 'foo', project: 'bar', - // path: 'src/app', inlineStyle: false, inlineTemplate: false, changeDetection: 'Default', diff --git a/modules/schematics/src/container/schema.json b/modules/schematics/src/container/schema.json index e45a296b81..7f108aeffd 100644 --- a/modules/schematics/src/container/schema.json +++ b/modules/schematics/src/container/schema.json @@ -13,7 +13,8 @@ "project": { "type": "string", "description": "The name of the project.", - "visible": false + "visible": false, + "aliases": ["p"] }, "name": { "type": "string", diff --git a/modules/schematics/src/effect/index.spec.ts b/modules/schematics/src/effect/index.spec.ts index 29409357ce..16a2276c9f 100644 --- a/modules/schematics/src/effect/index.spec.ts +++ b/modules/schematics/src/effect/index.spec.ts @@ -3,12 +3,13 @@ import { UnitTestTree, } from '@angular-devkit/schematics/testing'; import * as path from 'path'; -import {} from '../../schematics-core'; import { Schema as EffectOptions } from './schema'; import { getTestProjectPath, createWorkspace, createAppModuleWithEffects, + defaultWorkspaceOptions, + defaultAppOptions, } from '../../../schematics-core/testing'; describe('Effect Schematic', () => { @@ -19,7 +20,6 @@ describe('Effect Schematic', () => { const defaultOptions: EffectOptions = { name: 'foo', - // path: 'app', project: 'bar', spec: true, module: undefined, @@ -37,6 +37,27 @@ describe('Effect Schematic', () => { appTree = createWorkspace(schematicRunner, appTree); }); + it('should create an effect to specified project if provided', () => { + const options = { + ...defaultOptions, + project: 'baz', + }; + + const specifiedProjectPath = getTestProjectPath(defaultWorkspaceOptions, { + ...defaultAppOptions, + name: 'baz', + }); + + const tree = schematicRunner.runSchematic('effect', options, appTree); + const files = tree.files; + expect( + files.indexOf(`${specifiedProjectPath}/src/lib/foo/foo.effects.spec.ts`) + ).toBeGreaterThanOrEqual(0); + expect( + files.indexOf(`${specifiedProjectPath}/src/lib/foo/foo.effects.ts`) + ).toBeGreaterThanOrEqual(0); + }); + it('should create an effect with a spec file', () => { const options = { ...defaultOptions }; diff --git a/modules/schematics/src/effect/schema.json b/modules/schematics/src/effect/schema.json index ae06044596..7565bc833c 100644 --- a/modules/schematics/src/effect/schema.json +++ b/modules/schematics/src/effect/schema.json @@ -18,6 +18,12 @@ "description": "The path to create the component.", "visible": false }, + "project": { + "type": "string", + "description": "The name of the project.", + "visible": false, + "aliases": ["p"] + }, "flat": { "type": "boolean", "default": true, diff --git a/modules/schematics/src/entity/index.spec.ts b/modules/schematics/src/entity/index.spec.ts index b9f95ed87f..1ae89a8054 100644 --- a/modules/schematics/src/entity/index.spec.ts +++ b/modules/schematics/src/entity/index.spec.ts @@ -4,10 +4,11 @@ import { } from '@angular-devkit/schematics/testing'; import * as path from 'path'; import { Schema as EntityOptions } from './schema'; -import {} from '../../schematics-core'; import { getTestProjectPath, createWorkspace, + defaultWorkspaceOptions, + defaultAppOptions, } from '../../../schematics-core/testing'; describe('Entity Schematic', () => { @@ -18,7 +19,6 @@ describe('Entity Schematic', () => { const defaultOptions: EntityOptions = { name: 'foo', project: 'bar', - // path: 'app', spec: false, }; @@ -48,6 +48,30 @@ describe('Entity Schematic', () => { ).toBeGreaterThanOrEqual(0); }); + it('should create 3 files of an entity to specified project if provided', () => { + const options = { + ...defaultOptions, + project: 'baz', + }; + + const specifiedProjectPath = getTestProjectPath(defaultWorkspaceOptions, { + ...defaultAppOptions, + name: 'baz', + }); + + const tree = schematicRunner.runSchematic('entity', options, appTree); + const files = tree.files; + expect( + files.indexOf(`${specifiedProjectPath}/src/lib/foo.actions.ts`) + ).toBeGreaterThanOrEqual(0); + expect( + files.indexOf(`${specifiedProjectPath}/src/lib/foo.model.ts`) + ).toBeGreaterThanOrEqual(0); + expect( + files.indexOf(`${specifiedProjectPath}/src/lib/foo.reducer.ts`) + ).toBeGreaterThanOrEqual(0); + }); + it('should create a folder if flat is false', () => { const tree = schematicRunner.runSchematic( 'entity', diff --git a/modules/schematics/src/entity/schema.json b/modules/schematics/src/entity/schema.json index 0305ba3c3c..f6241efc42 100644 --- a/modules/schematics/src/entity/schema.json +++ b/modules/schematics/src/entity/schema.json @@ -18,6 +18,12 @@ "description": "The path to create the component.", "visible": false }, + "project": { + "type": "string", + "description": "The name of the project.", + "visible": false, + "aliases": ["p"] + }, "spec": { "type": "boolean", "description": "Specifies if a spec file is generated.", diff --git a/modules/schematics/src/feature/index.spec.ts b/modules/schematics/src/feature/index.spec.ts index 51d3594fdb..24d0caf52d 100644 --- a/modules/schematics/src/feature/index.spec.ts +++ b/modules/schematics/src/feature/index.spec.ts @@ -4,10 +4,11 @@ import { } from '@angular-devkit/schematics/testing'; import * as path from 'path'; import { Schema as FeatureOptions } from './schema'; -import {} from '../../schematics-core'; import { getTestProjectPath, createWorkspace, + defaultWorkspaceOptions, + defaultAppOptions, } from '../../../schematics-core/testing'; describe('Feature Schematic', () => { @@ -18,7 +19,6 @@ describe('Feature Schematic', () => { const defaultOptions: FeatureOptions = { name: 'foo', project: 'bar', - // path: 'app', module: '', spec: true, group: false, @@ -54,6 +54,36 @@ describe('Feature Schematic', () => { ).toBeGreaterThanOrEqual(0); }); + it('should create all files of a feature to specified project if provided', () => { + const options = { + ...defaultOptions, + project: 'baz', + }; + + const specifiedProjectPath = getTestProjectPath(defaultWorkspaceOptions, { + ...defaultAppOptions, + name: 'baz', + }); + + const tree = schematicRunner.runSchematic('feature', options, appTree); + const files = tree.files; + expect( + files.indexOf(`${specifiedProjectPath}/src/lib/foo.actions.ts`) + ).toBeGreaterThanOrEqual(0); + expect( + files.indexOf(`${specifiedProjectPath}/src/lib/foo.reducer.ts`) + ).toBeGreaterThanOrEqual(0); + expect( + files.indexOf(`${specifiedProjectPath}/src/lib/foo.reducer.spec.ts`) + ).toBeGreaterThanOrEqual(0); + expect( + files.indexOf(`${specifiedProjectPath}/src/lib/foo.effects.ts`) + ).toBeGreaterThanOrEqual(0); + expect( + files.indexOf(`${specifiedProjectPath}/src/lib/foo.effects.spec.ts`) + ).toBeGreaterThanOrEqual(0); + }); + it('should create all files of a feature within grouped folders if group is set', () => { const options = { ...defaultOptions, group: true }; @@ -85,12 +115,6 @@ describe('Feature Schematic', () => { }; const tree = schematicRunner.runSchematic('feature', options, appTree); - const effectsFileContent = tree.readContent( - `${projectPath}/src/app/foo/effects/foo.effects.ts` - ); - const reducerFileContent = tree.readContent( - `${projectPath}/src/app/foo/reducers/foo.reducer.ts` - ); const moduleFileContent = tree.readContent( `${projectPath}/src/app/app.module.ts` ); diff --git a/modules/schematics/src/feature/schema.json b/modules/schematics/src/feature/schema.json index 294752ea72..ffa3904a35 100644 --- a/modules/schematics/src/feature/schema.json +++ b/modules/schematics/src/feature/schema.json @@ -7,9 +7,15 @@ "path": { "type": "string", "format": "path", - "description": "The path to create the component.", + "description": "The path to create the feature.", "visible": false }, + "project": { + "type": "string", + "description": "The name of the project.", + "visible": false, + "aliases": ["p"] + }, "name": { "description": "The name of the feature.", "type": "string", diff --git a/modules/schematics/src/reducer/index.spec.ts b/modules/schematics/src/reducer/index.spec.ts index fbade280c7..f97c1aff88 100644 --- a/modules/schematics/src/reducer/index.spec.ts +++ b/modules/schematics/src/reducer/index.spec.ts @@ -4,11 +4,12 @@ import { } from '@angular-devkit/schematics/testing'; import * as path from 'path'; import { Schema as ReducerOptions } from './schema'; -import {} from '../../schematics-core'; import { getTestProjectPath, createReducers, createWorkspace, + defaultWorkspaceOptions, + defaultAppOptions, } from '../../../schematics-core/testing'; describe('Reducer Schematic', () => { @@ -19,7 +20,6 @@ describe('Reducer Schematic', () => { const defaultOptions: ReducerOptions = { name: 'foo', project: 'bar', - // path: 'app', spec: false, }; @@ -43,6 +43,24 @@ describe('Reducer Schematic', () => { ).toBeGreaterThanOrEqual(0); }); + it('should create a reducer to specified project if provided', () => { + const options = { + ...defaultOptions, + project: 'baz', + }; + + const specifiedProjectPath = getTestProjectPath(defaultWorkspaceOptions, { + ...defaultAppOptions, + name: 'baz', + }); + + const tree = schematicRunner.runSchematic('reducer', options, appTree); + const files = tree.files; + expect( + files.indexOf(`${specifiedProjectPath}/src/lib/foo.reducer.ts`) + ).toBeGreaterThanOrEqual(0); + }); + it('should create two files if spec is true', () => { const options = { ...defaultOptions, diff --git a/modules/schematics/src/reducer/schema.json b/modules/schematics/src/reducer/schema.json index 073552af5e..c5359646c9 100644 --- a/modules/schematics/src/reducer/schema.json +++ b/modules/schematics/src/reducer/schema.json @@ -18,6 +18,12 @@ "description": "The path to create the component.", "visible": false }, + "project": { + "type": "string", + "description": "The name of the project.", + "visible": false, + "aliases": ["p"] + }, "spec": { "type": "boolean", "description": "Specifies if a spec file is generated.", diff --git a/modules/schematics/src/store/index.spec.ts b/modules/schematics/src/store/index.spec.ts index 28f4daaeb0..f342a4c917 100644 --- a/modules/schematics/src/store/index.spec.ts +++ b/modules/schematics/src/store/index.spec.ts @@ -4,12 +4,12 @@ import { } from '@angular-devkit/schematics/testing'; import * as path from 'path'; import { Schema as StoreOptions } from './schema'; -import {} from '../../schematics-core'; import { getTestProjectPath, createWorkspace, + defaultWorkspaceOptions, + defaultAppOptions, } from '../../../schematics-core/testing'; -import { getProject } from '../../../schematics-core'; describe('Store Schematic', () => { const schematicRunner = new SchematicTestRunner( @@ -43,6 +43,24 @@ describe('Store Schematic', () => { ).toBeGreaterThanOrEqual(0); }); + it('should create the initial store to specified project if provided', () => { + const options = { + ...defaultOptions, + project: 'baz', + }; + + const specifiedProjectPath = getTestProjectPath(defaultWorkspaceOptions, { + ...defaultAppOptions, + name: 'baz', + }); + + const tree = schematicRunner.runSchematic('store', options, appTree); + const files = tree.files; + expect( + files.indexOf(`${specifiedProjectPath}/src/lib/reducers/index.ts`) + ).toBeGreaterThanOrEqual(0); + }); + it('should not be provided by default', () => { const options = { ...defaultOptions }; diff --git a/modules/schematics/src/store/schema.json b/modules/schematics/src/store/schema.json index b0df0bf3f4..e05993192c 100644 --- a/modules/schematics/src/store/schema.json +++ b/modules/schematics/src/store/schema.json @@ -18,6 +18,12 @@ "description": "The path to create the component.", "visible": false }, + "project": { + "type": "string", + "description": "The name of the project.", + "visible": false, + "aliases": ["p"] + }, "flat": { "type": "boolean", "default": true, diff --git a/projects/ngrx.io/content/guide/schematics/action.md b/projects/ngrx.io/content/guide/schematics/action.md index dceb302f9d..3a1c4d808e 100644 --- a/projects/ngrx.io/content/guide/schematics/action.md +++ b/projects/ngrx.io/content/guide/schematics/action.md @@ -21,6 +21,12 @@ ng generate a ActionName [options] ### Options +Provide the project name where the action files will be created. + +- `--project` + - Alias: `-p` + - Type: `string` + Nest the actions file within a folder based on the action `name`. - `--flat` diff --git a/projects/ngrx.io/content/guide/schematics/effect.md b/projects/ngrx.io/content/guide/schematics/effect.md index d39990ca63..3b62d951ee 100644 --- a/projects/ngrx.io/content/guide/schematics/effect.md +++ b/projects/ngrx.io/content/guide/schematics/effect.md @@ -20,6 +20,12 @@ ng generate ef EffectName [options] ## Options +Provide the project name where the effect files will be created. + +- `--project` + - Alias: `-p` + - Type: `string` + Nest the effects file within a folder based by the effect `name`. - `--flat` diff --git a/projects/ngrx.io/content/guide/schematics/entity.md b/projects/ngrx.io/content/guide/schematics/entity.md index 2af569422f..7f518100b5 100644 --- a/projects/ngrx.io/content/guide/schematics/entity.md +++ b/projects/ngrx.io/content/guide/schematics/entity.md @@ -20,6 +20,12 @@ ng generate en EntityName [options] ## Options +Provide the project name where the entity files will be created. + +- `--project` + - Alias: `-p` + - Type: `string` + Nest the effects file within a folder based on the entity `name`. - `--flat` diff --git a/projects/ngrx.io/content/guide/schematics/feature.md b/projects/ngrx.io/content/guide/schematics/feature.md index a63149b6b0..bb0e50dff6 100644 --- a/projects/ngrx.io/content/guide/schematics/feature.md +++ b/projects/ngrx.io/content/guide/schematics/feature.md @@ -20,6 +20,12 @@ ng generate f FeatureName [options] ## Options +Provide the project name where the feature files will be created. + +- `--project` + - Alias: `-p` + - Type: `string` + Nest the effects file within a folder based on the feature `name`. - `--flat` diff --git a/projects/ngrx.io/content/guide/schematics/reducer.md b/projects/ngrx.io/content/guide/schematics/reducer.md index 64bc7deb9c..6f8385277f 100644 --- a/projects/ngrx.io/content/guide/schematics/reducer.md +++ b/projects/ngrx.io/content/guide/schematics/reducer.md @@ -21,6 +21,12 @@ ng generate r ReducerName [options] ### Options +Provide the project name where the reducer files will be created. + +- `--project` + - Alias: `-p` + - Type: `string` + Nest the reducer file within a folder based on the reducer `name`. - `--flat` diff --git a/projects/ngrx.io/content/guide/schematics/store.md b/projects/ngrx.io/content/guide/schematics/store.md index 32ef7e6449..54867a9e9e 100644 --- a/projects/ngrx.io/content/guide/schematics/store.md +++ b/projects/ngrx.io/content/guide/schematics/store.md @@ -20,6 +20,12 @@ ng generate st State [options] ## Options +Provide the project name where the state files will be created. + +- `--project` + - Alias: `-p` + - Type: `string` + Provide the path to a file containing an `Angular Module` and the feature state will be added to its `imports` array using `StoreModule.forFeature` or `StoreModule.forRoot`. - `--module`