@brutalway/check-tree 0.12.0 → 0.13.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 +9 -3
- package/dist/checkTree.js +26 -11
- package/dist/{sharedFeatureViolations-BbyKWb38.js → sharedFeatureViolations-CG7GEvs8.js} +11 -3
- package/dist/sharedViolations.js +2 -2
- package/dist/{treeViolations-BjinY4OB.js → treeViolations-DCxZDgGS.js} +38 -8
- package/dist/treeViolations.js +6 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,13 +49,19 @@ On a codebase that did not follow the layout, the first run prints a lot. Write
|
|
|
49
49
|
npx check-tree src --write-known-violations
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
-
check-tree then skips the violations listed in that file, which it reads from the current folder.
|
|
52
|
+
check-tree then skips the violations listed in that file, which it reads from the current folder. Each entry names the rule and the folder or file it is about, plus a count for the rules that have one:
|
|
53
53
|
|
|
54
54
|
```json
|
|
55
|
-
[
|
|
55
|
+
[
|
|
56
|
+
{ "rule": "single-child", "path": "src/features/player/hooks" },
|
|
57
|
+
{ "rule": "shared-feature-groups", "path": "src/shared/ui", "count": 9 },
|
|
58
|
+
{ "rule": "kind-folder", "path": "src/features/player/hooks", "count": 3 }
|
|
59
|
+
]
|
|
56
60
|
```
|
|
57
61
|
|
|
58
|
-
|
|
62
|
+
An entry stays known while the same rule fires on the same path with a count no higher than the one written. Adding a file to `src/shared/ui` linked to one of its groups, or merging two of its groups, changes the message without making a new violation. A tenth group, or a fourth hook to move into `hooks/`, is reported. check-tree also fails on an entry whose rule no longer fires on its path: delete it from the file.
|
|
63
|
+
|
|
64
|
+
The rules are `empty-folder`, `single-child`, `not-a-layer`, `not-a-kind-folder`, `kind-folder` (files to move into a kind folder, the path being that folder), `shared-root-files`, `single-consumer` and `shared-feature-groups`. Up to 0.12, the file held the printed messages: check-tree now refuses that format, and `--write-known-violations` rewrites it.
|
|
59
65
|
|
|
60
66
|
### Run the check from code
|
|
61
67
|
|
package/dist/checkTree.js
CHANGED
|
@@ -1,31 +1,46 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { t as treeViolations } from "./treeViolations-
|
|
3
|
-
import { n as cruiseImports, t as sharedFeatureViolations } from "./sharedFeatureViolations-
|
|
2
|
+
import { t as treeViolations } from "./treeViolations-DCxZDgGS.js";
|
|
3
|
+
import { n as cruiseImports, t as sharedFeatureViolations } from "./sharedFeatureViolations-CG7GEvs8.js";
|
|
4
4
|
import { resolve } from "node:path";
|
|
5
5
|
import { parseArgs } from "node:util";
|
|
6
6
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
7
7
|
// src/features/known-violations/KnownViolationsConfig.ts
|
|
8
8
|
const KNOWN_VIOLATIONS_FILE = ".check-tree-known-violations.json";
|
|
9
|
-
// src/shared/string-lists/isStringList.ts
|
|
10
|
-
function isStringList(value) {
|
|
11
|
-
return Array.isArray(value) && value.every((entry) => typeof entry === "string");
|
|
12
|
-
}
|
|
13
9
|
// src/features/known-violations/knownViolations.ts
|
|
10
|
+
function isKnownViolation(value) {
|
|
11
|
+
if (typeof value !== "object" || value === null) return false;
|
|
12
|
+
if (!("rule" in value) || !("path" in value)) return false;
|
|
13
|
+
const count = "count" in value ? value.count : void 0;
|
|
14
|
+
return typeof value.rule === "string" && typeof value.path === "string" && (count === void 0 || typeof count === "number");
|
|
15
|
+
}
|
|
14
16
|
function readKnownViolations(file) {
|
|
15
17
|
if (!existsSync(file)) return [];
|
|
16
18
|
const parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
17
|
-
if (!
|
|
19
|
+
if (!Array.isArray(parsed) || !parsed.every(isKnownViolation)) throw new Error(`${file} holds a JSON array of { "rule", "path", "count" } objects, count being optional: run check-tree with --write-known-violations to write it in this format`);
|
|
18
20
|
return parsed;
|
|
19
21
|
}
|
|
22
|
+
function covers(entry, violation) {
|
|
23
|
+
return entry.rule === violation.rule && entry.path === violation.path && (violation.count ?? 0) <= (entry.count ?? 0);
|
|
24
|
+
}
|
|
20
25
|
function unknownViolations(violations, file) {
|
|
21
|
-
const known =
|
|
22
|
-
|
|
23
|
-
return [...violations.filter((violation) => !known.has(violation)), ...[...known].filter((entry) => !found.has(entry)).map((entry) => `${file} lists a violation that no longer happens: ${entry}`)];
|
|
26
|
+
const known = readKnownViolations(file);
|
|
27
|
+
return [...violations.filter((violation) => !known.some((entry) => covers(entry, violation))).map(({ message }) => message), ...known.filter((entry) => !violations.some(({ rule, path }) => rule === entry.rule && path === entry.path)).map(({ rule, path }) => `${file} lists ${rule} for ${path}, which no longer happens: delete the entry`)];
|
|
24
28
|
}
|
|
25
29
|
function writeKnownViolations(violations, file) {
|
|
26
|
-
|
|
30
|
+
const entries = violations.map(({ rule, path, count }) => count === void 0 ? {
|
|
31
|
+
rule,
|
|
32
|
+
path
|
|
33
|
+
} : {
|
|
34
|
+
rule,
|
|
35
|
+
path,
|
|
36
|
+
count
|
|
37
|
+
}).toSorted((a, b) => a.path.localeCompare(b.path) || a.rule.localeCompare(b.rule));
|
|
38
|
+
writeFileSync(file, `${JSON.stringify(entries, void 0, 2)}\n`);
|
|
27
39
|
}
|
|
28
40
|
// src/features/tree-settings/readTreeSettings.ts
|
|
41
|
+
function isStringList(value) {
|
|
42
|
+
return Array.isArray(value) && value.every((entry) => typeof entry === "string");
|
|
43
|
+
}
|
|
29
44
|
const SETTING_CHECKS = /* @__PURE__ */ new Map([
|
|
30
45
|
["categories", isStringList],
|
|
31
46
|
["rootEntries", (value) => typeof value === "boolean"],
|
|
@@ -85,8 +85,11 @@ function sharedFeatureOf(path) {
|
|
|
85
85
|
}
|
|
86
86
|
// src/features/shared-consumers/lib/sharedConsumerViolations.ts
|
|
87
87
|
function violation(path, owner, slot) {
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
return {
|
|
89
|
+
rule: "single-consumer",
|
|
90
|
+
path,
|
|
91
|
+
message: slot === owner ? `${path} is only used by ${owner} outside its sub-features: move it out of shared/ into the kind folders of ${owner}` : `${path} is only used by ${slot}: move it into ${slot}`
|
|
92
|
+
};
|
|
90
93
|
}
|
|
91
94
|
function sharedConsumerViolations(checkedFolder, modules, platforms) {
|
|
92
95
|
const { root, shown } = checkedPaths(checkedFolder);
|
|
@@ -151,7 +154,12 @@ function sharedFeatureGroupViolations(checkedFolder, modules, platforms) {
|
|
|
151
154
|
return [...groupsBySharedFeature(folders, linkedGroups(folders.keys(), links))].flatMap(([folder, groups]) => {
|
|
152
155
|
if (groups.size < 2) return [];
|
|
153
156
|
const listed = [...groups.values()].map((files) => `(${files.join(", ")})`).join(" ");
|
|
154
|
-
return [
|
|
157
|
+
return [{
|
|
158
|
+
rule: "shared-feature-groups",
|
|
159
|
+
path: shown(folder),
|
|
160
|
+
count: groups.size,
|
|
161
|
+
message: `${shown(folder)} holds ${groups.size} groups of files that never import each other ${listed}: split it into one shared feature per group`
|
|
162
|
+
}];
|
|
155
163
|
});
|
|
156
164
|
}
|
|
157
165
|
// src/features/shared-consumers/lib/sharedFeatureViolations.ts
|
package/dist/sharedViolations.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { n as cruiseImports, t as sharedFeatureViolations } from "./sharedFeatureViolations-
|
|
1
|
+
import { n as cruiseImports, t as sharedFeatureViolations } from "./sharedFeatureViolations-CG7GEvs8.js";
|
|
2
2
|
// src/sharedViolations.ts
|
|
3
3
|
async function sharedViolations(root, platforms) {
|
|
4
|
-
return sharedFeatureViolations(root, await cruiseImports(root), platforms);
|
|
4
|
+
return sharedFeatureViolations(root, await cruiseImports(root), platforms).map(({ message }) => message);
|
|
5
5
|
}
|
|
6
6
|
export { sharedViolations };
|
|
@@ -131,7 +131,11 @@ function kindFolder(path, categories, reason) {
|
|
|
131
131
|
};
|
|
132
132
|
return {
|
|
133
133
|
layer: "free",
|
|
134
|
-
violations: [
|
|
134
|
+
violations: [{
|
|
135
|
+
rule: "not-a-kind-folder",
|
|
136
|
+
path,
|
|
137
|
+
message: `${path} is not a kind folder: ${reason}`
|
|
138
|
+
}]
|
|
135
139
|
};
|
|
136
140
|
}
|
|
137
141
|
function sourceChild(path) {
|
|
@@ -142,7 +146,11 @@ function sourceChild(path) {
|
|
|
142
146
|
};
|
|
143
147
|
return {
|
|
144
148
|
layer: "free",
|
|
145
|
-
violations: [
|
|
149
|
+
violations: [{
|
|
150
|
+
rule: "not-a-layer",
|
|
151
|
+
path,
|
|
152
|
+
message: `${path} is not a layer: ${dirname(path)}/ holds app/, features/ and shared/ next to its entry points, move it into one of them`
|
|
153
|
+
}]
|
|
146
154
|
};
|
|
147
155
|
}
|
|
148
156
|
function featureChild(path, categories) {
|
|
@@ -175,10 +183,25 @@ function filesCheck(layer, holdsEntries) {
|
|
|
175
183
|
if (layer === "sharedFeatures") return "sharedFeatures";
|
|
176
184
|
return layer === "router" || holdsEntries ? "none" : "groups";
|
|
177
185
|
}
|
|
186
|
+
// src/features/tree-violations/features/layout/lib/kindFolderViolation.ts
|
|
187
|
+
function kindFolderViolation(name, folder, loose, folderExists) {
|
|
188
|
+
const message = folderExists ? `${name} has a ${folder}/ folder: move ${loose.join(", ")} into it` : `${name} holds ${loose.length} ${folder} files (${loose.join(", ")}): move them into ${folder}/`;
|
|
189
|
+
return {
|
|
190
|
+
rule: "kind-folder",
|
|
191
|
+
path: `${name}/${folder}`,
|
|
192
|
+
count: loose.length,
|
|
193
|
+
message
|
|
194
|
+
};
|
|
195
|
+
}
|
|
178
196
|
// src/features/tree-violations/features/layout/lib/sharedRootFileViolations.ts
|
|
179
197
|
function sharedRootFileViolations(name, files) {
|
|
180
198
|
if (files.length === 0) return [];
|
|
181
|
-
return [
|
|
199
|
+
return [{
|
|
200
|
+
rule: "shared-root-files",
|
|
201
|
+
path: name,
|
|
202
|
+
count: files.length,
|
|
203
|
+
message: `${name} holds ${files.join(", ")}: shared/ holds one folder per shared feature, move each file into the shared feature of its concept`
|
|
204
|
+
}];
|
|
182
205
|
}
|
|
183
206
|
// src/features/tree-violations/treeViolations.ts
|
|
184
207
|
function groupViolations(name, folderName, listing, categoryFolders) {
|
|
@@ -187,9 +210,8 @@ function groupViolations(name, folderName, listing, categoryFolders) {
|
|
|
187
210
|
if (!categoryFolders.has(folder) || !appliesIn(folderName)) return [];
|
|
188
211
|
const loose = listing.files.filter((file) => belongs(listing.baseOf(file)));
|
|
189
212
|
if (loose.length === 0 || holdsFilesOnly && loose.length === listing.files.length) return [];
|
|
190
|
-
if (listing.folders.has(folder)
|
|
191
|
-
|
|
192
|
-
return [`${name} holds ${loose.length} ${folder} files (${loose.join(", ")}): move them into ${folder}/`];
|
|
213
|
+
if (!listing.folders.has(folder) && new Set(loose.map(listing.baseOf)).size === 1) return [];
|
|
214
|
+
return [kindFolderViolation(name, folder, loose, listing.folders.has(folder))];
|
|
193
215
|
});
|
|
194
216
|
}
|
|
195
217
|
function folderListing(entries, platforms) {
|
|
@@ -200,8 +222,16 @@ function folderListing(entries, platforms) {
|
|
|
200
222
|
};
|
|
201
223
|
}
|
|
202
224
|
function shapeViolations(name, count, isCategory) {
|
|
203
|
-
if (count === 0) return [
|
|
204
|
-
|
|
225
|
+
if (count === 0) return [{
|
|
226
|
+
rule: "empty-folder",
|
|
227
|
+
path: name,
|
|
228
|
+
message: `${name} is empty: delete it`
|
|
229
|
+
}];
|
|
230
|
+
if (count === 1 && isCategory) return [{
|
|
231
|
+
rule: "single-child",
|
|
232
|
+
path: name,
|
|
233
|
+
message: `${name} holds a single child: move it up one level and delete the folder`
|
|
234
|
+
}];
|
|
205
235
|
return [];
|
|
206
236
|
}
|
|
207
237
|
function folderViolations(folder, name, scan, check) {
|
package/dist/treeViolations.js
CHANGED
|
@@ -1 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import { t as treeViolations$1 } from "./treeViolations-DCxZDgGS.js";
|
|
2
|
+
// src/treeViolations.ts
|
|
3
|
+
function treeViolations(root, categoryFolders, options) {
|
|
4
|
+
return treeViolations$1(root, categoryFolders, options).map(({ message }) => message);
|
|
5
|
+
}
|
|
6
|
+
export { treeViolations };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@brutalway/check-tree",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "Checks a source tree: only the folders the layout names, no empty folder, no kind folder holding a single child, stylesheets grouped in styles/, no file of a shared feature used by a single feature, no shared feature whose files never import each other.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lint",
|