Skip to content

Commit 1434f50

Browse files
author
Peter Smith
committed
feat(domain): add ngrx
1 parent 74e2e2d commit 1434f50

File tree

6 files changed

+62
-11
lines changed

6 files changed

+62
-11
lines changed

libs/ddd/src/schematics/domain/index.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,23 @@ import {
1212

1313
import { strings } from '@angular-devkit/core';
1414
import { DomainOptions } from './schema';
15-
import { addDomainToLintingRules } from '../rules';
15+
import {
16+
addDomainToLintingRules,
17+
addNgrxImportsToApp,
18+
addNgRxToPackageJson,
19+
} from '../rules';
1620

1721
export default function (options: DomainOptions): Rule {
1822
const libFolder = strings.dasherize(options.name);
1923

20-
const templateSource = options.ngrx
21-
? apply(url('./ngrx-files'), [
22-
template({}),
23-
move(`libs/${libFolder}/domain/src/lib`),
24-
])
25-
: apply(url('./files'), [
26-
template({}),
27-
move(`libs/${libFolder}/domain/src/lib`),
28-
]);
24+
const templateSource = apply(url('./files'), [
25+
template({}),
26+
move(`libs/${libFolder}/domain/src/lib`),
27+
]);
28+
29+
const appFolderName = strings.dasherize(options.name);
30+
const appPath = `apps/${appFolderName}/src/app`;
31+
const appModulePath = `${appPath}/app.module.ts`;
2932

3033
return chain([
3134
externalSchematic('@nrwl/angular', 'lib', {
@@ -46,5 +49,18 @@ export default function (options: DomainOptions): Rule {
4649
tags: `domain:${options.name},type:app`,
4750
style: 'scss',
4851
}),
52+
options.addApp && options.ngrx
53+
? chain([
54+
externalSchematic('@ngrx/schematics', 'store', {
55+
project: options.name,
56+
root: true,
57+
minimal: true,
58+
module: 'app.module.ts',
59+
name: 'state',
60+
}),
61+
addNgrxImportsToApp(appModulePath),
62+
addNgRxToPackageJson(),
63+
])
64+
: noop(),
4965
]);
5066
}

libs/ddd/src/schematics/domain/schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
"x-prompt": "Would you like to add an associated application?",
1919
"default": false
2020
},
21+
"ngrx": {
22+
"type": "boolean",
23+
"default": false,
24+
"description": "Add ngrx for the associated app (addApp required)",
25+
"x-prompt": "Would you like to add NgRx to your associated application?"
26+
},
2127
"type": {
2228
"type": "string",
2329
"enum": ["internal", "buildable", "publishable"],

libs/ddd/src/schematics/domain/schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export interface DomainOptions {
1414
* Add an app for the domain?
1515
*/
1616
addApp?: boolean;
17+
/**
18+
* Add ngrx for the associated app (addApp required)
19+
*/
20+
ngrx?: boolean;
1721
/**
1822
* A type to determine if and how to build the library.
1923
*/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Tree, Rule } from '@angular-devkit/schematics';
2+
import { addImportToModule } from '@schematics/angular/utility/ast-utils';
3+
import { readIntoSourceFile, insert } from '../utils';
4+
5+
/**
6+
* addNgrxImportsToApp
7+
* @param modulePath the path of the moudule the import is being included in
8+
*/
9+
export function addNgrxImportsToApp(modulePath: string): Rule {
10+
return (host: Tree) => {
11+
const effectsForRoot = `EffectsModule.forRoot()`;
12+
13+
if (!host.exists(modulePath)) {
14+
return;
15+
}
16+
17+
const source = readIntoSourceFile(host, modulePath);
18+
19+
insert(host, modulePath, [
20+
...addImportToModule(source, modulePath, effectsForRoot, '@ngrx/effects'),
21+
]);
22+
};
23+
}

libs/ddd/src/schematics/rules/add-ngrx-to-package-json.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export function addNgRxToPackageJson(): Rule {
1515
{
1616
'@ngrx/store': ngrxVersion,
1717
'@ngrx/effects': ngrxVersion,
18-
'@ngrx/entity': ngrxVersion
18+
'@ngrx/entity': ngrxVersion,
19+
'@ngrx/store-devtools': ngrxVersion,
1920
},
2021
{}
2122
);

libs/ddd/src/schematics/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export * from './add-ts-exports';
66
export * from './filter-templates';
77
export * from './init-linting-rules';
88
export * from './add-ngrx-imports-to-domain';
9+
export * from './add-ngrx-imports-to-app';
910
export * from './add-ngrx-to-package-json';

0 commit comments

Comments
 (0)