Skip to content

Commit 14eccac

Browse files
author
Peter Smith
committed
chore(general): organize rules into separate directory
Why was this necessary? - the features index was filled with rules to the point of confusion - the utils directory exported rules - additional ngrx rules are going to be required How does this commit solve the problem? - creates a rules directory with an index that exports all schematic rules - creates a utils directory with an index that exports all functions thats are not default in schematics ts files What side effects does this have? - all of the rules have a jsDoc parameter description so that developers can continue to know the details of the params at-a-glance
1 parent b5bc4fc commit 14eccac

22 files changed

+433
-292
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212

1313
import { strings } from '@angular-devkit/core';
1414
import { DomainOptions } from './schema';
15-
import { addDomainToLintingRules } from '../utils/update-linting-rules';
15+
import { addDomainToLintingRules } from '../rules';
1616

1717
export default function (options: DomainOptions): Rule {
1818
const libFolder = strings.dasherize(options.name);
Lines changed: 67 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,53 @@
1-
import { chain, externalSchematic, Rule, Tree, SchematicsException, apply, url, template, move, mergeWith, noop, filter } from '@angular-devkit/schematics';
1+
import {
2+
chain,
3+
externalSchematic,
4+
Rule,
5+
Tree,
6+
apply,
7+
url,
8+
template,
9+
move,
10+
mergeWith,
11+
noop,
12+
} from '@angular-devkit/schematics';
213
import { FeatureOptions } from './schema';
314
import { strings } from '@angular-devkit/core';
4-
5-
import { addImportToModule, addDeclarationToModule, addExportToModule } from '@schematics/angular/utility/ast-utils';
6-
import { InsertChange } from '@schematics/angular/utility/change';
7-
import * as ts from '@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript';
8-
// import * as ts from 'typescript';
9-
10-
11-
12-
// Taken from @schematics/angular
13-
function readIntoSourceFile(host: Tree, modulePath: string): ts.SourceFile {
14-
const text = host.read(modulePath);
15-
if (text === null) {
16-
throw new SchematicsException(`File ${modulePath} does not exist.`);
17-
}
18-
const sourceText = text.toString('utf-8');
19-
20-
return ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
21-
}
22-
23-
function addImport(
24-
modulePath: string,
25-
ngModuleToImportPath: string,
26-
ngModuleToImportName: string,
27-
optional = false): Rule {
28-
29-
return (host: Tree) => {
30-
31-
if (optional && !host.exists(modulePath)) {
32-
return;
33-
}
34-
35-
const source = readIntoSourceFile(host, modulePath);
36-
37-
const changes = addImportToModule(
38-
source,
39-
modulePath,
40-
ngModuleToImportName,
41-
ngModuleToImportPath)
42-
43-
const declarationRecorder = host.beginUpdate(modulePath);
44-
for (const change of changes) {
45-
if (change instanceof InsertChange) {
46-
declarationRecorder.insertLeft(change.pos, change.toAdd);
47-
}
48-
}
49-
host.commitUpdate(declarationRecorder);
50-
}
51-
}
52-
53-
function addDeclaration(
54-
modulePath: string,
55-
componentToImportPath: string,
56-
componentToImportName: string): Rule {
57-
58-
return (host: Tree) => {
59-
60-
const source = readIntoSourceFile(host, modulePath);
61-
62-
const changes = addDeclarationToModule(
63-
source,
64-
modulePath,
65-
componentToImportName,
66-
componentToImportPath);
67-
68-
const declarationRecorder = host.beginUpdate(modulePath);
69-
for (const change of changes) {
70-
if (change instanceof InsertChange) {
71-
declarationRecorder.insertLeft(change.pos, change.toAdd);
72-
}
73-
}
74-
host.commitUpdate(declarationRecorder);
75-
}
76-
}
77-
78-
function addExport(
79-
modulePath: string,
80-
componentToImportPath: string,
81-
componentToImportName: string): Rule {
82-
83-
return (host: Tree) => {
84-
85-
const source = readIntoSourceFile(host, modulePath);
86-
87-
const changes = addExportToModule(
88-
source,
89-
modulePath,
90-
componentToImportName,
91-
componentToImportPath);
92-
93-
const declarationRecorder = host.beginUpdate(modulePath);
94-
for (const change of changes) {
95-
if (change instanceof InsertChange) {
96-
declarationRecorder.insertLeft(change.pos, change.toAdd);
97-
}
98-
}
99-
host.commitUpdate(declarationRecorder);
100-
}
101-
}
102-
103-
function addTsExport(filePath: string, filesToExport: string[]): Rule {
15+
import {
16+
addDeclaration,
17+
addExport,
18+
addImport,
19+
addTsExport,
20+
filterTemplates,
21+
} from '../rules';
22+
import { readWorkspaceName } from '../utils';
23+
24+
export default function (options: FeatureOptions): Rule {
10425
return (host: Tree) => {
105-
let content = host.read(filePath) + '\n';
106-
107-
for(const file of filesToExport) {
108-
content += `export * from '${file}';\n`;
109-
}
110-
111-
host.overwrite(filePath, content);
112-
}
113-
}
114-
115-
function filterTemplates(options: FeatureOptions): Rule {
116-
if (!options.entity) {
117-
return filter(path => !!path.match(/\.facade\.ts$/));
118-
}
119-
return filter(_ => true);
120-
}
121-
122-
function readWorkspaceName(host: Tree): string {
123-
const content = host.read('nx.json').toString();
124-
const config = JSON.parse(content);
125-
return '@' + config['npmScope'];
126-
}
127-
128-
export default function(options: FeatureOptions): Rule {
129-
130-
return (host: Tree) => {
131-
13226
const workspaceName = readWorkspaceName(host);
13327

13428
const domainFolderName = strings.dasherize(options.domain);
13529
const domainPath = `libs/${domainFolderName}/domain/src/lib`;
136-
const domainModuleClassName = strings.classify(options.domain) + "DomainModule";
30+
const domainModuleClassName =
31+
strings.classify(options.domain) + 'DomainModule';
13732
const domainImportPath = `${workspaceName}/${domainFolderName}/domain`;
13833
const domainIndexPath = `libs/${domainFolderName}/domain/src/index.ts`;
13934

14035
const featureName = strings.dasherize(options.name);
14136
const featureFolderName = (options.prefix ? 'feature-' : '') + featureName;
14237
const featurePath = `libs/${domainFolderName}/${featureFolderName}/src/lib`;
14338
const featureModulePath = `${featurePath}/${domainFolderName}-${featureFolderName}.module.ts`;
144-
const featureModuleClassName = strings.classify(`${options.domain}-${featureFolderName}Module`);
39+
const featureModuleClassName = strings.classify(
40+
`${options.domain}-${featureFolderName}Module`
41+
);
14542
const featureImportPath = `${workspaceName}/${domainFolderName}/${featureFolderName}`;
14643
const featureIndexPath = `libs/${domainFolderName}/${featureFolderName}/src/index.ts`;
14744

14845
const entityName = options.entity ? strings.dasherize(options.entity) : '';
14946

15047
const featureComponentImportPath = `./${featureName}.component`;
151-
const featureComponentClassName = strings.classify(`${featureName}Component`);
48+
const featureComponentClassName = strings.classify(
49+
`${featureName}Component`
50+
);
15251

15352
const appName = options.app || options.domain;
15453
const appFolderName = strings.dasherize(appName);
@@ -157,7 +56,9 @@ export default function(options: FeatureOptions): Rule {
15756
if (options.app) {
15857
const requiredAppModulePath = `apps/${appFolderName}/src/app/app.module.ts`;
15958
if (!host.exists(requiredAppModulePath)) {
160-
throw new Error(`Specified app ${options.app} does not exist: ${requiredAppModulePath} expected!`);
59+
throw new Error(
60+
`Specified app ${options.app} does not exist: ${requiredAppModulePath} expected!`
61+
);
16162
}
16263
}
16364

@@ -175,7 +76,7 @@ export default function(options: FeatureOptions): Rule {
17576

17677
const featureTemplates = apply(url('./files/forFeature'), [
17778
template({ ...strings, ...options, workspaceName }),
178-
move(featurePath)
79+
move(featurePath),
17980
]);
18081

18182
return chain([
@@ -189,24 +90,42 @@ export default function(options: FeatureOptions): Rule {
18990
buildable: options.type === 'buildable',
19091
}),
19192
addImport(featureModulePath, domainImportPath, domainModuleClassName),
192-
(!options.lazy && host.exists(appModulePath)) ?
193-
chain([
194-
addImport(appModulePath, featureImportPath, featureModuleClassName, true),
195-
addImport(appModulePath, '@angular/common/http', 'HttpClientModule', true)
196-
]) :
197-
noop(),
93+
!options.lazy && host.exists(appModulePath)
94+
? chain([
95+
addImport(
96+
appModulePath,
97+
featureImportPath,
98+
featureModuleClassName,
99+
true
100+
),
101+
addImport(
102+
appModulePath,
103+
'@angular/common/http',
104+
'HttpClientModule',
105+
true
106+
),
107+
])
108+
: noop(),
198109
mergeWith(domainTemplates),
199-
(options.entity) ?
200-
addTsExport(domainIndexPath, [
201-
`./lib/entities/${entityName}`,
202-
`./lib/infrastructure/${entityName}.data.service`
203-
]) :
204-
noop(),
110+
options.entity
111+
? addTsExport(domainIndexPath, [
112+
`./lib/entities/${entityName}`,
113+
`./lib/infrastructure/${entityName}.data.service`,
114+
])
115+
: noop(),
205116
addTsExport(domainIndexPath, [`./lib/application/${featureName}.facade`]),
206117
mergeWith(featureTemplates),
207118
addTsExport(featureIndexPath, [`./lib/${featureName}.component`]),
208-
addDeclaration(featureModulePath, featureComponentImportPath, featureComponentClassName),
209-
addExport(featureModulePath, featureComponentImportPath, featureComponentClassName),
119+
addDeclaration(
120+
featureModulePath,
121+
featureComponentImportPath,
122+
featureComponentClassName
123+
),
124+
addExport(
125+
featureModulePath,
126+
featureComponentImportPath,
127+
featureComponentClassName
128+
),
210129
]);
211-
}
130+
};
212131
}

libs/ddd/src/schematics/ng-add/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import {
22
chain,
33
Rule,
44
} from '@angular-devkit/schematics';
5-
6-
import { initLintingRules } from '../utils/update-linting-rules';
5+
import { initLintingRules } from '../rules';
76

87
export default function(): Rule {
98
return chain([
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Rule, Tree } from '@angular-devkit/schematics';
2+
import { addDeclarationToModule } from '@schematics/angular/utility/ast-utils';
3+
import { InsertChange } from '@schematics/angular/utility/change';
4+
import { readIntoSourceFile } from '../utils';
5+
6+
/**
7+
* addDeclaration
8+
* @param modulePath path of the module to add the declaration to
9+
* @param componentToImportPath path of the component being imported
10+
* @param componentToImportName name of the component being imported
11+
*/
12+
export function addDeclaration(
13+
modulePath: string,
14+
componentToImportPath: string,
15+
componentToImportName: string
16+
): Rule {
17+
return (host: Tree) => {
18+
const source = readIntoSourceFile(host, modulePath);
19+
20+
const changes = addDeclarationToModule(
21+
source,
22+
modulePath,
23+
componentToImportName,
24+
componentToImportPath
25+
);
26+
27+
const declarationRecorder = host.beginUpdate(modulePath);
28+
for (const change of changes) {
29+
if (change instanceof InsertChange) {
30+
declarationRecorder.insertLeft(change.pos, change.toAdd);
31+
}
32+
}
33+
host.commitUpdate(declarationRecorder);
34+
};
35+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Rule, Tree, SchematicContext } from '@angular-devkit/schematics';
2+
import { checkRuleExists, updateDepConst } from '../utils';
3+
4+
/**
5+
* addDomainToLintingRules
6+
* @param domainName name of the domain that is being included in the tslint.json
7+
*/
8+
export function addDomainToLintingRules(domainName: string): Rule {
9+
return (host: Tree, context: SchematicContext) => {
10+
updateDepConst(host, context, (depConst) => {
11+
depConst.push({
12+
sourceTag: `domain:${domainName}`,
13+
onlyDependOnLibsWithTags: [`domain:${domainName}`, 'domain:shared'],
14+
});
15+
});
16+
};
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Rule, Tree } from '@angular-devkit/schematics';
2+
import { addExportToModule } from '@schematics/angular/utility/ast-utils';
3+
import { InsertChange } from '@schematics/angular/utility/change';
4+
import { readIntoSourceFile } from '../utils';
5+
6+
/**
7+
* addExport
8+
* @param modulePath - path of the module to include the export in
9+
* @param componentToImportPath path of the component to import
10+
* @param componentToImportName name of the component to import
11+
*/
12+
export function addExport(
13+
modulePath: string,
14+
componentToImportPath: string,
15+
componentToImportName: string
16+
): Rule {
17+
return (host: Tree) => {
18+
const source = readIntoSourceFile(host, modulePath);
19+
20+
const changes = addExportToModule(
21+
source,
22+
modulePath,
23+
componentToImportName,
24+
componentToImportPath
25+
);
26+
27+
const declarationRecorder = host.beginUpdate(modulePath);
28+
for (const change of changes) {
29+
if (change instanceof InsertChange) {
30+
declarationRecorder.insertLeft(change.pos, change.toAdd);
31+
}
32+
}
33+
host.commitUpdate(declarationRecorder);
34+
};
35+
}

0 commit comments

Comments
 (0)