Skip to content

Commit

Permalink
allow from and to file exceptions in dep cruiser config
Browse files Browse the repository at this point in the history
  • Loading branch information
electrovir committed Oct 11, 2023
1 parent db8b624 commit 25a8d3a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 29 deletions.
8 changes: 5 additions & 3 deletions configs/configs/dep-cruiser.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {generateDepCruiserConfig} from 'virmator/dist/compiled-base-configs/base
const depCruiserConfig: IConfiguration = generateDepCruiserConfig({
fileExceptions: {
// enter file exceptions by rule name here
'no-orphans': [
'src/index.ts',
],
'no-orphans': {
from: [
'src/index.ts',
],
},
},
omitRules: [
// enter rule names here to omit
Expand Down
16 changes: 0 additions & 16 deletions configs/dep-cruiser.config.ts

This file was deleted.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "virmator",
"version": "9.0.2",
"version": "9.1.0",
"description": "Handle common package configs, commands, and dependencies.",
"keywords": [
"automation",
Expand Down
43 changes: 36 additions & 7 deletions src/compiled-base-configs/base-dep-cruiser.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import type {IConfiguration} from 'dependency-cruiser';
import type {IConfiguration, IForbiddenRuleType} from 'dependency-cruiser';

export function generateDepCruiserConfig({
fileExceptions,
omitRules,
}: {
fileExceptions: Record<string, string[]>;
omitRules: string[];
fileExceptions: Readonly<
Record<
string,
Readonly<
Partial<{
to: ReadonlyArray<string> | undefined;
from: ReadonlyArray<string> | undefined;
}>
>
>
>;
omitRules: ReadonlyArray<string>;
}) {
const forbiddenRules = [
/* rules from the 'recommended' preset: */
Expand Down Expand Up @@ -170,18 +180,37 @@ export function generateDepCruiserConfig({
ruleName,
fileExceptions,
]) => {
const rule = forbiddenRules.find((rule) => rule.name === ruleName);
const rule: IForbiddenRuleType | undefined = forbiddenRules.find(
(rule) => rule.name === ruleName,
);

if (!rule) {
throw new Error(`Rule name not used: '${ruleName}'`);
}

const pathNot = rule.from.pathNot ?? [];
const fromPathNot: string[] = rule.from.pathNot
? Array.isArray(rule.from.pathNot)
? rule.from.pathNot
: [rule.from.pathNot]
: [];
const toPathNot: string[] = rule.to.pathNot
? Array.isArray(rule.to.pathNot)
? rule.to.pathNot
: [rule.to.pathNot]
: [];

pathNot.push(...fileExceptions);
if (fileExceptions.from?.length) {
fromPathNot.push(...fileExceptions.from);
}
if (fileExceptions.to?.length) {
toPathNot.push(...fileExceptions.to);
}

if (!rule.from.pathNot) {
(rule.from as {pathNot: string[]}).pathNot = pathNot;
(rule.from as {pathNot: string[]}).pathNot = fromPathNot;
}
if (!rule.to.pathNot) {
(rule.to as {pathNot: string[]}).pathNot = toPathNot;
}
},
);
Expand Down

0 comments on commit 25a8d3a

Please sign in to comment.