Skip to content

Commit 71ade1f

Browse files
Merge pull request #6 from Adrael/master
Adding interactive mode and optional feature prefix
2 parents 7dbc4f1 + 44bc51a commit 71ade1f

File tree

5 files changed

+81
-30
lines changed

5 files changed

+81
-30
lines changed

libs/ddd/README.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Add this plugin to an Nx workspace:
2323
ng add @angular-architects/ddd
2424
```
2525

26-
Add domains and features:
26+
Add domains and features manually:
2727

2828
```
2929
ng g @angular-architects/ddd:domain booking
@@ -33,6 +33,42 @@ ng g @angular-architects/ddd:feature cancel --domain booking --app flight-app
3333
ng g @angular-architects/ddd:feature manage --domain boarding --app flight-app
3434
```
3535

36+
Add domains and features interactively:
37+
38+
```
39+
ng g @angular-architects/ddd:domain
40+
> ? What is the name of the domain? booking
41+
> ? Would you like to add an associated application? (y/N) No
42+
43+
ng g @angular-architects/ddd:domain
44+
> ? What is the name of the domain? boarding
45+
> ? Would you like to add an associated application? (y/N) No
46+
47+
ng g @angular-architects/ddd:feature
48+
> ? What is the name of the library? search
49+
> ? What is the name of the associated domain? booking
50+
> ? Would you like to add the "feature-" prefix? (Y/n) Yes
51+
> ? Is this feature lazy loaded? (y/N) No
52+
> [Optional] What is the associated application? (Leave blank if none) flight-app
53+
> [Optional] What is the name of the entity to create for this feature? (Leave blank if none) flight
54+
55+
ng g @angular-architects/ddd:feature
56+
> ? What is the name of the library? cancel
57+
> ? What is the name of the associated domain? booking
58+
> ? Would you like to add the "feature-" prefix? (Y/n) Yes
59+
> ? Is this feature lazy loaded? (y/N) No
60+
> [Optional] What is the associated application? (Leave blank if none) flight-app
61+
> [Optional] What is the name of the entity to create for this feature? (Leave blank if none)
62+
63+
ng g @angular-architects/ddd:feature
64+
> ? What is the name of the library? manage
65+
> ? What is the name of the associated domain? boarding
66+
> ? Would you like to add the "feature-" prefix? (Y/n) Yes
67+
> ? Is this feature lazy loaded? (y/N) No
68+
> [Optional] What is the associated application? (Leave blank if none) flight-app
69+
> [Optional] What is the name of the entity to create for this feature? (Leave blank if none)
70+
```
71+
3672
This example assumes that you have an app ``flight-app`` in place.
3773

3874
These schematics also wire up the individual libs. To see the result, create a dependency graph:
@@ -86,4 +122,4 @@ see https://github.com/angular-architects/ddd-demo
86122
- [Recording of session about this architecture](https://www.youtube.com/watch?v=94HFD391zkE&t=1s)
87123
- [Article series about DDD with Angular](https://www.softwarearchitekt.at/aktuelles/sustainable-angular-architectures-1/)
88124
- [Our eBook about this architecture](https://leanpub.com/enterprise-angular)
89-
- [Thomas Burlison's article about facades in Angular](https://medium.com/@thomasburlesonIA/push-based-architectures-with-rxjs-81b327d7c32d)
125+
- [Thomas Burlison's article about facades in Angular](https://medium.com/@thomasburlesonIA/push-based-architectures-with-rxjs-81b327d7c32d)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"name": {
77
"type": "string",
88
"description": "Domain name",
9+
"x-prompt": "What is the name of the domain?",
910
"$default": {
1011
"$source": "argv",
1112
"index": 0
@@ -14,6 +15,7 @@
1415
"addApp": {
1516
"type": "boolean",
1617
"description": "Add an app for the domain?",
18+
"x-prompt": "Would you like to add an associated application?",
1719
"default": false
1820
},
1921
"type": {

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

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ function readIntoSourceFile(host: Tree, modulePath: string): ts.SourceFile {
2121
}
2222

2323
function addImport(
24-
modulePath: string,
25-
ngModuleToImportPath: string,
26-
ngModuleToImportName: string,
24+
modulePath: string,
25+
ngModuleToImportPath: string,
26+
ngModuleToImportName: string,
2727
optional = false): Rule {
28-
28+
2929
return (host: Tree) => {
3030

3131
if (optional && !host.exists(modulePath)) {
@@ -35,9 +35,9 @@ function addImport(
3535
const source = readIntoSourceFile(host, modulePath);
3636

3737
const changes = addImportToModule(
38-
source,
39-
modulePath,
40-
ngModuleToImportName,
38+
source,
39+
modulePath,
40+
ngModuleToImportName,
4141
ngModuleToImportPath)
4242

4343
const declarationRecorder = host.beginUpdate(modulePath);
@@ -51,16 +51,16 @@ function addImport(
5151
}
5252

5353
function addDeclaration(
54-
modulePath: string,
55-
componentToImportPath: string,
54+
modulePath: string,
55+
componentToImportPath: string,
5656
componentToImportName: string): Rule {
57-
57+
5858
return (host: Tree) => {
5959

6060
const source = readIntoSourceFile(host, modulePath);
6161

6262
const changes = addDeclarationToModule(
63-
source,
63+
source,
6464
modulePath,
6565
componentToImportName,
6666
componentToImportPath);
@@ -76,16 +76,16 @@ function addDeclaration(
7676
}
7777

7878
function addExport(
79-
modulePath: string,
80-
componentToImportPath: string,
79+
modulePath: string,
80+
componentToImportPath: string,
8181
componentToImportName: string): Rule {
82-
82+
8383
return (host: Tree) => {
8484

8585
const source = readIntoSourceFile(host, modulePath);
8686

8787
const changes = addExportToModule(
88-
source,
88+
source,
8989
modulePath,
9090
componentToImportName,
9191
componentToImportPath);
@@ -103,7 +103,7 @@ function addExport(
103103
function addTsExport(filePath: string, filesToExport: string[]): Rule {
104104
return (host: Tree) => {
105105
let content = host.read(filePath) + '\n';
106-
106+
107107
for(const file of filesToExport) {
108108
content += `export * from '${file}';\n`;
109109
}
@@ -131,24 +131,22 @@ export default function(options: FeatureOptions): Rule {
131131

132132
const workspaceName = readWorkspaceName(host);
133133

134-
const domainName = strings.dasherize(options.domain);
135-
const domainFolderName = domainName;
134+
const domainFolderName = strings.dasherize(options.domain);
136135
const domainPath = `libs/${domainFolderName}/domain/src/lib`;
137-
const domainModulePath = `${domainPath}/${domainFolderName}-domain.module.ts`;
138136
const domainModuleClassName = strings.classify(options.domain) + "DomainModule";
139137
const domainImportPath = `${workspaceName}/${domainFolderName}/domain`;
140138
const domainIndexPath = `libs/${domainFolderName}/domain/src/index.ts`;
141139

142140
const featureName = strings.dasherize(options.name);
143-
const featureFolderName = 'feature-' + featureName;
141+
const featureFolderName = (options.prefix ? 'feature-' : '') + featureName;
144142
const featurePath = `libs/${domainFolderName}/${featureFolderName}/src/lib`;
145143
const featureModulePath = `${featurePath}/${domainFolderName}-${featureFolderName}.module.ts`;
146144
const featureModuleClassName = strings.classify(`${options.domain}-${featureFolderName}Module`);
147145
const featureImportPath = `${workspaceName}/${domainFolderName}/${featureFolderName}`;
148146
const featureIndexPath = `libs/${domainFolderName}/${featureFolderName}/src/index.ts`;
149147

150148
const entityName = options.entity ? strings.dasherize(options.entity) : '';
151-
149+
152150
const featureComponentImportPath = `./${featureName}.component`;
153151
const featureComponentClassName = strings.classify(`${featureName}Component`);
154152

@@ -185,14 +183,14 @@ export default function(options: FeatureOptions): Rule {
185183
buildable: options.type === 'buildable',
186184
}),
187185
addImport(featureModulePath, domainImportPath, domainModuleClassName),
188-
(!options.lazy && host.exists(appModulePath)) ?
186+
(!options.lazy && host.exists(appModulePath)) ?
189187
chain([
190188
addImport(appModulePath, featureImportPath, featureModuleClassName, true),
191189
addImport(appModulePath, '@angular/common/http', 'HttpClientModule', true)
192190
]) :
193191
noop(),
194192
mergeWith(domainTemplates),
195-
(options.entity) ?
193+
(options.entity) ?
196194
addTsExport(domainIndexPath, [
197195
`./lib/entities/${entityName}`,
198196
`./lib/infrastructure/${entityName}.data.service`

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,37 @@
66
"name": {
77
"type": "string",
88
"description": "Library name",
9+
"x-prompt": "What is the name of the library?",
910
"$default": {
1011
"$source": "argv",
1112
"index": 0
1213
}
1314
},
1415
"domain": {
1516
"type": "string",
17+
"x-prompt": "What is the name of the associated domain?",
1618
"description": "Domain name"
1719
},
18-
"app": {
19-
"type": "string",
20-
"description": "app name"
20+
"prefix": {
21+
"type": "boolean",
22+
"description": "Apply the \"feature-\" prefix?",
23+
"x-prompt": "Would you like to add the \"feature-\" prefix?",
24+
"default": true
2125
},
2226
"lazy": {
2327
"type": "boolean",
2428
"description": "Is this feature module lazy loaded?",
29+
"x-prompt": "Is this feature lazy loaded?",
2530
"default": false
2631
},
32+
"app": {
33+
"type": "string",
34+
"x-prompt": "[Optional] What is the associated application? (Leave blank if none)",
35+
"description": "app name"
36+
},
2737
"entity": {
2838
"type": "string",
39+
"x-prompt": "[Optional] What is the name of the entity to create for this feature? (Leave blank if none)",
2940
"description": "Optional entity to create for this feature"
3041
},
3142
"type": {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ export interface FeatureOptions {
1515
*/
1616
domain: string;
1717
/**
18-
* app name
18+
* Apply the "feature-" prefix?
1919
*/
20-
app?: string;
20+
prefix?: boolean;
2121
/**
2222
* Is this feature module lazy loaded?
2323
*/
2424
lazy?: boolean;
25+
/**
26+
* app name
27+
*/
28+
app?: string;
2529
/**
2630
* Optional entity to create for this feature
2731
*/

0 commit comments

Comments
 (0)