|
1 | 1 | import { Rule, Tree, SchematicContext } from '@angular-devkit/schematics'; |
2 | 2 |
|
3 | | -export function checkRuleExists(rules: object, context: SchematicContext) { |
| 3 | +function checkRuleExists(filePath: string, rule: string, rules: object, context: SchematicContext) { |
4 | 4 | if (!rules['rules']) { |
5 | | - context.logger.info('tslint.json: rules expected'); |
| 5 | + context.logger.info(`${filePath}: rules expected`); |
6 | 6 | return false; |
7 | 7 | } |
8 | 8 |
|
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`); |
11 | 11 | return false; |
12 | 12 | } |
13 | 13 |
|
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`); |
16 | 16 | return false; |
17 | 17 | } |
18 | 18 |
|
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.`); |
21 | 21 | return false; |
22 | 22 | } |
23 | 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.'); |
| 24 | + if (!Array.isArray(rules['rules'][rule][1]['depConstraints'])) { |
| 25 | + context.logger.info(`${filePath}: ${rule}.1.depConstraints expected to be an array.`); |
26 | 26 | return false; |
27 | 27 | } |
28 | 28 |
|
29 | 29 | return true; |
30 | 30 | } |
31 | 31 |
|
| 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 | + |
32 | 61 |
|
33 | 62 | export function addDomainToLintingRules(domainName: string): Rule { |
34 | 63 | 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 | + }); |
44 | 69 | }); |
45 | | - |
46 | | - const newText = JSON.stringify(rules, undefined, 2); |
47 | | - host.overwrite('tslint.json', newText); |
48 | 70 | } |
49 | 71 | } |
50 | 72 |
|
51 | 73 | export function initLintingRules(): Rule { |
52 | 74 | 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 | + }); |
75 | 117 | }); |
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); |
104 | 118 | } |
105 | 119 | } |
0 commit comments