Skip to content

Commit 01ff157

Browse files
itayodbrandonroberts
authored andcommitted
feat(schematics): Add ng-add support with prompt for making our schematics default (#1552)
1 parent 9d3597a commit 01ff157

File tree

5 files changed

+118
-8
lines changed

5 files changed

+118
-8
lines changed

modules/schematics/collection.json

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,59 @@
22
"extends": ["@schematics/angular"],
33
"schematics": {
44
"action": {
5-
"aliases": [ "a" ],
5+
"aliases": ["a"],
66
"factory": "./src/action",
77
"schema": "./src/action/schema.json",
88
"description": "Add store actions"
99
},
1010

1111
"container": {
12-
"aliases": [ "co" ],
12+
"aliases": ["co"],
1313
"factory": "./src/container",
1414
"schema": "./src/container/schema.json",
1515
"description": "Add store container component"
1616
},
1717

1818
"effect": {
19-
"aliases": [ "ef" ],
19+
"aliases": ["ef"],
2020
"factory": "./src/effect",
2121
"schema": "./src/effect/schema.json",
2222
"description": "Add side effect class"
2323
},
2424

2525
"entity": {
26-
"aliases": [ "en" ],
26+
"aliases": ["en"],
2727
"factory": "./src/entity",
2828
"schema": "./src/entity/schema.json",
2929
"description": "Add entity state"
3030
},
3131

3232
"feature": {
33-
"aliases": [ "f" ],
33+
"aliases": ["f"],
3434
"factory": "./src/feature",
3535
"schema": "./src/feature/schema.json",
3636
"description": "Add feature state"
3737
},
3838

3939
"reducer": {
40-
"aliases": [ "r" ],
40+
"aliases": ["r"],
4141
"factory": "./src/reducer",
4242
"schema": "./src/reducer/schema.json",
4343
"description": "Add state reducer"
4444
},
4545

4646
"store": {
47-
"aliases": [ "st" ],
47+
"aliases": ["st"],
4848
"factory": "./src/store",
4949
"schema": "./src/store/schema.json",
50-
"description": "Adds initial setup for state managment"
50+
"description": "Adds initial setup for state management"
51+
},
52+
53+
"ng-add": {
54+
"aliases": ["init"],
55+
"factory": "./src/ng-add",
56+
"schema": "./src/ng-add/schema.json",
57+
"description": "Installs the NgRx schematics package"
5158
}
5259
}
5360
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {
2+
SchematicTestRunner,
3+
UnitTestTree,
4+
} from '@angular-devkit/schematics/testing';
5+
import * as path from 'path';
6+
import { createWorkspace } from '../../../schematics-core/testing';
7+
import { Schema as SchematicOptions } from './schema';
8+
9+
describe('ng-add Schematic', () => {
10+
const schematicRunner = new SchematicTestRunner(
11+
'@ngrx/schematics',
12+
path.join(__dirname, '../../collection.json')
13+
);
14+
const defaultOptions: SchematicOptions = {
15+
defaultCollection: true,
16+
};
17+
18+
let appTree: UnitTestTree;
19+
20+
beforeEach(() => {
21+
appTree = createWorkspace(schematicRunner, appTree);
22+
});
23+
24+
it(`should leave the workspace's cli as default`, () => {
25+
const options: SchematicOptions = {
26+
...defaultOptions,
27+
defaultCollection: false,
28+
};
29+
30+
const tree = schematicRunner.runSchematic('ng-add', options, appTree);
31+
const workspace = JSON.parse(tree.readContent('/angular.json'));
32+
expect(workspace.cli).not.toBeDefined();
33+
});
34+
35+
it('should set workspace default cli to @ngrx/schematics', () => {
36+
const options: SchematicOptions = {
37+
...defaultOptions,
38+
defaultCollection: true,
39+
};
40+
41+
const tree = schematicRunner.runSchematic('ng-add', options, appTree);
42+
const workspace = JSON.parse(tree.readContent('/angular.json'));
43+
expect(workspace.cli.defaultCollection).toEqual('@ngrx/schematics');
44+
});
45+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {
2+
Rule,
3+
SchematicContext,
4+
Tree,
5+
chain,
6+
noop,
7+
} from '@angular-devkit/schematics';
8+
import {
9+
WorkspaceSchema,
10+
getWorkspacePath,
11+
getWorkspace,
12+
} from '../../schematics-core/utility/config';
13+
import { Schema as SchematicOptions } from './schema';
14+
15+
function updateWorkspace(host: Tree, key: keyof WorkspaceSchema, value: any) {
16+
const workspace = getWorkspace(host);
17+
const path = getWorkspacePath(host);
18+
workspace[key] = value;
19+
host.overwrite(path, JSON.stringify(workspace, null, 2));
20+
}
21+
22+
function setAsDefaultSchematics() {
23+
const cli = {
24+
defaultCollection: '@ngrx/schematics',
25+
};
26+
return (host: Tree) => {
27+
updateWorkspace(host, 'cli', cli);
28+
return host;
29+
};
30+
}
31+
32+
export default function(options: SchematicOptions): Rule {
33+
return (host: Tree, context: SchematicContext) => {
34+
return chain([
35+
options && options.defaultCollection ? setAsDefaultSchematics() : noop(),
36+
])(host, context);
37+
};
38+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"$schema": "http://json-schema.org/schema",
3+
"id": "SchematicsNgRxSchematics",
4+
"title": "Scaffolding library for Angular applications using NgRx libraries",
5+
"type": "object",
6+
"properties": {
7+
"defaultCollection": {
8+
"type": "boolean",
9+
"default": true,
10+
"description": "Use @ngrx/schematics as the default collection",
11+
"x-prompt":
12+
"Do you want to use @ngrx/schematics as the default collection?",
13+
"alias": "d"
14+
}
15+
},
16+
"required": []
17+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface Schema {
2+
defaultCollection?: boolean;
3+
}

0 commit comments

Comments
 (0)