Skip to content

Commit b99d9ff

Browse files
brandonrobertsMikeRyanDev
authored andcommitted
fix(Schematics): Upgrade schematics to new CLI structure
BREAKING CHANGE: Minimum dependency for @ngrx/schematics has changed: @angular-devkit/core: ^0.5.0 @angular-devkit/schematics: ^0.5.0
1 parent ca5df12 commit b99d9ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1214
-479
lines changed

angular.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{
2+
"$schema":
3+
"./node_modules/@angular-devkit/core/src/workspace/workspace-schema.json",
24
"version": 1,
35
"newProjectRoot": "projects",
46
"projects": {
57
"example-app": {
6-
"root": "",
8+
"root": "example-app",
79
"projectType": "application",
810
"architect": {
911
"build": {

example-app/tsconfig.app.json

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,21 @@
55
"moduleResolution": "node",
66
"emitDecoratorMetadata": true,
77
"experimentalDecorators": true,
8-
"lib": [
9-
"es2017",
10-
"dom"
11-
],
8+
"lib": ["es2017", "dom"],
129
"outDir": "../out-tsc/app",
1310
"target": "es5",
1411
"module": "es2015",
1512
"types": [],
1613
"baseUrl": ".",
1714
"rootDir": "../",
1815
"paths": {
19-
"@ngrx/effects": [
20-
"../modules/effects"
21-
],
22-
"@ngrx/store": [
23-
"../modules/store"
24-
],
25-
"@ngrx/store-devtools": [
26-
"../modules/store-devtools"
27-
],
28-
"@ngrx/router-store": [
29-
"../modules/router-store"
30-
],
31-
"@ngrx/entity": [
32-
"../modules/entity"
33-
]
16+
"@ngrx/effects": ["../modules/effects"],
17+
"@ngrx/store": ["../modules/store"],
18+
"@ngrx/store-devtools": ["../modules/store-devtools"],
19+
"@ngrx/router-store": ["../modules/router-store"],
20+
"@ngrx/entity": ["../modules/entity"],
21+
"@ngrx/schematics": ["../modules/schematics"]
3422
}
3523
},
36-
"exclude": [
37-
"../node_modules",
38-
"test.ts",
39-
"**/*.spec.ts"
40-
]
24+
"exclude": ["../node_modules", "test.ts", "**/*.spec.ts"]
4125
}

modules/schematics/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"homepage": "https://github.com/ngrx/platform#readme",
2323
"schematics": "./collection.json",
2424
"peerDependencies": {
25-
"@angular-devkit/core": "^0.4.0",
26-
"@angular-devkit/schematics": "^0.4.0"
25+
"@angular-devkit/core": "^0.5.0",
26+
"@angular-devkit/schematics": "^0.5.0"
2727
}
2828
}
Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
1+
import {
2+
SchematicTestRunner,
3+
UnitTestTree,
4+
} from '@angular-devkit/schematics/testing';
25
import * as path from 'path';
36
import { Schema as ActionOptions } from './schema';
7+
import {
8+
getProjectPath,
9+
createWorkspace,
10+
} from '../utility/test/create-workspace';
411

512
describe('Action Schematic', () => {
613
const schematicRunner = new SchematicTestRunner(
@@ -9,49 +16,70 @@ describe('Action Schematic', () => {
916
);
1017
const defaultOptions: ActionOptions = {
1118
name: 'foo',
12-
path: 'app',
13-
sourceDir: 'src',
19+
// path: 'app',
20+
project: 'bar',
1421
spec: false,
1522
group: false,
1623
flat: true,
1724
};
1825

26+
const projectPath = getProjectPath();
27+
28+
let appTree: UnitTestTree;
29+
30+
beforeEach(() => {
31+
appTree = createWorkspace(schematicRunner, appTree);
32+
});
33+
1934
it('should create one file', () => {
20-
const tree = schematicRunner.runSchematic('action', defaultOptions);
21-
expect(tree.files.length).toEqual(1);
22-
expect(tree.files[0]).toEqual('/src/app/foo.actions.ts');
35+
const tree = schematicRunner.runSchematic(
36+
'action',
37+
defaultOptions,
38+
appTree
39+
);
40+
expect(
41+
tree.files.indexOf(`${projectPath}/src/app/foo.actions.ts`)
42+
).toBeGreaterThanOrEqual(0);
2343
});
2444

2545
it('should create two files if spec is true', () => {
2646
const options = {
2747
...defaultOptions,
2848
spec: true,
2949
};
30-
const tree = schematicRunner.runSchematic('action', options);
31-
expect(tree.files.length).toEqual(2);
50+
const tree = schematicRunner.runSchematic('action', options, appTree);
3251
expect(
33-
tree.files.indexOf('/src/app/foo.actions.spec.ts')
52+
tree.files.indexOf(`${projectPath}/src/app/foo.actions.spec.ts`)
3453
).toBeGreaterThanOrEqual(0);
3554
expect(
36-
tree.files.indexOf('/src/app/foo.actions.ts')
55+
tree.files.indexOf(`${projectPath}/src/app/foo.actions.ts`)
3756
).toBeGreaterThanOrEqual(0);
3857
});
3958

4059
it('should create an enum named "Foo"', () => {
41-
const tree = schematicRunner.runSchematic('action', defaultOptions);
42-
const fileEntry = tree.get(tree.files[0]);
43-
if (fileEntry) {
44-
const fileContent = fileEntry.content.toString();
45-
expect(fileContent).toMatch(/export enum FooActionTypes/);
46-
}
60+
const tree = schematicRunner.runSchematic(
61+
'action',
62+
defaultOptions,
63+
appTree
64+
);
65+
const fileContent = tree.readContent(
66+
`${projectPath}/src/app/foo.actions.ts`
67+
);
68+
69+
expect(fileContent).toMatch(/export enum FooActionTypes/);
4770
});
4871

4972
it('should group within an "actions" folder if group is set', () => {
50-
const tree = schematicRunner.runSchematic('action', {
51-
...defaultOptions,
52-
group: true,
53-
});
54-
expect(tree.files.length).toEqual(1);
55-
expect(tree.files[0]).toEqual('/src/app/actions/foo.actions.ts');
73+
const tree = schematicRunner.runSchematic(
74+
'action',
75+
{
76+
...defaultOptions,
77+
group: true,
78+
},
79+
appTree
80+
);
81+
expect(
82+
tree.files.indexOf(`${projectPath}/src/app/actions/foo.actions.ts`)
83+
).toBeGreaterThanOrEqual(0);
5684
});
5785
});

modules/schematics/src/action/index.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,34 @@ import {
1111
noop,
1212
template,
1313
url,
14+
Tree,
15+
SchematicContext,
1416
} from '@angular-devkit/schematics';
1517
import * as stringUtils from '../strings';
1618
import { Schema as ActionOptions } from './schema';
19+
import { getProjectPath } from '../utility/project';
1720

1821
export default function(options: ActionOptions): Rule {
19-
options.path = options.path ? normalize(options.path) : options.path;
20-
const sourceDir = options.sourceDir;
21-
if (!sourceDir) {
22-
throw new SchematicsException(`sourceDir option is required.`);
23-
}
22+
return (host: Tree, context: SchematicContext) => {
23+
options.path = getProjectPath(host, options);
2424

25-
const templateSource = apply(url('./files'), [
26-
options.spec ? noop() : filter(path => !path.endsWith('__spec.ts')),
27-
template({
28-
'if-flat': (s: string) =>
29-
stringUtils.group(
30-
options.flat ? '' : s,
31-
options.group ? 'actions' : ''
32-
),
33-
...stringUtils,
34-
...(options as object),
35-
dot: () => '.',
36-
} as any),
37-
move(sourceDir),
38-
]);
25+
const templateSource = apply(url('./files'), [
26+
options.spec ? noop() : filter(path => !path.endsWith('__spec.ts')),
27+
template({
28+
'if-flat': (s: string) =>
29+
stringUtils.group(
30+
options.flat ? '' : s,
31+
options.group ? 'actions' : ''
32+
),
33+
...stringUtils,
34+
...(options as object),
35+
dot: () => '.',
36+
} as any),
37+
]);
3938

40-
return chain([branchAndMerge(chain([mergeWith(templateSource)]))]);
39+
return chain([branchAndMerge(chain([mergeWith(templateSource)]))])(
40+
host,
41+
context
42+
);
43+
};
4144
}
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
export interface Schema {
2+
/**
3+
* The name of the component.
4+
*/
5+
26
name: string;
3-
appRoot?: string;
7+
/**
8+
* The path to create the component.
9+
*/
10+
411
path?: string;
5-
sourceDir?: string;
12+
/**
13+
* The name of the project.
14+
*/
15+
project?: string;
616
/**
717
* Specifies if a spec file is generated.
818
*/
919
spec?: boolean;
20+
/**
21+
* Flag to indicate if a dir is created.
22+
*/
23+
1024
flat?: boolean;
25+
/**
26+
* Group actions file within 'actions' folder
27+
*/
28+
1129
group?: boolean;
1230
}

modules/schematics/src/action/schema.json

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@
55
"type": "object",
66
"properties": {
77
"name": {
8-
"type": "string"
9-
},
10-
"appRoot": {
8+
"description": "The name of the action.",
119
"type": "string"
1210
},
1311
"path": {
1412
"type": "string",
15-
"default": "app"
16-
},
17-
"sourceDir": {
18-
"type": "string",
19-
"default": "src"
13+
"format": "path",
14+
"description": "The path to create the component.",
15+
"visible": false
2016
},
2117
"spec": {
2218
"type": "boolean",
@@ -35,7 +31,5 @@
3531
"aliases": ["g"]
3632
}
3733
},
38-
"required": [
39-
"name"
40-
]
34+
"required": ["name"]
4135
}

modules/schematics/src/cli.spec.ts

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,35 @@ import {
44
UnitTestTree,
55
} from '@angular-devkit/schematics/testing';
66
import * as path from 'path';
7-
import { createAppModule, getFileContent } from './utility/test';
7+
import {
8+
createWorkspace,
9+
getProjectPath,
10+
} from './utility/test/create-workspace';
811

912
describe('CLI Schematic', () => {
1013
const schematicRunner = new SchematicTestRunner(
1114
'@ngrx/schematics',
1215
path.join(__dirname, '../collection.json')
1316
);
1417

15-
const workspaceOptions = {
16-
name: 'workspace',
17-
newProjectRoot: 'projects',
18-
version: '6.0.0',
19-
};
20-
21-
const appOptions = {
22-
name: 'bar',
23-
inlineStyle: false,
24-
inlineTemplate: false,
25-
viewEncapsulation: 'Emulated',
26-
routing: false,
27-
style: 'css',
28-
skipTests: false,
29-
};
30-
3118
const defaultOptions = {
3219
name: 'foo',
20+
project: 'bar',
3321
};
3422

23+
const projectPath = getProjectPath();
24+
3525
let appTree: UnitTestTree;
3626

3727
beforeEach(() => {
38-
appTree = schematicRunner.runExternalSchematic(
39-
'@schematics/angular',
40-
'workspace',
41-
workspaceOptions
42-
);
43-
appTree = schematicRunner.runExternalSchematic(
44-
'@schematics/angular',
45-
'application',
46-
appOptions,
47-
appTree
48-
);
28+
appTree = createWorkspace(schematicRunner, appTree);
4929
});
5030

5131
it('should create a class by the angular/cli', () => {
5232
const options = { ...defaultOptions, state: undefined };
5333
const tree = schematicRunner.runSchematic('class', options, appTree);
54-
const content = tree.readContent('/projects/bar/src/app/foo.ts');
34+
const content = tree.readContent(`${projectPath}/src/app/foo.ts`);
35+
5536
expect(content).toMatch(/export class Foo/);
5637
});
5738
});

0 commit comments

Comments
 (0)