-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schematics): add ng add support for @ngrx/entity (#1503)
- Loading branch information
1 parent
d155d68
commit da1c955
Showing
7 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface Schema { | ||
skipPackageJson?: boolean; | ||
} |