Skip to content

Commit d68662b

Browse files
authored
feat/labs-6-accept-options (#28)
1. include `d.ts` files 1. configure schematic to accept options - argv[0] - entity-name - path - init - initialize with NgRx files `ng g @briebug/ngrx-entity-schematic:ngrx-entity-schematic customers --init --path src/app/customers-state` or locally `yarn launch customers --init --path src/app/customers-state`
1 parent 1b0cfd3 commit d68662b

File tree

10 files changed

+119
-22
lines changed

10 files changed

+119
-22
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ Thumbs.db
4141
src/**/*.js
4242
!src/**/__files__/**/*.js
4343
src/**/*.js.map
44-
src/**/*.d.ts
44+
src/**.d.ts

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{
88
"type": "node",
99
"request": "attach",
10-
"name": "Attach Node",
10+
"name": "Attach Schematic",
1111
"port": 9229,
1212
"restart": false,
1313
"protocol": "inspector"

README.md

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
# NgRx Entity Generator
22

3-
## Usage
3+
## How to Use
4+
5+
### Run schematic
6+
7+
Generate Entity files
8+
9+
```shell
10+
ng g @briebug/ngrx-entity-schematic:ngrx-entity-schematic ENTITY_NAME
11+
```
12+
13+
Generate Entity files at a specific relative path
14+
15+
```shell
16+
ng g @briebug/ngrx-entity-schematic:ngrx-entity-schematic ENTITY_NAME --path RELATIVE/PATH
17+
```
18+
19+
Generate Entity files with NgRx setup files
20+
21+
```shell
22+
ng g @briebug/ngrx-entity-schematic:ngrx-entity-schematic ENTITY_NAME --init
23+
```
24+
25+
- `ENTITY_NAME`, `--path`, and `--init` flags can be used together.
26+
- `ENTITY_NAME` is **required** as the first argument after the schematic name
427

528
## Development
629

@@ -12,10 +35,10 @@ yarn link:schematic
1235

1336
### Run schematic locally
1437

15-
Compile the schematic code
38+
You can run or re-run schematic against `sandbox-app` and pass options
1639

1740
```shell
18-
yarn build:schematic
41+
yarn launch ENTITY_NAME --init
1942
```
2043

2144
Reset the `sandbox-app` to it's version controlled state, then execute the schematic locally against the `sandbox-app`.
@@ -24,6 +47,12 @@ Reset the `sandbox-app` to it's version controlled state, then execute the schem
2447
yarn clean:launch
2548
```
2649

50+
Compile the schematic code if changes have been made to `./src/*`
51+
52+
```shell
53+
yarn build:schematic
54+
```
55+
2756
### Copy `schematic-src` files into schematic template files
2857

2958
Read the blueprint app, `/schematic-src`, and copy all the necessary files into the schematic folder, `/src/__files__`. During the copy, replace all "entity" placeholders with their respective template placeholder. Run this if changes to the `schematic-src` app have been made.
@@ -41,19 +70,19 @@ yarn build:run:fileBuilder
4170

4271
#### About
4372

44-
This schematic uses a template project that's the working blueprint for what the schematic will eventually generate. This pattern was chosen to provide a faster and easier development cycle when testing the template app as a standalone application with the typical dev feedback provided by the Angular CLI. The alternative would involve developing against the template files (which include template variable like `<%= classify(name) %>`) and having to run the schematic locally on every change.
73+
This schematic uses a template project that acts as the working blueprint for what the schematic will eventually generate. This pattern was chosen to provide a faster and easier development cycle when testing the blueprint app as a standalone application providing the typical dev feedback by the Angular CLI and other devtools. The alternative would involve developing against the template files (which include template variables like `<%= classify(name) %>`) and having to run the schematic locally on every change.
4574

4675
### Developing the `buildFiles` script
4776

48-
When editing the script that copies and modifies the blueprint files into the schematic directory. The following commands all for quick dev feedback and debugging.
77+
This script copies and modifies the blueprint Entity files into the schematic `__files__` directory. The following commands allow for quick dev feedback and debugging.
4978

5079
Compile the `buildFiles` script into `/tmp` in one shell
5180

5281
```shell
5382
build:fileBuilder
5483
```
5584

56-
In another shell, run the script and watch the `/tmp` dir for changes. Allows for attaching a debugger on port `9222`. See the attached `.vscode/launch.json` file for debugging with VSCode.
85+
In another shell, run the following script which will watch the `/tmp` dir for changes. Allows for attaching a debugger on port `9222`. See the attached `.vscode/launch.json` file for debugging with VSCode. Run the `Attach Schematic` debugger.
5786

5887
```shell
5988
run:fileBuilder
@@ -65,12 +94,12 @@ Once both of these are run, changes to `./buildFiles.ts` should recompile and tr
6594

6695
### ./src
6796

68-
This is the schematic code that's executed when running `ng g @briebug/ngrx-entity-schematic`.
97+
This is the schematic code that's executed when running `ng g @briebug/ngrx-entity-schematic:ngrx-entity-schematic`.
6998

7099
### ./schematic-src
71100

72-
This the blueprint used for generating the schematic, specifically in template files in `./src/ngrx-entity/__files__`. This is a working application that defines the final form of the schematic.
101+
This is the blueprint app used for generating the schematic, specifically the template files in `./src/ngrx-entity/__files__`. This is a working application that defines the final form of the schematic.
73102

74103
### ./sandbox-app
75104

76-
This is an application that's used for testing the schematic locally for development.
105+
This is an application that's used for testing the schematic locally during development.

src/ngrx-entity/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Rule } from '@angular-devkit/schematics';
2+
import { NgRxOptions } from './utility/util';
3+
export default function (options: NgRxOptions): Rule;

src/ngrx-entity/index.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
template,
1111
filter,
1212
noop,
13+
SchematicsException,
1314
} from '@angular-devkit/schematics';
1415
import { NgRxOptions } from './utility/util';
1516
import { parseName } from './utility/parseName';
@@ -22,19 +23,27 @@ export default function(options: NgRxOptions): Rule {
2223

2324
function addNgRxFiles(options: NgRxOptions): Rule {
2425
return (tree: Tree, context: SchematicContext) => {
25-
const path = './src/app/state'; // read path from where uses runs schematic?
26-
const entityName = 'orders';
27-
context.logger.debug(`adding NgRX files to ${path} dir`);
26+
if (!options.name) {
27+
throw new SchematicsException('Entity name is required');
28+
}
2829

29-
const parsedPath = parseName(path, entityName);
30+
if (!options.path) {
31+
// todo: determine default based on current working dir
32+
options.path = `./src/app/state`;
33+
console.log(`No Entity path specified, adding files to ${options.path}`);
34+
}
35+
36+
context.logger.debug(`adding NgRX files to ${options.path} dir`);
37+
38+
const parsedPath = parseName(options.path, options.name);
3039
options.name = parsedPath.name;
3140
options.path = parsedPath.path;
3241

3342
const templateSource = apply(url('./__files__'), [
34-
tree.exists(`${parsedPath.path}/app.interfaces.ts`) ? filter(path => !path.endsWith('app.interfaces.ts')) : noop(),
35-
tree.exists(`${parsedPath.path}/app.reducer.ts`) ? filter(path => !path.endsWith('app.reducer.ts')) : noop(),
36-
tree.exists(`${parsedPath.path}/state-utils.ts`) ? filter(path => !path.endsWith('state-utils.ts')) : noop(),
37-
tree.exists(`${parsedPath.path}/state.module.ts`) ? filter(path => !path.endsWith('state.module.ts')) : noop(),
43+
options.init && !tree.exists(`${parsedPath.path}/app.interfaces.ts`) ? noop() : filter(path => !path.endsWith('app.interfaces.ts')) ,
44+
options.init && !tree.exists(`${parsedPath.path}/app.reducer.ts`) ? noop() : filter(path => !path.endsWith('app.reducer.ts')) ,
45+
options.init && !tree.exists(`${parsedPath.path}/state-utils.ts`) ? noop() : filter(path => !path.endsWith('state-utils.ts')) ,
46+
options.init && !tree.exists(`${parsedPath.path}/state.module.ts`) ? noop() : filter(path => !path.endsWith('state.module.ts')) ,
3847
template({
3948
...strings,
4049
'if-flat': (s: string) => (options.flat ? '' : s),

src/ngrx-entity/schema.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export interface Schema {
2+
/**
3+
* The name of the Entity.
4+
*/
5+
name: string;
6+
/**
7+
* The path to create the entity files.
8+
*/
9+
path?: string;
10+
/**
11+
* Should setup NgRx.
12+
*/
13+
init?: boolean;
14+
}

src/ngrx-entity/schema.json

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,27 @@
33
"id": "BriebugSchematicsNgRxEntity",
44
"title": "NgRx Generator Options Schema",
55
"type": "object",
6-
"properties": {},
6+
"properties": {
7+
"name": {
8+
"type": "string",
9+
"description": "The name of the component.",
10+
"$default": {
11+
"$source": "argv",
12+
"index": 0
13+
}
14+
},
15+
"path": {
16+
"type": "string",
17+
"format": "path",
18+
"description": "The path to create the entity files",
19+
"visible": false,
20+
"alias": "p"
21+
},
22+
"init": {
23+
"type": "boolean",
24+
"description": "Flag to setup NgRx.",
25+
"default": false
26+
}
27+
},
728
"required": []
829
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
* source: https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/utility/parse-name.ts
8+
*/
9+
import { Path } from '@angular-devkit/core';
10+
export interface Location {
11+
name: string;
12+
path: Path;
13+
}
14+
export declare function parseName(path: string, name: string): Location;

src/ngrx-entity/utility/util.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface NgRxOptions {
2+
name: string;
3+
path?: string;
4+
init?: boolean;
5+
flat?: boolean;
6+
}

src/ngrx-entity/utility/util.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface NgRxOptions {
2-
flat?: boolean;
3-
name?: string;
2+
name: string;
43
path?: string;
4+
init?: boolean;
5+
flat?: boolean;
56
}

0 commit comments

Comments
 (0)