@brutalway/check-tree 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -1
- package/dist/checkTree.js +29 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -29,6 +29,22 @@ A published package keeps its entry points at the root of `src/`, one file per b
|
|
|
29
29
|
npx check-tree src --root-entries
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
+
### Keep the violations of an existing tree
|
|
33
|
+
|
|
34
|
+
On a codebase that did not follow the layout, the first run prints a lot. Write the current violations to `.check-tree-known-violations.json` and commit it:
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
npx check-tree src --write-known-violations
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
check-tree then skips the violations listed in that file, which it reads from the current folder. It still fails on a violation that is not in the list, and on an entry that no longer happens, which you delete from the file. The file is a JSON array of the messages check-tree prints:
|
|
41
|
+
|
|
42
|
+
```json
|
|
43
|
+
["src/features/player/hooks holds a single child: move it up one level and delete the folder"]
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
A message that names a count, like `holds 3 groups of files`, changes when the count changes: run `--write-known-violations` again and check the diff.
|
|
47
|
+
|
|
32
48
|
### Run the check from code
|
|
33
49
|
|
|
34
50
|
```ts
|
|
@@ -88,7 +104,7 @@ Test files (`*.test.ts`, `*.test.tsx`) are not counted.
|
|
|
88
104
|
|
|
89
105
|
### Output
|
|
90
106
|
|
|
91
|
-
It prints one line per problem and exits with code 1 if it found any.
|
|
107
|
+
It prints one line per problem and exits with code 1 if it found any. The violations listed in `.check-tree-known-violations.json` are left out.
|
|
92
108
|
|
|
93
109
|
### Options
|
|
94
110
|
|
|
@@ -97,3 +113,4 @@ It prints one line per problem and exits with code 1 if it found any.
|
|
|
97
113
|
| folder | `src` |
|
|
98
114
|
| `--categories <list>` | `components`, `orchestrators`, `presenters`, `hooks`, `styles`, `types`, `configs`, `stores`, `routes`, `services`, `queries`, `keys`, `schemas`, `clients`, `workers`, `lib` |
|
|
99
115
|
| `--root-entries` | off |
|
|
116
|
+
| `--write-known-violations` | off |
|
package/dist/checkTree.js
CHANGED
|
@@ -3,20 +3,46 @@ import { t as treeViolations } from "./treeViolations-CEQ5ks7L.js";
|
|
|
3
3
|
import { n as cruiseImports, t as sliceViolations } from "./sliceViolations-DojT6WkL.js";
|
|
4
4
|
import { resolve } from "node:path";
|
|
5
5
|
import { parseArgs } from "node:util";
|
|
6
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
7
|
+
// src/features/known-violations/KnownViolationsConfig.ts
|
|
8
|
+
const KNOWN_VIOLATIONS_FILE = ".check-tree-known-violations.json";
|
|
9
|
+
// src/features/known-violations/knownViolations.ts
|
|
10
|
+
function isStringList(value) {
|
|
11
|
+
return Array.isArray(value) && value.every((entry) => typeof entry === "string");
|
|
12
|
+
}
|
|
13
|
+
function readKnownViolations(file) {
|
|
14
|
+
if (!existsSync(file)) return [];
|
|
15
|
+
const parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
16
|
+
if (!isStringList(parsed)) throw new Error(`${file} holds a JSON array with one string per known violation`);
|
|
17
|
+
return parsed;
|
|
18
|
+
}
|
|
19
|
+
function unknownViolations(violations, file) {
|
|
20
|
+
const known = new Set(readKnownViolations(file));
|
|
21
|
+
const found = new Set(violations);
|
|
22
|
+
return [...violations.filter((violation) => !known.has(violation)), ...[...known].filter((entry) => !found.has(entry)).map((entry) => `${file} lists a violation that no longer happens: ${entry}`)];
|
|
23
|
+
}
|
|
24
|
+
function writeKnownViolations(violations, file) {
|
|
25
|
+
writeFileSync(file, `${JSON.stringify(violations.toSorted(), void 0, 2)}\n`);
|
|
26
|
+
}
|
|
6
27
|
// src/checkTree.ts
|
|
7
28
|
const { values, positionals } = parseArgs({
|
|
8
29
|
allowPositionals: true,
|
|
9
30
|
options: {
|
|
10
31
|
categories: { type: "string" },
|
|
11
|
-
"root-entries": { type: "boolean" }
|
|
32
|
+
"root-entries": { type: "boolean" },
|
|
33
|
+
"write-known-violations": { type: "boolean" }
|
|
12
34
|
}
|
|
13
35
|
});
|
|
14
36
|
const folder = positionals[0] ?? "src";
|
|
15
37
|
const categories = values.categories?.split(",").map((category) => category.trim());
|
|
16
38
|
try {
|
|
17
39
|
const violations = [...treeViolations(resolve(folder), categories, { rootEntries: values["root-entries"] === true }), ...sliceViolations(folder, await cruiseImports(folder))];
|
|
18
|
-
|
|
19
|
-
|
|
40
|
+
if (values["write-known-violations"] === true) writeKnownViolations(violations, KNOWN_VIOLATIONS_FILE);
|
|
41
|
+
else {
|
|
42
|
+
const reported = unknownViolations(violations, KNOWN_VIOLATIONS_FILE);
|
|
43
|
+
for (const violation of reported) console.error(violation);
|
|
44
|
+
process.exitCode = reported.length === 0 ? 0 : 1;
|
|
45
|
+
}
|
|
20
46
|
} catch (error) {
|
|
21
47
|
console.error(error instanceof Error ? error.message : String(error));
|
|
22
48
|
process.exitCode = 1;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@brutalway/check-tree",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Checks a source tree: only the folders the layout names, no empty folder, no category folder holding a single child, stylesheets grouped in styles/, no file of a slice of shared/ used by a single feature, no slice whose files never import each other.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lint",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"unused": "knip"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@brutalway/dependency-cruiser-config": "0.
|
|
47
|
+
"@brutalway/dependency-cruiser-config": "0.10.0",
|
|
48
48
|
"@brutalway/oxlint-config-node": "0.2.1",
|
|
49
49
|
"@brutalway/oxlint-config-node-test": "0.1.0",
|
|
50
50
|
"@brutalway/prettier-config": "0.3.0",
|