@brutalway/check-tree 0.6.0 → 0.7.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 +13 -3
- package/dist/checkTree.js +11 -4
- package/dist/sharedConsumerViolations-CmRppyrD.js +75 -0
- package/dist/sharedViolations.d.ts +1 -0
- package/dist/sharedViolations.js +6 -0
- package/dist/{treeViolations-CeTXg9WP.js → treeViolations-lxAwbM5R.js} +8 -8
- package/dist/treeViolations.js +1 -1
- package/package.json +7 -3
package/README.md
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
# @brutalway/check-tree
|
|
2
2
|
|
|
3
|
-
Checks the folders of a source tree, which a linter does not see: only the folders the layout names, no empty folder, no category folder with a single child,
|
|
3
|
+
Checks the folders of a source tree, which a linter does not see: only the folders the layout names, no empty folder, no category folder with a single child, files of one kind grouped in that kind's folder once there are two of them, and no file in a `shared/` folder that a single feature uses.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
7
7
|
```sh
|
|
8
|
-
npm install --save-dev @brutalway/check-tree
|
|
8
|
+
npm install --save-dev @brutalway/check-tree dependency-cruiser
|
|
9
9
|
npx check-tree src
|
|
10
10
|
```
|
|
11
11
|
|
|
12
|
+
check-tree runs from the folder that holds the dependency-cruiser config (`.dependency-cruiser.cjs` and the like) and reads it to resolve the imports, with the same tsconfig and aliases.
|
|
13
|
+
|
|
12
14
|
## Usage
|
|
13
15
|
|
|
14
16
|
### Use your own category folders
|
|
@@ -35,7 +37,13 @@ import { treeViolations } from '@brutalway/check-tree/treeViolations';
|
|
|
35
37
|
const violations = treeViolations('src');
|
|
36
38
|
```
|
|
37
39
|
|
|
38
|
-
It takes the category folders as second argument and `{ rootEntries: true }` as third, like `--categories` and `--root-entries`.
|
|
40
|
+
It takes the category folders as second argument and `{ rootEntries: true }` as third, like `--categories` and `--root-entries`. The check on `shared/` reads the imports, so it has its own export:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { sharedViolations } from '@brutalway/check-tree/sharedViolations';
|
|
44
|
+
|
|
45
|
+
const violations = await sharedViolations('src');
|
|
46
|
+
```
|
|
39
47
|
|
|
40
48
|
## Reference
|
|
41
49
|
|
|
@@ -69,6 +77,8 @@ It takes the category folders as second argument and `{ rootEntries: true }` as
|
|
|
69
77
|
| `stores/` | `xStore.ts` |
|
|
70
78
|
| `lib/` | any other `.ts` file, except `xContent.ts`, `xEnv.ts` and `.d.ts` |
|
|
71
79
|
|
|
80
|
+
- a file of a `shared/` folder is used by at least two consumers. For `src/shared/`, a consumer is a feature or a shared feature, a sub-feature counting as the feature it sits in. For the `shared/` of a feature or a shared feature, a consumer is one of its sub-features, one of its sub shared features, or its own files outside them. A file imported by another file of the same `shared/` gets the consumers of that file. A test counts for the feature it sits in, since moving the file would make that test import another feature, and a test next to the file in `shared/` adds no consumer. A file of `src/shared/` used only by `app/` or by the entry files of `src/` passes. A file with a single consumer gets `src/shared/lib/format.ts is only used by src/features/cart: move it into src/features/cart`.
|
|
81
|
+
|
|
72
82
|
So a feature with two components puts them in `components/`, and a `components/` folder with two presenters puts them in `presenters/`. The `components/` rule does not apply inside `components/`, `orchestrators/` or `presenters/`.
|
|
73
83
|
|
|
74
84
|
Test files (`*.test.ts`, `*.test.tsx`) are not counted.
|
package/dist/checkTree.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { t as treeViolations } from "./treeViolations-
|
|
2
|
+
import { t as treeViolations } from "./treeViolations-lxAwbM5R.js";
|
|
3
|
+
import { n as cruiseImports, t as sharedConsumerViolations } from "./sharedConsumerViolations-CmRppyrD.js";
|
|
3
4
|
import { resolve } from "node:path";
|
|
4
5
|
import { parseArgs } from "node:util";
|
|
5
6
|
// src/checkTree.ts
|
|
@@ -10,8 +11,14 @@ const { values, positionals } = parseArgs({
|
|
|
10
11
|
"root-entries": { type: "boolean" }
|
|
11
12
|
}
|
|
12
13
|
});
|
|
14
|
+
const folder = positionals[0] ?? "src";
|
|
13
15
|
const categories = values.categories?.split(",").map((category) => category.trim());
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
try {
|
|
17
|
+
const violations = [...treeViolations(resolve(folder), categories, { rootEntries: values["root-entries"] === true }), ...sharedConsumerViolations(folder, await cruiseImports(folder))];
|
|
18
|
+
for (const violation of violations) console.error(violation);
|
|
19
|
+
process.exitCode = violations.length === 0 ? 0 : 1;
|
|
20
|
+
} catch (error) {
|
|
21
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
22
|
+
process.exitCode = 1;
|
|
23
|
+
}
|
|
17
24
|
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { cruise } from "dependency-cruiser";
|
|
3
|
+
import extractDepcruiseOptions from "dependency-cruiser/config-utl/extract-depcruise-options";
|
|
4
|
+
import extractTSConfig from "dependency-cruiser/config-utl/extract-ts-config";
|
|
5
|
+
// src/features/shared-consumers/lib/cruiseImports.ts
|
|
6
|
+
const CONFIG_FILES = [
|
|
7
|
+
".dependency-cruiser.js",
|
|
8
|
+
".dependency-cruiser.cjs",
|
|
9
|
+
".dependency-cruiser.mjs",
|
|
10
|
+
".dependency-cruiser.json"
|
|
11
|
+
];
|
|
12
|
+
async function cruiseImports(root) {
|
|
13
|
+
const configFile = CONFIG_FILES.find((file) => existsSync(file));
|
|
14
|
+
if (configFile === void 0) throw new Error(`check-tree reads the dependency-cruiser config to resolve imports, and the current folder has none of ${CONFIG_FILES.join(", ")}`);
|
|
15
|
+
const options = await extractDepcruiseOptions(`./${configFile}`);
|
|
16
|
+
const tsConfigFile = options.tsConfig?.fileName;
|
|
17
|
+
const result = await cruise([root], {
|
|
18
|
+
...options,
|
|
19
|
+
validate: false,
|
|
20
|
+
tsPreCompilationDeps: true
|
|
21
|
+
}, {}, { tsConfig: tsConfigFile === void 0 ? void 0 : extractTSConfig(tsConfigFile) });
|
|
22
|
+
if (typeof result.output === "string") throw new Error(result.output);
|
|
23
|
+
return result.output.modules.map(({ source, dependencies }) => ({
|
|
24
|
+
source,
|
|
25
|
+
imports: dependencies.map(({ resolved }) => resolved)
|
|
26
|
+
}));
|
|
27
|
+
}
|
|
28
|
+
// src/features/shared-consumers/SharedConsumersConfig.ts
|
|
29
|
+
const TEST_CODE = /\.test\.tsx?$/u;
|
|
30
|
+
const SLICE = /^(?:features|shared-features)\/[^/]+/u;
|
|
31
|
+
// src/features/shared-consumers/lib/consumerSlots.ts
|
|
32
|
+
function slotOf(owner, importer) {
|
|
33
|
+
if (!importer.startsWith(`${owner}/`)) return importer;
|
|
34
|
+
const slice = SLICE.exec(importer.slice(owner.length + 1));
|
|
35
|
+
return slice === null ? owner : `${owner}/${slice[0]}`;
|
|
36
|
+
}
|
|
37
|
+
function importersByModule(modules) {
|
|
38
|
+
const importers = /* @__PURE__ */ new Map();
|
|
39
|
+
for (const { source, imports } of modules) for (const target of imports) importers.set(target, [...importers.get(target) ?? [], source]);
|
|
40
|
+
return importers;
|
|
41
|
+
}
|
|
42
|
+
function consumerSlots(modules) {
|
|
43
|
+
const importers = importersByModule(modules);
|
|
44
|
+
const slotsByModule = /* @__PURE__ */ new Map();
|
|
45
|
+
return function slots(path, owner) {
|
|
46
|
+
const known = slotsByModule.get(path);
|
|
47
|
+
if (known !== void 0) return known;
|
|
48
|
+
const found = /* @__PURE__ */ new Set();
|
|
49
|
+
slotsByModule.set(path, found);
|
|
50
|
+
for (const importer of importers.get(path) ?? []) if (importer.startsWith(`${owner}/shared/`)) for (const slot of slots(importer, owner)) found.add(slot);
|
|
51
|
+
else found.add(slotOf(owner, importer));
|
|
52
|
+
return found;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
// src/features/shared-consumers/lib/sharedConsumerViolations.ts
|
|
56
|
+
function ownerOf(path) {
|
|
57
|
+
const index = path.lastIndexOf("/shared/");
|
|
58
|
+
return index === -1 ? void 0 : path.slice(0, index);
|
|
59
|
+
}
|
|
60
|
+
function violation(path, owner, slot) {
|
|
61
|
+
if (slot === owner) return `${path} is only used by ${owner} outside its sub-features: move it out of shared/ into the kind folders of ${owner}`;
|
|
62
|
+
return `${path} is only used by ${slot}: move it into ${slot}`;
|
|
63
|
+
}
|
|
64
|
+
function sharedConsumerViolations(root, modules) {
|
|
65
|
+
const slots = consumerSlots(modules);
|
|
66
|
+
return modules.flatMap(({ source }) => {
|
|
67
|
+
const owner = ownerOf(source);
|
|
68
|
+
if (owner === void 0 || TEST_CODE.test(source) || !source.startsWith(`${root}/`)) return [];
|
|
69
|
+
const [slot, ...others] = slots(source, owner);
|
|
70
|
+
if (slot === void 0 || others.length > 0) return [];
|
|
71
|
+
if (owner === root && !SLICE.test(slot.slice(root.length + 1))) return [];
|
|
72
|
+
return [violation(source, owner, slot)];
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
export { cruiseImports as n, sharedConsumerViolations as t };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function sharedViolations(root: string): Promise<string[]>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { n as cruiseImports, t as sharedConsumerViolations } from "./sharedConsumerViolations-CmRppyrD.js";
|
|
2
|
+
// src/sharedViolations.ts
|
|
3
|
+
async function sharedViolations(root) {
|
|
4
|
+
return sharedConsumerViolations(root, await cruiseImports(root));
|
|
5
|
+
}
|
|
6
|
+
export { sharedViolations };
|
|
@@ -173,15 +173,15 @@ function childLayer(layer, path, categories) {
|
|
|
173
173
|
}
|
|
174
174
|
// src/features/tree-violations/treeViolations.ts
|
|
175
175
|
function groupViolations(name, folderName, listing, categoryFolders) {
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
if (!categoryFolders.has(folder) || !appliesIn(folderName))
|
|
176
|
+
const holdsFilesOnly = categoryFolders.has(folderName) && listing.folders.size === 0;
|
|
177
|
+
return FILE_GROUPS.flatMap(({ folder, belongs, appliesIn }) => {
|
|
178
|
+
if (!categoryFolders.has(folder) || !appliesIn(folderName)) return [];
|
|
179
179
|
const loose = listing.files.filter(belongs);
|
|
180
|
-
if (loose.length === 0)
|
|
181
|
-
if (listing.folders.has(folder))
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
180
|
+
if (loose.length === 0 || holdsFilesOnly && loose.length === listing.files.length) return [];
|
|
181
|
+
if (listing.folders.has(folder)) return [`${name} has a ${folder}/ folder: move ${loose.join(", ")} into it`];
|
|
182
|
+
if (loose.length === 1) return [];
|
|
183
|
+
return [`${name} holds ${loose.length} ${folder} files (${loose.join(", ")}): move them into ${folder}/`];
|
|
184
|
+
});
|
|
185
185
|
}
|
|
186
186
|
function folderListing(entries) {
|
|
187
187
|
return {
|
package/dist/treeViolations.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { t as treeViolations } from "./treeViolations-
|
|
1
|
+
export { t as treeViolations } from "./treeViolations-lxAwbM5R.js";
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@brutalway/check-tree",
|
|
3
|
-
"version": "0.
|
|
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
|
|
3
|
+
"version": "0.7.0",
|
|
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 shared/ used by a single feature.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lint",
|
|
7
7
|
"folder-structure",
|
|
@@ -28,7 +28,8 @@
|
|
|
28
28
|
"check-tree": "./dist/checkTree.js"
|
|
29
29
|
},
|
|
30
30
|
"exports": {
|
|
31
|
-
"./treeViolations": "./dist/treeViolations.js"
|
|
31
|
+
"./treeViolations": "./dist/treeViolations.js",
|
|
32
|
+
"./sharedViolations": "./dist/sharedViolations.js"
|
|
32
33
|
},
|
|
33
34
|
"scripts": {
|
|
34
35
|
"build": "tsdown",
|
|
@@ -70,5 +71,8 @@
|
|
|
70
71
|
"eslint-plugin-no-comments": {
|
|
71
72
|
"minimatch": "^11.0.1"
|
|
72
73
|
}
|
|
74
|
+
},
|
|
75
|
+
"peerDependencies": {
|
|
76
|
+
"dependency-cruiser": ">=18"
|
|
73
77
|
}
|
|
74
78
|
}
|