Skip to content

Commit 7055295

Browse files
committedMar 1, 2023
refactor: deps cruiser rules, + generated dependency graph
1 parent 210213f commit 7055295

File tree

5 files changed

+2080
-29
lines changed

5 files changed

+2080
-29
lines changed
 

‎.dependency-cruiser.js

+35-28
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/** @type {import('dependency-cruiser').IConfiguration} */
22

3-
const applicationLayerPaths = [
4-
'application',
3+
const apiLayerPaths = [
54
'controller',
65
'dtos',
76
'request',
87
'response',
98
'dto\\.ts$',
109
'controller\\.ts$',
1110
'resolver\\.ts$',
12-
'\\.service\\.ts$',
1311
];
1412

13+
const applicationLayerPaths = ['application', '\\.service\\.ts$'];
14+
1515
const infrastructureLayerPaths = [
1616
'infrastructure',
1717
'infra',
@@ -30,14 +30,23 @@ const domainLayerPaths = [
3030
module.exports = {
3131
forbidden: [
3232
/* user defined rules */
33+
{
34+
name: 'no-domain-to-api-deps',
35+
comment: 'Domain layer cannot depend on api layer',
36+
severity: 'error',
37+
from: { path: domainLayerPaths },
38+
to: {
39+
path: apiLayerPaths,
40+
},
41+
},
3342
{
3443
name: 'no-domain-to-app-deps',
3544
comment: 'Domain layer cannot depend on application layer',
3645
severity: 'error',
3746
from: { path: domainLayerPaths },
3847
to: {
3948
path: applicationLayerPaths,
40-
pathNot: ['AppRequestContext\\.ts$'],
49+
pathNot: ['AppRequestContext\\.ts'],
4150
},
4251
},
4352
{
@@ -51,18 +60,17 @@ module.exports = {
5160
},
5261
},
5362
{
54-
name: 'no-infra-to-app-deps',
55-
comment: 'Infrastructure layer cannot depend on application layer',
63+
name: 'no-infra-to-api-deps',
64+
comment: 'Infrastructure layer cannot depend on api layer',
5665
severity: 'error',
5766
from: { path: infrastructureLayerPaths },
5867
to: {
59-
path: applicationLayerPaths,
60-
pathNot: ['AppRequestContext\\.ts$'],
68+
path: apiLayerPaths,
6169
},
6270
},
6371
{
64-
name: 'query-deps',
65-
comment: 'Commands and Queries cannot depend on application layer',
72+
name: 'no-command-query-to-api-deps',
73+
comment: 'Commands and Queries cannot depend on api layer',
6674
severity: 'error',
6775
from: {
6876
path: [
@@ -73,23 +81,22 @@ module.exports = {
7381
],
7482
},
7583
to: {
76-
path: applicationLayerPaths,
77-
pathNot: ['AppRequestContext\\.ts$'],
84+
path: apiLayerPaths,
7885
},
7986
},
8087

8188
/* rules from the 'recommended' preset: */
82-
{
83-
name: 'no-circular',
84-
severity: 'warn',
85-
comment:
86-
'This dependency is part of a circular relationship. You might want to revise ' +
87-
'your solution (i.e. use dependency inversion, make sure the modules have a single responsibility) ',
88-
from: {},
89-
to: {
90-
circular: true,
91-
},
92-
},
89+
// {
90+
// name: 'no-circular',
91+
// severity: 'warn',
92+
// comment:
93+
// 'This dependency is part of a circular relationship. You might want to revise ' +
94+
// 'your solution (i.e. use dependency inversion, make sure the modules have a single responsibility) ',
95+
// from: {},
96+
// to: {
97+
// circular: true,
98+
// },
99+
// },
93100
{
94101
name: 'no-orphans',
95102
comment:
@@ -98,7 +105,7 @@ module.exports = {
98105
'add an exception for it in your dependency-cruiser configuration. By default ' +
99106
'this rule does not scrutinize dot-files (e.g. .eslintrc.js), TypeScript declaration ' +
100107
'files (.d.ts), tsconfig.json and some of the babel and webpack configs.',
101-
severity: 'warn',
108+
severity: 'error',
102109
from: {
103110
orphan: true,
104111
pathNot: [
@@ -115,7 +122,7 @@ module.exports = {
115122
comment:
116123
'A module depends on a node core module that has been deprecated. Find an alternative - these are ' +
117124
"bound to exist - node doesn't deprecate lightly.",
118-
severity: 'warn',
125+
severity: 'error',
119126
from: {},
120127
to: {
121128
dependencyTypes: ['core'],
@@ -148,7 +155,7 @@ module.exports = {
148155
comment:
149156
'This module uses a (version of an) npm module that has been deprecated. Either upgrade to a later ' +
150157
'version of that module, or find an alternative. Deprecated modules are a security risk.',
151-
severity: 'warn',
158+
severity: 'error',
152159
from: {},
153160
to: {
154161
dependencyTypes: ['deprecated'],
@@ -184,7 +191,7 @@ module.exports = {
184191
"Likely this module depends on an external ('npm') package that occurs more than once " +
185192
'in your package.json i.e. bot as a devDependencies and in dependencies. This will cause ' +
186193
'maintenance problems later on.',
187-
severity: 'warn',
194+
severity: 'error',
188195
from: {},
189196
to: {
190197
moreThanOneDependencyType: true,
@@ -260,7 +267,7 @@ module.exports = {
260267
'in your package.json. This makes sense if your package is e.g. a plugin, but in ' +
261268
'other cases - maybe not so much. If the use of a peer dependency is intentional ' +
262269
'add an exception to your dependency-cruiser configuration.',
263-
severity: 'warn',
270+
severity: 'error',
264271
from: {},
265272
to: {
266273
dependencyTypes: ['npm-peer'],

‎README.md

+8
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,14 @@ For example:
12231223

12241224
Snippet of code above will prevent your domain layer to depend on the application layer, controllers, etc. Simplified example config: [.dependency-cruiser.js](.dependency-cruiser.js)
12251225

1226+
You can also generate graphs like this:
1227+
1228+
<details>
1229+
<summary>Click to see dependency graph</summary>
1230+
<img src="dependency-graph.svg" alt="Dependency graph">
1231+
</details>
1232+
<br>
1233+
12261234
Example tools:
12271235

12281236
- [Dependency cruiser](https://github.com/sverweij/dependency-cruiser) - Validate and visualize dependencies for JavaScript / TypeScript.

0 commit comments

Comments
 (0)
Failed to load comments.