Skip to content

Commit ebb9c46

Browse files
committed
refactor(rules): moved rules from utils to rules directory
1 parent dc93488 commit ebb9c46

File tree

9 files changed

+60
-39
lines changed

9 files changed

+60
-39
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
Tree
99
} from '@angular-devkit/schematics';
1010
import { getWorkspace } from '@schematics/angular/utility/config';
11-
import { addDomainToLintingRules } from '../utils/update-linting-rules';
11+
import { addDomainToLintingRules } from '../rules';
1212
import { DomainOptions } from './schema';
1313

1414
export default function(options: DomainOptions): Rule {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import {
99
Tree,
1010
url
1111
} from '@angular-devkit/schematics';
12-
import { addExportsToBarrel } from '../utils/add-exports-to-barrel';
13-
import { readWorkspaceName } from '../utils/read-workspace-name';
14-
import { addImportsToModule } from './rules/add-imports-to-module';
12+
import {
13+
addExportsToBarrel,
14+
addNgxsImportsToModule,
15+
readWorkspaceName
16+
} from '../rules';
1517
import { EntityOptions } from './schema';
1618

1719
export default function(options: EntityOptions): Rule {
@@ -35,7 +37,7 @@ export default function(options: EntityOptions): Rule {
3537
`./lib/services/${strings.dasherize(options.name)}.service`,
3638
`./lib/state/${strings.dasherize(options.name)}.state`
3739
]),
38-
addImportsToModule(options)
40+
addNgxsImportsToModule(options)
3941
]);
4042
};
4143
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { chain, Rule } from '@angular-devkit/schematics';
2-
import { addNgRxToPackageJson } from '../utils/add-ngxs-to-package-json';
3-
import { initLintingRules } from '../utils/update-linting-rules';
2+
import { addNgxsToPackageJson, initLintingRules } from '../rules';
43

54
export default function(): Rule {
6-
return chain([initLintingRules(), addNgRxToPackageJson()]);
5+
return chain([initLintingRules(), addNgxsToPackageJson()]);
76
}

libs/ddd/src/schematics/entity/rules/add-imports-to-module.ts renamed to libs/ddd/src/schematics/rules/add-ngxs-imports-to-module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { addImportToModule } from '@nrwl/angular/src/utils/ast-utils';
44
import { insert, toClassName } from '@nrwl/workspace';
55
import { insertImport } from '@nrwl/workspace/src/utils/ast-utils';
66
import * as ts from 'typescript';
7-
import { EntityOptions } from '../schema';
7+
import { EntityOptions } from '../entity/schema';
88

9-
export function addImportsToModule(options: EntityOptions): Rule {
9+
export function addNgxsImportsToModule(options: EntityOptions): Rule {
1010
return (host: Tree) => {
1111
const stateName = `${toClassName(options.name)}State`;
1212
const storeForFeature = `NgxsModule.forFeature([${stateName}])`;

libs/ddd/src/schematics/utils/add-ngxs-to-package-json.ts renamed to libs/ddd/src/schematics/rules/add-ngxs-to-package-json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Rule } from '@angular-devkit/schematics';
22
import { addDepsToPackageJson } from '@nrwl/workspace/src/utils/ast-utils';
33

44
const ngxsVersion = '3.6.2';
5-
export function addNgRxToPackageJson(): Rule {
5+
export function addNgxsToPackageJson(): Rule {
66
return addDepsToPackageJson(
77
{
88
'@ngxs/devtools-plugin': ngxsVersion,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export { addExportsToBarrel } from './add-exports-to-barrel';
2+
export { addNgxsImportsToModule } from './add-ngxs-imports-to-module';
3+
export { addNgxsToPackageJson } from './add-ngxs-to-package-json';
4+
export { readWorkspaceName } from './read-workspace-name';
5+
export {
6+
addDomainToLintingRules,
7+
initLintingRules
8+
} from './update-linting-rules';

libs/ddd/src/schematics/utils/update-linting-rules.ts renamed to libs/ddd/src/schematics/rules/update-linting-rules.ts

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Rule, Tree, SchematicContext } from '@angular-devkit/schematics';
1+
import { Rule, Tree, SchematicContext } from '@angular-devkit/schematics';
22

33
export function checkRuleExists(rules: object, context: SchematicContext) {
44
if (!rules['rules']) {
@@ -17,35 +17,43 @@ export function checkRuleExists(rules: object, context: SchematicContext) {
1717
}
1818

1919
if (!rules['rules']['nx-enforce-module-boundaries'][1]['depConstraints']) {
20-
context.logger.info('tslint.json: nx-enforce-module-boundaries.1.depConstraints expected.');
20+
context.logger.info(
21+
'tslint.json: nx-enforce-module-boundaries.1.depConstraints expected.'
22+
);
2123
return false;
2224
}
2325

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+
if (
27+
!Array.isArray(
28+
rules['rules']['nx-enforce-module-boundaries'][1]['depConstraints']
29+
)
30+
) {
31+
context.logger.info(
32+
'tslint.json: nx-enforce-module-boundaries.1.depConstraints expected to be an array.'
33+
);
2634
return false;
2735
}
2836

2937
return true;
3038
}
3139

32-
3340
export function addDomainToLintingRules(domainName: string): Rule {
3441
return (host: Tree, context: SchematicContext) => {
3542
const text = host.read('tslint.json').toString();
3643
const rules = JSON.parse(text);
3744

3845
if (!checkRuleExists(rules, context)) return;
3946

40-
const depConst = rules['rules']['nx-enforce-module-boundaries'][1]['depConstraints'];
47+
const depConst =
48+
rules['rules']['nx-enforce-module-boundaries'][1]['depConstraints'];
4149
depConst.push({
42-
'sourceTag': `domain:${domainName}`,
43-
'onlyDependOnLibsWithTags': [`domain:${domainName}`, 'domain:shared']
50+
sourceTag: `domain:${domainName}`,
51+
onlyDependOnLibsWithTags: [`domain:${domainName}`, 'domain:shared']
4452
});
4553

4654
const newText = JSON.stringify(rules, undefined, 2);
4755
host.overwrite('tslint.json', newText);
48-
}
56+
};
4957
}
5058

5159
export function initLintingRules(): Rule {
@@ -55,15 +63,19 @@ export function initLintingRules(): Rule {
5563

5664
if (!checkRuleExists(rules, context)) return;
5765

58-
const depConst = rules['rules']['nx-enforce-module-boundaries'][1]['depConstraints'] as Array<object>;
66+
const depConst = rules['rules']['nx-enforce-module-boundaries'][1][
67+
'depConstraints'
68+
] as Array<object>;
5969

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] === '*');
70+
const jokerIndex = depConst.findIndex(
71+
entry =>
72+
entry['sourceTag'] &&
73+
entry['sourceTag'] === '*' &&
74+
entry['onlyDependOnLibsWithTags'] &&
75+
Array.isArray(entry['onlyDependOnLibsWithTags']) &&
76+
entry['onlyDependOnLibsWithTags'].length > 0 &&
77+
entry['onlyDependOnLibsWithTags'][0] === '*'
78+
);
6779

6880
if (jokerIndex !== -1) {
6981
depConst.splice(jokerIndex, 1);
@@ -90,31 +102,31 @@ export function initLintingRules(): Rule {
90102
});
91103

92104
depConst.push({
93-
'sourceTag': 'type:api',
94-
'onlyDependOnLibsWithTags': ['type:ui', 'type:domain-logic', 'type:util']
105+
sourceTag: 'type:api',
106+
onlyDependOnLibsWithTags: ['type:ui', 'type:domain-logic', 'type:util']
95107
});
96108

97109
depConst.push({
98-
'sourceTag': 'type:feature',
99-
'onlyDependOnLibsWithTags': ['type:ui', 'type:domain-logic', 'type:util']
110+
sourceTag: 'type:feature',
111+
onlyDependOnLibsWithTags: ['type:ui', 'type:domain-logic', 'type:util']
100112
});
101113

102114
depConst.push({
103-
'sourceTag': 'type:ui',
104-
'onlyDependOnLibsWithTags': ['type:domain-logic', 'type:util']
115+
sourceTag: 'type:ui',
116+
onlyDependOnLibsWithTags: ['type:domain-logic', 'type:util']
105117
});
106118

107119
depConst.push({
108-
'sourceTag': 'domain-logic',
109-
'onlyDependOnLibsWithTags': ['type:util']
120+
sourceTag: 'domain-logic',
121+
onlyDependOnLibsWithTags: ['type:util']
110122
});
111123

112124
depConst.push({
113-
'sourceTag': 'domain:shared',
114-
'onlyDependOnLibsWithTags': ['domain:shared']
125+
sourceTag: 'domain:shared',
126+
onlyDependOnLibsWithTags: ['domain:shared']
115127
});
116128

117129
const newText = JSON.stringify(rules, undefined, 2);
118130
host.overwrite('tslint.json', newText);
119-
}
131+
};
120132
}

0 commit comments

Comments
 (0)