Skip to content

Commit af39fd2

Browse files
Rafa ASbrandonroberts
authored andcommitted
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
1 parent e921cd9 commit af39fd2

File tree

21 files changed

+218
-23
lines changed

21 files changed

+218
-23
lines changed

modules/schematics-core/testing/create-workspace.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import {
33
SchematicTestRunner,
44
} from '@angular-devkit/schematics/testing';
55

6-
const defaultWorkspaceOptions = {
6+
export const defaultWorkspaceOptions = {
77
name: 'workspace',
88
newProjectRoot: 'projects',
99
version: '6.0.0',
1010
};
1111

12-
const defaultAppOptions = {
12+
export const defaultAppOptions = {
1313
name: 'bar',
1414
inlineStyle: false,
1515
inlineTemplate: false,

modules/schematics/src/action/index.spec.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { Schema as ActionOptions } from './schema';
77
import {
88
getTestProjectPath,
99
createWorkspace,
10+
defaultWorkspaceOptions,
11+
defaultAppOptions,
1012
} from '../../../schematics-core/testing';
1113

1214
describe('Action Schematic', () => {
@@ -16,7 +18,6 @@ describe('Action Schematic', () => {
1618
);
1719
const defaultOptions: ActionOptions = {
1820
name: 'foo',
19-
// path: 'app',
2021
project: 'bar',
2122
spec: false,
2223
group: false,
@@ -31,6 +32,24 @@ describe('Action Schematic', () => {
3132
appTree = createWorkspace(schematicRunner, appTree);
3233
});
3334

35+
it('should create an action to specified project if provided', () => {
36+
const options = {
37+
...defaultOptions,
38+
project: 'baz',
39+
};
40+
41+
const specifiedProjectPath = getTestProjectPath(defaultWorkspaceOptions, {
42+
...defaultAppOptions,
43+
name: 'baz',
44+
});
45+
46+
const tree = schematicRunner.runSchematic('action', options, appTree);
47+
const files = tree.files;
48+
expect(
49+
files.indexOf(`${specifiedProjectPath}/src/lib/foo.actions.ts`)
50+
).toBeGreaterThanOrEqual(0);
51+
});
52+
3453
it('should create one file', () => {
3554
const tree = schematicRunner.runSchematic(
3655
'action',

modules/schematics/src/action/schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
"description": "The path to create the component.",
1919
"visible": false
2020
},
21+
"project": {
22+
"type": "string",
23+
"description": "The name of the project.",
24+
"visible": false,
25+
"aliases": ["p"]
26+
},
2127
"spec": {
2228
"type": "boolean",
2329
"description": "Specifies if a spec file is generated.",

modules/schematics/src/container/index.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
} from '@angular-devkit/schematics/testing';
55
import * as path from 'path';
66
import { Schema as ContainerOptions } from './schema';
7-
import {} from '../../schematics-core';
87
import {
98
getTestProjectPath,
109
createWorkspace,
@@ -19,7 +18,6 @@ describe('Container Schematic', () => {
1918
const defaultOptions: ContainerOptions = {
2019
name: 'foo',
2120
project: 'bar',
22-
// path: 'src/app',
2321
inlineStyle: false,
2422
inlineTemplate: false,
2523
changeDetection: 'Default',

modules/schematics/src/container/schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"project": {
1414
"type": "string",
1515
"description": "The name of the project.",
16-
"visible": false
16+
"visible": false,
17+
"aliases": ["p"]
1718
},
1819
"name": {
1920
"type": "string",

modules/schematics/src/effect/index.spec.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import {
33
UnitTestTree,
44
} from '@angular-devkit/schematics/testing';
55
import * as path from 'path';
6-
import {} from '../../schematics-core';
76
import { Schema as EffectOptions } from './schema';
87
import {
98
getTestProjectPath,
109
createWorkspace,
1110
createAppModuleWithEffects,
11+
defaultWorkspaceOptions,
12+
defaultAppOptions,
1213
} from '../../../schematics-core/testing';
1314

1415
describe('Effect Schematic', () => {
@@ -19,7 +20,6 @@ describe('Effect Schematic', () => {
1920

2021
const defaultOptions: EffectOptions = {
2122
name: 'foo',
22-
// path: 'app',
2323
project: 'bar',
2424
spec: true,
2525
module: undefined,
@@ -37,6 +37,27 @@ describe('Effect Schematic', () => {
3737
appTree = createWorkspace(schematicRunner, appTree);
3838
});
3939

40+
it('should create an effect to specified project if provided', () => {
41+
const options = {
42+
...defaultOptions,
43+
project: 'baz',
44+
};
45+
46+
const specifiedProjectPath = getTestProjectPath(defaultWorkspaceOptions, {
47+
...defaultAppOptions,
48+
name: 'baz',
49+
});
50+
51+
const tree = schematicRunner.runSchematic('effect', options, appTree);
52+
const files = tree.files;
53+
expect(
54+
files.indexOf(`${specifiedProjectPath}/src/lib/foo/foo.effects.spec.ts`)
55+
).toBeGreaterThanOrEqual(0);
56+
expect(
57+
files.indexOf(`${specifiedProjectPath}/src/lib/foo/foo.effects.ts`)
58+
).toBeGreaterThanOrEqual(0);
59+
});
60+
4061
it('should create an effect with a spec file', () => {
4162
const options = { ...defaultOptions };
4263

modules/schematics/src/effect/schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
"description": "The path to create the component.",
1919
"visible": false
2020
},
21+
"project": {
22+
"type": "string",
23+
"description": "The name of the project.",
24+
"visible": false,
25+
"aliases": ["p"]
26+
},
2127
"flat": {
2228
"type": "boolean",
2329
"default": true,

modules/schematics/src/entity/index.spec.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import {
44
} from '@angular-devkit/schematics/testing';
55
import * as path from 'path';
66
import { Schema as EntityOptions } from './schema';
7-
import {} from '../../schematics-core';
87
import {
98
getTestProjectPath,
109
createWorkspace,
10+
defaultWorkspaceOptions,
11+
defaultAppOptions,
1112
} from '../../../schematics-core/testing';
1213

1314
describe('Entity Schematic', () => {
@@ -18,7 +19,6 @@ describe('Entity Schematic', () => {
1819
const defaultOptions: EntityOptions = {
1920
name: 'foo',
2021
project: 'bar',
21-
// path: 'app',
2222
spec: false,
2323
};
2424

@@ -48,6 +48,30 @@ describe('Entity Schematic', () => {
4848
).toBeGreaterThanOrEqual(0);
4949
});
5050

51+
it('should create 3 files of an entity to specified project if provided', () => {
52+
const options = {
53+
...defaultOptions,
54+
project: 'baz',
55+
};
56+
57+
const specifiedProjectPath = getTestProjectPath(defaultWorkspaceOptions, {
58+
...defaultAppOptions,
59+
name: 'baz',
60+
});
61+
62+
const tree = schematicRunner.runSchematic('entity', options, appTree);
63+
const files = tree.files;
64+
expect(
65+
files.indexOf(`${specifiedProjectPath}/src/lib/foo.actions.ts`)
66+
).toBeGreaterThanOrEqual(0);
67+
expect(
68+
files.indexOf(`${specifiedProjectPath}/src/lib/foo.model.ts`)
69+
).toBeGreaterThanOrEqual(0);
70+
expect(
71+
files.indexOf(`${specifiedProjectPath}/src/lib/foo.reducer.ts`)
72+
).toBeGreaterThanOrEqual(0);
73+
});
74+
5175
it('should create a folder if flat is false', () => {
5276
const tree = schematicRunner.runSchematic(
5377
'entity',

modules/schematics/src/entity/schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
"description": "The path to create the component.",
1919
"visible": false
2020
},
21+
"project": {
22+
"type": "string",
23+
"description": "The name of the project.",
24+
"visible": false,
25+
"aliases": ["p"]
26+
},
2127
"spec": {
2228
"type": "boolean",
2329
"description": "Specifies if a spec file is generated.",

modules/schematics/src/feature/index.spec.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import {
44
} from '@angular-devkit/schematics/testing';
55
import * as path from 'path';
66
import { Schema as FeatureOptions } from './schema';
7-
import {} from '../../schematics-core';
87
import {
98
getTestProjectPath,
109
createWorkspace,
10+
defaultWorkspaceOptions,
11+
defaultAppOptions,
1112
} from '../../../schematics-core/testing';
1213

1314
describe('Feature Schematic', () => {
@@ -18,7 +19,6 @@ describe('Feature Schematic', () => {
1819
const defaultOptions: FeatureOptions = {
1920
name: 'foo',
2021
project: 'bar',
21-
// path: 'app',
2222
module: '',
2323
spec: true,
2424
group: false,
@@ -54,6 +54,36 @@ describe('Feature Schematic', () => {
5454
).toBeGreaterThanOrEqual(0);
5555
});
5656

57+
it('should create all files of a feature to specified project if provided', () => {
58+
const options = {
59+
...defaultOptions,
60+
project: 'baz',
61+
};
62+
63+
const specifiedProjectPath = getTestProjectPath(defaultWorkspaceOptions, {
64+
...defaultAppOptions,
65+
name: 'baz',
66+
});
67+
68+
const tree = schematicRunner.runSchematic('feature', options, appTree);
69+
const files = tree.files;
70+
expect(
71+
files.indexOf(`${specifiedProjectPath}/src/lib/foo.actions.ts`)
72+
).toBeGreaterThanOrEqual(0);
73+
expect(
74+
files.indexOf(`${specifiedProjectPath}/src/lib/foo.reducer.ts`)
75+
).toBeGreaterThanOrEqual(0);
76+
expect(
77+
files.indexOf(`${specifiedProjectPath}/src/lib/foo.reducer.spec.ts`)
78+
).toBeGreaterThanOrEqual(0);
79+
expect(
80+
files.indexOf(`${specifiedProjectPath}/src/lib/foo.effects.ts`)
81+
).toBeGreaterThanOrEqual(0);
82+
expect(
83+
files.indexOf(`${specifiedProjectPath}/src/lib/foo.effects.spec.ts`)
84+
).toBeGreaterThanOrEqual(0);
85+
});
86+
5787
it('should create all files of a feature within grouped folders if group is set', () => {
5888
const options = { ...defaultOptions, group: true };
5989

@@ -85,12 +115,6 @@ describe('Feature Schematic', () => {
85115
};
86116

87117
const tree = schematicRunner.runSchematic('feature', options, appTree);
88-
const effectsFileContent = tree.readContent(
89-
`${projectPath}/src/app/foo/effects/foo.effects.ts`
90-
);
91-
const reducerFileContent = tree.readContent(
92-
`${projectPath}/src/app/foo/reducers/foo.reducer.ts`
93-
);
94118
const moduleFileContent = tree.readContent(
95119
`${projectPath}/src/app/app.module.ts`
96120
);

0 commit comments

Comments
 (0)