Skip to content

Commit 46f1b7e

Browse files
committed
feat(general): ng-add adds initial access restrictions to tslint.json
1 parent edae1cf commit 46f1b7e

File tree

6 files changed

+122
-42
lines changed

6 files changed

+122
-42
lines changed

libs/ddd/assets/linting-3.png

28.9 KB
Loading

libs/ddd/collection.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"name": "@angular-architects/ddd",
44
"version": "0.0.1",
55
"schematics": {
6+
"ng-add": {
7+
"factory": "./src/schematics/ng-add/index",
8+
"description": "adds initial access restrictions to linting rules"
9+
},
610
"domain": {
711
"factory": "./src/schematics/domain/index",
812
"schema": "./src/schematics/domain/schema.json",

libs/ddd/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
{
22
"name": "@angular-architects/ddd",
3-
"version": "0.0.7",
3+
"version": "1.0.0",
4+
"author": "Manfred Steyer",
5+
"description": "Nx plugin for structuring a monorepo with domains and layers",
6+
"repository": {
7+
"type": "github",
8+
"url": "https://github.com/angular-architects/nx-ddd-plugin"
9+
},
410
"main": "src/index.js",
511
"schematics": "./collection.json"
612
}

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

Lines changed: 2 additions & 2 deletions
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 { updateLintingRules } from '../utils/update-linting-rules';
15+
import { addDomainToLintingRules } from '../utils/update-linting-rules';
1616

1717
export default function(options: DomainOptions): Rule {
1818
const libFolder = strings.dasherize(options.name);
@@ -30,7 +30,7 @@ export default function(options: DomainOptions): Rule {
3030
style: 'scss',
3131
prefix: options.name,
3232
}),
33-
updateLintingRules(options.name),
33+
addDomainToLintingRules(options.name),
3434
mergeWith(templateSource),
3535
(!options.addApp) ?
3636
noop() :
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {
2+
chain,
3+
Rule,
4+
} from '@angular-devkit/schematics';
5+
6+
import { initLintingRules } from '../utils/update-linting-rules';
7+
8+
export default function(): Rule {
9+
return chain([
10+
initLintingRules(),
11+
]);
12+
}
Lines changed: 97 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,100 @@
11
import { Rule, Tree, SchematicContext } from '@angular-devkit/schematics';
22

3-
export function updateLintingRules(domainName: string): Rule {
4-
return (host: Tree, context: SchematicContext) => {
5-
const text = host.read('tslint.json').toString();
6-
const rules = JSON.parse(text);
7-
8-
if (!rules['rules']) {
9-
context.logger.info('tslint.json: rules expected');
10-
return;
11-
}
12-
13-
if (!rules['rules']['nx-enforce-module-boundaries']) {
14-
context.logger.info('tslint.json: nx-enforce-module-boundaries expected');
15-
return;
16-
}
17-
18-
if (rules['rules']['nx-enforce-module-boundaries']['length'] < 2) {
19-
context.logger.info('nx-enforce-module-boundaries.1 unexpected');
20-
return;
21-
}
22-
23-
if (!rules['rules']['nx-enforce-module-boundaries'][1]['depConstraints']) {
24-
context.logger.info('tslint.json: nx-enforce-module-boundaries.1.depConstraints expected.');
25-
return;
26-
}
27-
28-
if (!Array.isArray(rules['rules']['nx-enforce-module-boundaries'][1]['depConstraints'])) {
29-
context.logger.info('tslint.json: nx-enforce-module-boundaries.1.depConstraints expected to be an array.');
30-
return;
31-
}
32-
33-
const depConst = rules['rules']['nx-enforce-module-boundaries'][1]['depConstraints'];
34-
depConst.push({
35-
'sourceTag': `domain:${domainName}`,
36-
'onlyDependOnLibsWithTags': [`domain:${domainName}`, 'shared']
37-
});
38-
39-
const newText = JSON.stringify(rules, undefined, 2);
40-
host.overwrite('tslint.json', newText);
3+
export function checkRuleExists(rules: object, context: SchematicContext) {
4+
if (!rules['rules']) {
5+
context.logger.info('tslint.json: rules expected');
6+
return false;
7+
}
8+
9+
if (!rules['rules']['nx-enforce-module-boundaries']) {
10+
context.logger.info('tslint.json: nx-enforce-module-boundaries expected');
11+
return false;
12+
}
13+
14+
if (rules['rules']['nx-enforce-module-boundaries']['length'] < 2) {
15+
context.logger.info('nx-enforce-module-boundaries.1 unexpected');
16+
return false;
17+
}
18+
19+
if (!rules['rules']['nx-enforce-module-boundaries'][1]['depConstraints']) {
20+
context.logger.info('tslint.json: nx-enforce-module-boundaries.1.depConstraints expected.');
21+
return false;
22+
}
23+
24+
if (!Array.isArray(rules['rules']['nx-enforce-module-boundaries'][1]['depConstraints'])) {
25+
context.logger.info('tslint.json: nx-enforce-module-boundaries.1.depConstraints expected to be an array.');
26+
return false;
27+
}
28+
29+
return true;
30+
}
31+
32+
33+
export function addDomainToLintingRules(domainName: string): Rule {
34+
return (host: Tree, context: SchematicContext) => {
35+
const text = host.read('tslint.json').toString();
36+
const rules = JSON.parse(text);
37+
38+
if (!checkRuleExists(rules, context)) return;
39+
40+
const depConst = rules['rules']['nx-enforce-module-boundaries'][1]['depConstraints'];
41+
depConst.push({
42+
'sourceTag': `domain:${domainName}`,
43+
'onlyDependOnLibsWithTags': [`domain:${domainName}`, 'domain:shared']
44+
});
45+
46+
const newText = JSON.stringify(rules, undefined, 2);
47+
host.overwrite('tslint.json', newText);
48+
}
49+
}
50+
51+
export function initLintingRules(): Rule {
52+
return (host: Tree, context: SchematicContext) => {
53+
const text = host.read('tslint.json').toString();
54+
const rules = JSON.parse(text);
55+
56+
if (!checkRuleExists(rules, context)) return;
57+
58+
const depConst = rules['rules']['nx-enforce-module-boundaries'][1]['depConstraints'] as Array<object>;
59+
60+
const jokerIndex = depConst.findIndex(entry =>
61+
entry['sourceTag']
62+
&& entry['sourceTag'] === '*'
63+
&& entry['onlyDependOnLibsWithTags']
64+
&& Array.isArray(entry['onlyDependOnLibsWithTags'])
65+
&& entry['onlyDependOnLibsWithTags'].length > 0
66+
&& entry['onlyDependOnLibsWithTags'][0] === '*');
67+
68+
if (jokerIndex !== -1) {
69+
depConst.splice(jokerIndex, 1);
4170
}
42-
}
71+
72+
depConst.push({
73+
'sourceTag': 'type:api',
74+
'onlyDependOnLibsWithTags': ['type:ui', 'type:domain-logic', 'type:util']
75+
});
76+
77+
depConst.push({
78+
'sourceTag': 'type:feature',
79+
'onlyDependOnLibsWithTags': ['type:ui', 'type:domain-logic', 'type:util']
80+
});
81+
82+
depConst.push({
83+
'sourceTag': 'type:ui',
84+
'onlyDependOnLibsWithTags': ['type:domain-logic', 'type:util']
85+
});
86+
87+
depConst.push({
88+
'sourceTag': 'domain-logic',
89+
'onlyDependOnLibsWithTags': ['type:util']
90+
});
91+
92+
depConst.push({
93+
'sourceTag': 'domain:shared',
94+
'onlyDependOnLibsWithTags': ['domain:shared']
95+
});
96+
97+
const newText = JSON.stringify(rules, undefined, 2);
98+
host.overwrite('tslint.json', newText);
99+
}
100+
}

0 commit comments

Comments
 (0)