Skip to content

Commit 257704c

Browse files
author
Peter Smith
committed
feat(feature): ngrx import and install deps
1 parent 14eccac commit 257704c

File tree

7 files changed

+152
-16
lines changed

7 files changed

+152
-16
lines changed

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

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {
1818
addImport,
1919
addTsExport,
2020
filterTemplates,
21+
addNgrxImportsToDomain,
22+
addNgRxToPackageJson,
2123
} from '../rules';
2224
import { readWorkspaceName } from '../utils';
2325

@@ -27,6 +29,7 @@ export default function (options: FeatureOptions): Rule {
2729

2830
const domainFolderName = strings.dasherize(options.domain);
2931
const domainPath = `libs/${domainFolderName}/domain/src/lib`;
32+
const domainModulePath = `${domainPath}/${domainFolderName}-domain.module.ts`;
3033
const domainModuleClassName =
3134
strings.classify(options.domain) + 'DomainModule';
3235
const domainImportPath = `${workspaceName}/${domainFolderName}/domain`;
@@ -62,22 +65,30 @@ export default function (options: FeatureOptions): Rule {
6265
}
6366
}
6467

65-
const domainTemplates = options.ngrx
66-
? apply(url('./files/forDomainWithNgrx'), [
67-
filterTemplates(options),
68-
template({ ...strings, ...options, workspaceName }),
69-
move(domainPath),
70-
])
71-
: apply(url('./files/forDomain'), [
72-
filterTemplates(options),
73-
template({ ...strings, ...options, workspaceName }),
74-
move(domainPath),
75-
]);
68+
const domainTemplates =
69+
options.ngrx && options.entity
70+
? apply(url('./files/forDomainWithNgrx'), [
71+
filterTemplates(options),
72+
template({ ...strings, ...options, workspaceName }),
73+
move(domainPath),
74+
])
75+
: apply(url('./files/forDomain'), [
76+
filterTemplates(options),
77+
template({ ...strings, ...options, workspaceName }),
78+
move(domainPath),
79+
]);
7680

77-
const featureTemplates = apply(url('./files/forFeature'), [
78-
template({ ...strings, ...options, workspaceName }),
79-
move(featurePath),
80-
]);
81+
const featureTemplates =
82+
options.ngrx && options.entity
83+
? apply(url('./files/forFeatureWithNgrx'), [
84+
filterTemplates(options),
85+
template({ ...strings, ...options, workspaceName }),
86+
move(featurePath),
87+
])
88+
: apply(url('./files/forFeature'), [
89+
template({ ...strings, ...options, workspaceName }),
90+
move(featurePath),
91+
]);
8192

8293
return chain([
8394
externalSchematic('@nrwl/angular', 'lib', {
@@ -113,6 +124,15 @@ export default function (options: FeatureOptions): Rule {
113124
`./lib/infrastructure/${entityName}.data.service`,
114125
])
115126
: noop(),
127+
options.ngrx && options.entity && host.exists(domainModulePath)
128+
? chain([
129+
addNgRxToPackageJson(),
130+
addNgrxImportsToDomain(domainModulePath, entityName),
131+
addTsExport(domainIndexPath, [
132+
`./lib/+state/${entityName}/${entityName}.actions`,
133+
]),
134+
])
135+
: noop(),
116136
addTsExport(domainIndexPath, [`./lib/application/${featureName}.facade`]),
117137
mergeWith(featureTemplates),
118138
addTsExport(featureIndexPath, [`./lib/${featureName}.component`]),

libs/ddd/src/schematics/rules/add-import.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { readIntoSourceFile } from '../utils';
55

66
/**
77
* addImport
8-
* @param modulePath the path of the moudule the import is being included in
8+
* @param modulePath the path of the module the import is being included in
99
* @param ngModuleToImportPath the path of angular module that is being imported.
1010
* @param ngModuleToImportName the name of the angular module that is being imported.
1111
* @param optional whether or not optional
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Tree, Rule } from '@angular-devkit/schematics';
2+
import { classify, dasherize } from '@nrwl/workspace/src/utils/strings';
3+
import {
4+
addImportToModule,
5+
insertImport,
6+
} from '@schematics/angular/utility/ast-utils';
7+
import { readIntoSourceFile, insert } from '../utils';
8+
9+
/**
10+
* addNgrxImportsToDomain
11+
* @param modulePath the path of the moudule the import is being included in
12+
* @param entityName the name of the entity
13+
*/
14+
export function addNgrxImportsToDomain(
15+
modulePath: string,
16+
entityName: string
17+
): Rule {
18+
return (host: Tree) => {
19+
const pathPrefix = `./+state/${dasherize(entityName)}/${dasherize(
20+
entityName
21+
)}`;
22+
const reducerPath = `${pathPrefix}.reducer`;
23+
const reducerImports = `* as from${classify(entityName)}`;
24+
const effectsName = `${classify(entityName)}Effects`;
25+
const effectsPath = `${pathPrefix}.effects`;
26+
const effectsImports = `{ ${effectsName} }`;
27+
28+
const classfiedEntityName = classify(entityName);
29+
const fromFeature = `from${classfiedEntityName}`;
30+
const storeForFeature = `StoreModule.forFeature(${fromFeature}.${classfiedEntityName.toUpperCase()}_FEATURE_KEY, ${fromFeature}.reducer)`;
31+
const effectsForFeature = `EffectsModule.forFeature([${effectsName}])`;
32+
33+
if (!host.exists(modulePath)) {
34+
return;
35+
}
36+
37+
const source = readIntoSourceFile(host, modulePath);
38+
39+
insert(host, modulePath, [
40+
insertImport(source, modulePath, effectsImports, effectsPath, true),
41+
insertImport(source, modulePath, reducerImports, reducerPath, true),
42+
...addImportToModule(source, modulePath, storeForFeature, '@ngrx/store'),
43+
...addImportToModule(
44+
source,
45+
modulePath,
46+
effectsForFeature,
47+
'@ngrx/effects'
48+
),
49+
]);
50+
};
51+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* credit: https://github.com/nrwl/nx/tree/master/packages/angular/src/schematics/ngrx/rules
3+
*/
4+
import { Rule } from '@angular-devkit/schematics';
5+
import { addDepsToPackageJson } from '@nrwl/workspace';
6+
7+
/**
8+
* addNgRxToPackageJson
9+
* add the ngrx packages to the package.json and install them
10+
*/
11+
export function addNgRxToPackageJson(): Rule {
12+
const ngrxVersion = '10.0.0';
13+
14+
return addDepsToPackageJson(
15+
{
16+
'@ngrx/store': ngrxVersion,
17+
'@ngrx/effects': ngrxVersion,
18+
'@ngrx/entity': ngrxVersion
19+
},
20+
{}
21+
);
22+
}
23+
24+
/**
25+
* TODO: reconcile the assumption of forRoot
26+
*/

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ export * from './add-import';
55
export * from './add-ts-exports';
66
export * from './filter-templates';
77
export * from './init-linting-rules';
8+
export * from './add-ngrx-imports-to-domain';
9+
export * from './add-ngrx-to-package-json';

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './check-rule-exists';
33
export * from './read-workspace-name';
44
export * from './validate-inputs';
55
export * from './update-dep-const';
6+
export * from './insert';
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* credit: https://github.com/nrwl/nx/blob/master/packages/workspace/src/utils/ast-utils.ts
3+
* we use this instead of @nrwl/workspace version of import because the nrwl/workspace uses
4+
* an internal version of Change and we are using the @schematics/angular/utility/ast-utils'
5+
* version
6+
*/
7+
import { Tree } from '@angular-devkit/schematics';
8+
import { RemoveChange } from '@nrwl/workspace';
9+
import { Change, InsertChange } from '@schematics/angular/utility/change';
10+
11+
/**
12+
* insert
13+
* @param host Host
14+
* @param modulePath path to module
15+
* @param changes array of changes
16+
*/
17+
export function insert(host: Tree, modulePath: string, changes: Change[]) {
18+
if (changes.length < 1) {
19+
return;
20+
}
21+
22+
// sort changes so that the highest pos goes first
23+
const orderedChanges = changes.sort((a, b) => b.order - a.order);
24+
25+
const recorder = host.beginUpdate(modulePath);
26+
for (const change of orderedChanges) {
27+
if (change instanceof InsertChange) {
28+
recorder.insertLeft(change.pos, change.toAdd);
29+
} else if (change instanceof RemoveChange) {
30+
recorder.remove(change.pos - 1, change.toRemove.length + 1);
31+
} else {
32+
throw new Error(`Unexpected Change '${change.constructor.name}'`);
33+
}
34+
}
35+
host.commitUpdate(recorder);
36+
}

0 commit comments

Comments
 (0)