Skip to content

Commit

Permalink
feat(schematics): add ng add support for @ngrx/entity (#1503)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver authored and brandonroberts committed Jan 7, 2019
1 parent d155d68 commit da1c955
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 0 deletions.
8 changes: 8 additions & 0 deletions modules/entity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
"type": "git",
"url": "https://github.com/ngrx/platform.git"
},
"keywords": [
"Angular",
"Redux",
"Entity",
"Schematics",
"Angular CLI"
],
"author": "NgRx",
"license": "MIT",
"bugs": {
Expand All @@ -17,6 +24,7 @@
"@ngrx/store": "0.0.0-PLACEHOLDER",
"rxjs": "RXJS_VERSION"
},
"schematics": "MODULE_SCHEMATICS_COLLECTION",
"ng-update": {
"packageGroup": "NG_UPDATE_PACKAGE_GROUP",
"migrations": "NG_UPDATE_MIGRATIONS"
Expand Down
35 changes: 35 additions & 0 deletions modules/entity/schematics/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package(default_visibility = ["//visibility:public"])

load("//tools:defaults.bzl", "npm_package", "ts_library")

ts_library(
name = "schematics",
srcs = glob(
[
"**/*.ts",
],
exclude = [
"**/*.spec.ts",
"**/files/**/*",
],
),
module_name = "@ngrx/entity/schematics",
deps = [
"//modules/entity/schematics-core",
"@npm//@angular-devkit/schematics",
"@npm//typescript",
],
)

npm_package(
name = "npm_package",
srcs = [
":collection.json",
] + glob([
"**/files/**/*",
"**/schema.json",
]),
deps = [
":schematics",
],
)
10 changes: 10 additions & 0 deletions modules/entity/schematics/collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"schematics": {
"ng-add": {
"aliases": ["init"],
"factory": "./ng-add",
"schema": "./ng-add/schema.json",
"description": "Add @ngrx/entity to your application"
}
}
}
41 changes: 41 additions & 0 deletions modules/entity/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
SchematicTestRunner,
UnitTestTree,
} from '@angular-devkit/schematics/testing';
import * as path from 'path';
import { Schema as EntityOptions } from './schema';
import { createWorkspace } from '../../../schematics-core/testing';

describe('Entity ng-add Schematic', () => {
const schematicRunner = new SchematicTestRunner(
'@ngrx/entity',
path.join(__dirname, '../collection.json')
);
const defaultOptions: EntityOptions = {
skipPackageJson: false,
};

let appTree: UnitTestTree;

beforeEach(() => {
appTree = createWorkspace(schematicRunner, appTree);
});

it('should update package.json', () => {
const options = { ...defaultOptions };

const tree = schematicRunner.runSchematic('ng-add', options, appTree);
const packageJson = JSON.parse(tree.readContent('/package.json'));

expect(packageJson.dependencies['@ngrx/entity']).toBeDefined();
});

it('should skip package.json update', () => {
const options = { ...defaultOptions, skipPackageJson: true };

const tree = schematicRunner.runSchematic('ng-add', options, appTree);
const packageJson = JSON.parse(tree.readContent('/package.json'));

expect(packageJson.dependencies['@ngrx/entity']).toBeUndefined();
});
});
36 changes: 36 additions & 0 deletions modules/entity/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
Rule,
SchematicContext,
Tree,
chain,
noop,
} from '@angular-devkit/schematics';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import {
addPackageToPackageJson,
platformVersion,
} from '@ngrx/entity/schematics-core';
import { Schema as EntityOptions } from './schema';

function addNgRxEntityToPackageJson() {
return (host: Tree, context: SchematicContext) => {
addPackageToPackageJson(
host,
'dependencies',
'@ngrx/entity',
platformVersion
);
context.addTask(new NodePackageInstallTask());
return host;
};
}

export default function(options: EntityOptions): Rule {
return (host: Tree, context: SchematicContext) => {
return chain([
options && options.skipPackageJson
? noop()
: addNgRxEntityToPackageJson(),
])(host, context);
};
}
15 changes: 15 additions & 0 deletions modules/entity/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "http://json-schema.org/schema",
"id": "SchematicsNgRxEntity",
"title": "NgRx Entity Schema",
"type": "object",
"properties": {
"skipPackageJson": {
"type": "boolean",
"default": false,
"description":
"Do not add @ngrx/entity as dependency to package.json (e.g., --skipPackageJson)."
}
},
"required": []
}
3 changes: 3 additions & 0 deletions modules/entity/schematics/ng-add/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface Schema {
skipPackageJson?: boolean;
}

0 comments on commit da1c955

Please sign in to comment.