Skip to content

Commit ee7868d

Browse files
committed
support eslint
1 parent 90e9d0b commit ee7868d

File tree

1 file changed

+86
-72
lines changed

1 file changed

+86
-72
lines changed
Lines changed: 86 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,119 @@
11
import { Rule, Tree, SchematicContext } from '@angular-devkit/schematics';
22

3-
export function checkRuleExists(rules: object, context: SchematicContext) {
3+
function checkRuleExists(filePath: string, rule: string, rules: object, context: SchematicContext) {
44
if (!rules['rules']) {
5-
context.logger.info('tslint.json: rules expected');
5+
context.logger.info(`${filePath}: rules expected`);
66
return false;
77
}
88

9-
if (!rules['rules']['nx-enforce-module-boundaries']) {
10-
context.logger.info('tslint.json: nx-enforce-module-boundaries expected');
9+
if (!rules['rules'][rule]) {
10+
context.logger.info(`${filePath}: ${rule} expected`);
1111
return false;
1212
}
1313

14-
if (rules['rules']['nx-enforce-module-boundaries']['length'] < 2) {
15-
context.logger.info('nx-enforce-module-boundaries.1 unexpected');
14+
if (rules['rules'][rule]['length'] < 2) {
15+
context.logger.info(`${filePath}: ${rule}.1 unexpected`);
1616
return false;
1717
}
1818

19-
if (!rules['rules']['nx-enforce-module-boundaries'][1]['depConstraints']) {
20-
context.logger.info('tslint.json: nx-enforce-module-boundaries.1.depConstraints expected.');
19+
if (!rules['rules'][rule][1]['depConstraints']) {
20+
context.logger.info(`${filePath}: ${rule}.1.depConstraints expected.`);
2121
return false;
2222
}
2323

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.');
24+
if (!Array.isArray(rules['rules'][rule][1]['depConstraints'])) {
25+
context.logger.info(`${filePath}: ${rule}.1.depConstraints expected to be an array.`);
2626
return false;
2727
}
2828

2929
return true;
3030
}
3131

32+
function updateDepConst(host: Tree, context: SchematicContext, update: (depConst: Array<object>) => void) {
33+
let filePath = 'tslint.json';
34+
let rule = 'nx-enforce-module-boundaries';
35+
36+
if (!host.exists('tslint.json')) {
37+
if (host.exists(".eslintrc.json")) {
38+
filePath = ".eslintrc.json";
39+
rule = '@nrwl/nx/enforce-module-boundaries';
40+
} else if (host.exists(".eslintrc")) {
41+
filePath = ".eslintrc";
42+
rule = '@nrwl/nx/enforce-module-boundaries';
43+
} else {
44+
context.logger.info('Cannot add linting rules: linting config file does not exist');
45+
return;
46+
}
47+
}
48+
49+
const text = host.read(filePath).toString();
50+
const rules = JSON.parse(text);
51+
52+
if (!checkRuleExists(filePath, rule, rules, context)) return;
53+
54+
const depConst = rules['rules'][rule][1]['depConstraints'] as Array<object>;
55+
update(depConst);
56+
57+
const newText = JSON.stringify(rules, undefined, 2);
58+
host.overwrite(filePath, newText);
59+
}
60+
3261

3362
export function addDomainToLintingRules(domainName: string): Rule {
3463
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']
64+
updateDepConst(host, context, (depConst) => {
65+
depConst.push({
66+
'sourceTag': `domain:${domainName}`,
67+
'onlyDependOnLibsWithTags': [`domain:${domainName}`, 'domain:shared']
68+
});
4469
});
45-
46-
const newText = JSON.stringify(rules, undefined, 2);
47-
host.overwrite('tslint.json', newText);
4870
}
4971
}
5072

5173
export function initLintingRules(): Rule {
5274
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);
70-
}
71-
72-
depConst.push({
73-
'sourceTag': 'type:app',
74-
'onlyDependOnLibsWithTags': ['type:api', 'type:feature', 'type:ui', 'type:domain-logic', 'type:util']
75+
updateDepConst(host, context, (depConst) => {
76+
const jokerIndex = depConst.findIndex(entry =>
77+
entry['sourceTag']
78+
&& entry['sourceTag'] === '*'
79+
&& entry['onlyDependOnLibsWithTags']
80+
&& Array.isArray(entry['onlyDependOnLibsWithTags'])
81+
&& entry['onlyDependOnLibsWithTags'].length > 0
82+
&& entry['onlyDependOnLibsWithTags'][0] === '*');
83+
84+
if (jokerIndex !== -1) {
85+
depConst.splice(jokerIndex, 1);
86+
}
87+
88+
depConst.push({
89+
'sourceTag': 'type:app',
90+
'onlyDependOnLibsWithTags': ['type:api', 'type:feature', 'type:ui', 'type:domain-logic', 'type:util']
91+
});
92+
93+
depConst.push({
94+
'sourceTag': 'type:api',
95+
'onlyDependOnLibsWithTags': ['type:ui', 'type:domain-logic', 'type:util']
96+
});
97+
98+
depConst.push({
99+
'sourceTag': 'type:feature',
100+
'onlyDependOnLibsWithTags': ['type:ui', 'type:domain-logic', 'type:util']
101+
});
102+
103+
depConst.push({
104+
'sourceTag': 'type:ui',
105+
'onlyDependOnLibsWithTags': ['type:domain-logic', 'type:util']
106+
});
107+
108+
depConst.push({
109+
'sourceTag': 'type:domain-logic',
110+
'onlyDependOnLibsWithTags': ['type:util']
111+
});
112+
113+
depConst.push({
114+
'sourceTag': 'domain:shared',
115+
'onlyDependOnLibsWithTags': ['domain:shared']
116+
});
75117
});
76-
77-
depConst.push({
78-
'sourceTag': 'type:api',
79-
'onlyDependOnLibsWithTags': ['type:ui', 'type:domain-logic', 'type:util']
80-
});
81-
82-
depConst.push({
83-
'sourceTag': 'type:feature',
84-
'onlyDependOnLibsWithTags': ['type:ui', 'type:domain-logic', 'type:util']
85-
});
86-
87-
depConst.push({
88-
'sourceTag': 'type:ui',
89-
'onlyDependOnLibsWithTags': ['type:domain-logic', 'type:util']
90-
});
91-
92-
depConst.push({
93-
'sourceTag': 'type:domain-logic',
94-
'onlyDependOnLibsWithTags': ['type:util']
95-
});
96-
97-
depConst.push({
98-
'sourceTag': 'domain:shared',
99-
'onlyDependOnLibsWithTags': ['domain:shared']
100-
});
101-
102-
const newText = JSON.stringify(rules, undefined, 2);
103-
host.overwrite('tslint.json', newText);
104118
}
105119
}

0 commit comments

Comments
 (0)