1
1
import { Rule , Tree , SchematicContext } from '@angular-devkit/schematics' ;
2
2
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 ) ;
41
70
}
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