Skip to content

Commit d57b4d9

Browse files
committed
migrate to generators
1 parent b6ef483 commit d57b4d9

43 files changed

Lines changed: 1422 additions & 1 deletion

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

libs/ddd/src/_generators/domain/files/application/.gitkeep

Whitespace-only changes.

libs/ddd/src/_generators/domain/files/entities/.gitkeep

Whitespace-only changes.

libs/ddd/src/_generators/domain/files/infrastructure/.gitkeep

Whitespace-only changes.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import { Tree, formatFiles, installPackagesTask, generateFiles, joinPathFragments, readProjectConfiguration, addDependenciesToPackageJson } from '@nrwl/devkit';
2+
import { libraryGenerator, applicationGenerator } from '@nrwl/angular/generators';
3+
import { strings } from '@angular-devkit/core';
4+
import { DomainOptions } from './schema';
5+
import { updateDepConst } from '../utils/update-dep-const';
6+
import { wrapAngularDevkitSchematic } from '@nrwl/devkit/ngcli-adapter';
7+
import { insertImport } from '@nrwl/workspace/src/utilities/ast-utils';
8+
import { insertNgModuleImport } from '@nrwl/angular/src/generators/utils';
9+
import * as ts from 'typescript';
10+
import { NGRX_VERSION } from '../utils/ngrx-version';
11+
12+
export default async function (tree: Tree, options: DomainOptions) {
13+
14+
const appName = strings.dasherize(options.name);
15+
const appNameAndDirectory = options.appDirectory
16+
? `${options.appDirectory}/${appName}`
17+
: appName;
18+
const appNameAndDirectoryDasherized = strings
19+
.dasherize(appNameAndDirectory)
20+
.split('/')
21+
.join('-');
22+
const appFolderPath = `apps/${appNameAndDirectory}`;
23+
const appModuleFolder = `${appFolderPath}/src/app`;
24+
const appModuleFilepath = `${appModuleFolder}/app.module.ts`;
25+
26+
const libName = strings.dasherize(options.name);
27+
const libNameAndDirectory = options.directory
28+
? `${options.directory}/${libName}`
29+
: libName;
30+
const libNameAndDirectoryDasherized = strings
31+
.dasherize(libNameAndDirectory)
32+
.split('/')
33+
.join('-');
34+
const libFolderPath = `libs/${libNameAndDirectory}`;
35+
const libLibFolder = `${libFolderPath}/domain/src/lib`;
36+
37+
if (options.ngrx && !options.addApp) {
38+
throw new Error(
39+
`The 'ngrx' option may only be used when the 'addApp' option is used.`
40+
);
41+
}
42+
43+
await libraryGenerator(tree, {
44+
name: 'domain',
45+
directory: libNameAndDirectory,
46+
tags: `domain:${libName},type:domain-logic`,
47+
prefix: libName,
48+
publishable: options.type === 'publishable',
49+
buildable: options.type === 'buildable',
50+
importPath: options.importPath
51+
});
52+
53+
updateDepConst(tree, (depConst) => {
54+
depConst.push({
55+
sourceTag: `domain:${libName}`,
56+
onlyDependOnLibsWithTags: [`domain:${libName}`, 'domain:shared'],
57+
});
58+
});
59+
60+
generateFiles(
61+
tree,
62+
joinPathFragments(__dirname, './files'), // path to the file templates
63+
libLibFolder,
64+
{}
65+
);
66+
67+
if (options.addApp) {
68+
await applicationGenerator(tree, {
69+
name: appName,
70+
directory: options.appDirectory,
71+
tags: `domain:${appName},type:app`,
72+
style: 'scss',
73+
});
74+
}
75+
76+
if (options.addApp && options.ngrx) {
77+
const generateStore = wrapAngularDevkitSchematic('@ngrx/schematics', 'store');
78+
79+
await generateStore(tree, {
80+
project: appNameAndDirectoryDasherized,
81+
root: true,
82+
minimal: true,
83+
module: 'app.module.ts',
84+
name: 'state',
85+
});
86+
87+
addNgrxImportsToApp(tree, appModuleFilepath);
88+
addNgrxDependencies(tree);
89+
90+
await formatFiles(tree);
91+
return () => {
92+
installPackagesTask(tree);
93+
};
94+
95+
}
96+
97+
}
98+
99+
function addNgrxDependencies(tree: Tree) {
100+
addDependenciesToPackageJson(tree, {
101+
'@ngrx/store': NGRX_VERSION,
102+
'@ngrx/effects': NGRX_VERSION,
103+
'@ngrx/entity': NGRX_VERSION,
104+
'@ngrx/store-devtools': NGRX_VERSION,
105+
}, {});
106+
}
107+
108+
function addNgrxImportsToApp(tree: Tree, appModuleFilepath: string) {
109+
const moduleSource = tree.read(appModuleFilepath, 'utf-8');
110+
111+
let sourceFile = ts.createSourceFile(
112+
appModuleFilepath,
113+
moduleSource,
114+
ts.ScriptTarget.Latest,
115+
true
116+
);
117+
118+
insertImport(
119+
tree,
120+
sourceFile,
121+
appModuleFilepath,
122+
'EffectsModule',
123+
'@ngrx/effects');
124+
125+
insertNgModuleImport(tree, appModuleFilepath, 'EffectsModule.forRoot()');
126+
127+
}
128+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"$schema": "http://json-schema.org/schema",
3+
"cli": "nx",
4+
"$id": "domain",
5+
"type": "object",
6+
"properties": {
7+
"name": {
8+
"type": "string",
9+
"description": "Grouping name for the Domain",
10+
"x-prompt": "What is the name of the domain?",
11+
"$default": {
12+
"$source": "argv",
13+
"index": 0
14+
}
15+
},
16+
"appDirectory": {
17+
"type": "string",
18+
"description": "Grouping folder within the apps directory"
19+
},
20+
"directory": {
21+
"type": "string",
22+
"description": "Subpath of the domain within libs directory"
23+
},
24+
"addApp": {
25+
"type": "boolean",
26+
"description": "Add an app for the domain?",
27+
"x-prompt": "Would you like to add an associated application?",
28+
"default": false
29+
},
30+
"ngrx": {
31+
"type": "boolean",
32+
"default": false,
33+
"description": "Add ngrx for the associated app (addApp required)"
34+
},
35+
"type": {
36+
"type": "string",
37+
"enum": ["internal", "buildable", "publishable"],
38+
"description": "A type to determine if and how to build the library.",
39+
"default": "buildable"
40+
},
41+
"importPath": {
42+
"type": "string",
43+
"description": "For publishable libs: Official package name used in import statements"
44+
}
45+
},
46+
"required": ["name"]
47+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* tslint:disable */
2+
/**
3+
* This file was automatically generated by json-schema-to-typescript.
4+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
5+
* and run json-schema-to-typescript to regenerate this file.
6+
*/
7+
8+
export interface DomainOptions {
9+
/**
10+
* Grouping name for the Domain
11+
*/
12+
name: string;
13+
/**
14+
* Grouping folder within the apps directory
15+
*/
16+
appDirectory?: string;
17+
/**
18+
* Subpath of the domain within libs directory
19+
*/
20+
directory?: string;
21+
/**
22+
* Add an app for the domain?
23+
*/
24+
addApp?: boolean;
25+
/**
26+
* Add ngrx for the associated app (addApp required)
27+
*/
28+
ngrx?: boolean;
29+
/**
30+
* A type to determine if and how to build the library.
31+
*/
32+
type?: "internal" | "buildable" | "publishable";
33+
/**
34+
* For publishable libs: Official package name used in import statements
35+
*/
36+
importPath?: string;
37+
[k: string]: any;
38+
}
39+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Injectable } from '@angular/core';
2+
import { BehaviorSubject } from 'rxjs';
3+
<% if (entity) { %>
4+
import { <%=classify(entity)%> } from '../entities/<%=dasherize(entity)%>';
5+
import { <%=classify(entity)%>DataService } from '../infrastructure/<%=dasherize(entity)%>.data.service';
6+
<% } %>
7+
8+
@Injectable({ providedIn: 'root' })
9+
export class <%=classify(name)%>Facade {
10+
<% if (entity) { %>
11+
private <%=camelize(entity)%>ListSubject = new BehaviorSubject<<%=classify(entity)%>[]>([]);
12+
<%=camelize(entity)%>List$ = this.<%=camelize(entity)%>ListSubject.asObservable();
13+
14+
constructor(private <%=camelize(entity)%>DataService: <%=classify(entity)%>DataService) {
15+
}
16+
17+
load(): void {
18+
this.<%=camelize(entity)%>DataService.load().subscribe({
19+
next: <%=camelize(entity)%>List => {
20+
this.<%=camelize(entity)%>ListSubject.next(<%=camelize(entity)%>List)
21+
},
22+
error: err => {
23+
console.error('err', err);
24+
}
25+
});
26+
}
27+
<% } %>
28+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface <%=classify(entity)%> {
2+
id: number;
3+
name: string;
4+
description: string;
5+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {Injectable} from '@angular/core';
2+
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
3+
import {Observable, of} from 'rxjs';
4+
import {<%=classify(entity)%>} from '../entities/<%=dasherize(entity)%>';
5+
6+
@Injectable({ providedIn: 'root' })
7+
export class <%=classify(entity)%>DataService {
8+
9+
constructor(private http: HttpClient) {
10+
}
11+
12+
load(): Observable<<%=classify(entity)%>[]> {
13+
14+
// Uncomment if needed
15+
/*
16+
const url = '...';
17+
const params = new HttpParams().set('param', 'value');
18+
const headers = new HttpHeaders().set('Accept', 'application/json');
19+
return this.http.get<<%=classify(entity)%>[]>(url, {params, headers});
20+
*/
21+
22+
return of([
23+
{id: 1, name: 'Lorem ipsum', description: 'Lorem ipsum dolor sit amet'},
24+
{id: 2, name: 'At vero eos', description: 'At vero eos et accusam et justo duo dolores'},
25+
{id: 3, name: 'Duis autem', description: 'Duis autem vel eum iriure dolor in hendrerit'},
26+
]);
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { createAction, props } from '@ngrx/store';
2+
import { <%= classify(entity) %> } from '../../entities/<%= dasherize(entity) %>';
3+
4+
export const load<%= classify(entity) %> = createAction(
5+
'[<%= classify(entity) %>] Load <%= classify(entity) %>'
6+
);
7+
8+
export const load<%= classify(entity) %>Success = createAction(
9+
'[<%= classify(entity) %>] Load <%= classify(entity) %> Success',
10+
props<{ <%= camelize(entity) %>: <%= classify(entity) %>[] }>()
11+
);
12+
13+
export const load<%= classify(entity) %>Failure = createAction(
14+
'[<%= classify(entity) %>] Load <%= classify(entity) %> Failure',
15+
props<{ error: any }>()
16+
);

0 commit comments

Comments
 (0)