Skip to content

Commit da1c955

Browse files
timdeschryverbrandonroberts
authored andcommitted
feat(schematics): add ng add support for @ngrx/entity (ngrx#1503)
1 parent d155d68 commit da1c955

File tree

7 files changed

+148
-0
lines changed

7 files changed

+148
-0
lines changed

modules/entity/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
"type": "git",
77
"url": "https://github.com/ngrx/platform.git"
88
},
9+
"keywords": [
10+
"Angular",
11+
"Redux",
12+
"Entity",
13+
"Schematics",
14+
"Angular CLI"
15+
],
916
"author": "NgRx",
1017
"license": "MIT",
1118
"bugs": {
@@ -17,6 +24,7 @@
1724
"@ngrx/store": "0.0.0-PLACEHOLDER",
1825
"rxjs": "RXJS_VERSION"
1926
},
27+
"schematics": "MODULE_SCHEMATICS_COLLECTION",
2028
"ng-update": {
2129
"packageGroup": "NG_UPDATE_PACKAGE_GROUP",
2230
"migrations": "NG_UPDATE_MIGRATIONS"

modules/entity/schematics/BUILD

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("//tools:defaults.bzl", "npm_package", "ts_library")
4+
5+
ts_library(
6+
name = "schematics",
7+
srcs = glob(
8+
[
9+
"**/*.ts",
10+
],
11+
exclude = [
12+
"**/*.spec.ts",
13+
"**/files/**/*",
14+
],
15+
),
16+
module_name = "@ngrx/entity/schematics",
17+
deps = [
18+
"//modules/entity/schematics-core",
19+
"@npm//@angular-devkit/schematics",
20+
"@npm//typescript",
21+
],
22+
)
23+
24+
npm_package(
25+
name = "npm_package",
26+
srcs = [
27+
":collection.json",
28+
] + glob([
29+
"**/files/**/*",
30+
"**/schema.json",
31+
]),
32+
deps = [
33+
":schematics",
34+
],
35+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"schematics": {
3+
"ng-add": {
4+
"aliases": ["init"],
5+
"factory": "./ng-add",
6+
"schema": "./ng-add/schema.json",
7+
"description": "Add @ngrx/entity to your application"
8+
}
9+
}
10+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {
2+
SchematicTestRunner,
3+
UnitTestTree,
4+
} from '@angular-devkit/schematics/testing';
5+
import * as path from 'path';
6+
import { Schema as EntityOptions } from './schema';
7+
import { createWorkspace } from '../../../schematics-core/testing';
8+
9+
describe('Entity ng-add Schematic', () => {
10+
const schematicRunner = new SchematicTestRunner(
11+
'@ngrx/entity',
12+
path.join(__dirname, '../collection.json')
13+
);
14+
const defaultOptions: EntityOptions = {
15+
skipPackageJson: false,
16+
};
17+
18+
let appTree: UnitTestTree;
19+
20+
beforeEach(() => {
21+
appTree = createWorkspace(schematicRunner, appTree);
22+
});
23+
24+
it('should update package.json', () => {
25+
const options = { ...defaultOptions };
26+
27+
const tree = schematicRunner.runSchematic('ng-add', options, appTree);
28+
const packageJson = JSON.parse(tree.readContent('/package.json'));
29+
30+
expect(packageJson.dependencies['@ngrx/entity']).toBeDefined();
31+
});
32+
33+
it('should skip package.json update', () => {
34+
const options = { ...defaultOptions, skipPackageJson: true };
35+
36+
const tree = schematicRunner.runSchematic('ng-add', options, appTree);
37+
const packageJson = JSON.parse(tree.readContent('/package.json'));
38+
39+
expect(packageJson.dependencies['@ngrx/entity']).toBeUndefined();
40+
});
41+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {
2+
Rule,
3+
SchematicContext,
4+
Tree,
5+
chain,
6+
noop,
7+
} from '@angular-devkit/schematics';
8+
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
9+
import {
10+
addPackageToPackageJson,
11+
platformVersion,
12+
} from '@ngrx/entity/schematics-core';
13+
import { Schema as EntityOptions } from './schema';
14+
15+
function addNgRxEntityToPackageJson() {
16+
return (host: Tree, context: SchematicContext) => {
17+
addPackageToPackageJson(
18+
host,
19+
'dependencies',
20+
'@ngrx/entity',
21+
platformVersion
22+
);
23+
context.addTask(new NodePackageInstallTask());
24+
return host;
25+
};
26+
}
27+
28+
export default function(options: EntityOptions): Rule {
29+
return (host: Tree, context: SchematicContext) => {
30+
return chain([
31+
options && options.skipPackageJson
32+
? noop()
33+
: addNgRxEntityToPackageJson(),
34+
])(host, context);
35+
};
36+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"$schema": "http://json-schema.org/schema",
3+
"id": "SchematicsNgRxEntity",
4+
"title": "NgRx Entity Schema",
5+
"type": "object",
6+
"properties": {
7+
"skipPackageJson": {
8+
"type": "boolean",
9+
"default": false,
10+
"description":
11+
"Do not add @ngrx/entity as dependency to package.json (e.g., --skipPackageJson)."
12+
}
13+
},
14+
"required": []
15+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface Schema {
2+
skipPackageJson?: boolean;
3+
}

0 commit comments

Comments
 (0)