@brutalway/check-tree 0.5.0 → 0.6.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
CHANGED
|
@@ -1,7 +1,51 @@
|
|
|
1
1
|
# @brutalway/check-tree
|
|
2
2
|
|
|
3
|
-
Checks the folders of a source tree, which a linter does not see:
|
|
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, and files of one kind grouped in that kind's folder once there are two of them.
|
|
4
4
|
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install --save-dev @brutalway/check-tree
|
|
9
|
+
npx check-tree src
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
### Use your own category folders
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
npx check-tree src --categories components,hooks,utils
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
These are the kind folders the layout accepts, and the grouping rule only applies to the kinds listed in the table below that are also in this list.
|
|
21
|
+
|
|
22
|
+
### Keep the entry points of a published package
|
|
23
|
+
|
|
24
|
+
A published package keeps its entry points at the root of `src/`, one file per bin or export, whatever their kind. `--root-entries` leaves the files at the root of the checked folder where they are, and checks everything below as usual:
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
npx check-tree src --root-entries
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Run the check from code
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { treeViolations } from '@brutalway/check-tree/treeViolations';
|
|
34
|
+
|
|
35
|
+
const violations = treeViolations('src');
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
It takes the category folders as second argument and `{ rootEntries: true }` as third, like `--categories` and `--root-entries`.
|
|
39
|
+
|
|
40
|
+
## Reference
|
|
41
|
+
|
|
42
|
+
### Rules
|
|
43
|
+
|
|
44
|
+
- a folder has a name the layout gives it:
|
|
45
|
+
- `src/` holds `app/`, `features/`, `shared-features/` and `shared/`, next to its entry files, and `app/` is not checked
|
|
46
|
+
- `src/shared/` holds kind folders only (`lib/`, `types/`, `configs/`...), since it has no domain
|
|
47
|
+
- a feature (`features/<x>/`) or a shared feature (`shared-features/<x>/`), at any depth, holds kind folders, `features/` for its sub-features, `shared-features/` for the business code they share and `shared/` for the code with no domain
|
|
48
|
+
- a kind folder holds kind folders only, like `presenters/` inside `components/`
|
|
5
49
|
- no empty folder
|
|
6
50
|
- a category folder (`components`, `hooks`, `stores`…) does not hold a single child: move the child up and delete the folder
|
|
7
51
|
- files of one kind stay at the root of their folder while there is one of them, and move into the kind's folder from two on. A folder that has that kind's folder keeps none of them next to it. The kinds are recognized by their name:
|
|
@@ -29,33 +73,14 @@ So a feature with two components puts them in `components/`, and a `components/`
|
|
|
29
73
|
|
|
30
74
|
Test files (`*.test.ts`, `*.test.tsx`) are not counted.
|
|
31
75
|
|
|
32
|
-
|
|
33
|
-
npm install --save-dev @brutalway/check-tree
|
|
34
|
-
npx check-tree src
|
|
35
|
-
```
|
|
76
|
+
### Output
|
|
36
77
|
|
|
37
78
|
It prints one line per problem and exits with code 1 if it found any.
|
|
38
79
|
|
|
39
|
-
|
|
80
|
+
### Options
|
|
40
81
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
A published package keeps its entry points at the root of `src/`, one file per bin or export, whatever their kind. `--root-entries` leaves the files at the root of the checked folder where they are, and checks everything below as usual:
|
|
48
|
-
|
|
49
|
-
```sh
|
|
50
|
-
npx check-tree src --root-entries
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
The check is also exported as a function:
|
|
54
|
-
|
|
55
|
-
```ts
|
|
56
|
-
import { treeViolations } from '@brutalway/check-tree/treeViolations';
|
|
57
|
-
|
|
58
|
-
const violations = treeViolations('src');
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
It takes the category folders as second argument and `{ rootEntries: true }` as third, like `--categories` and `--root-entries`.
|
|
82
|
+
| Option | Default |
|
|
83
|
+
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
84
|
+
| folder | `src` |
|
|
85
|
+
| `--categories <list>` | `components`, `orchestrators`, `presenters`, `hooks`, `styles`, `types`, `configs`, `stores`, `routes`, `services`, `queries`, `keys`, `schemas`, `clients`, `workers`, `lib` |
|
|
86
|
+
| `--root-entries` | off |
|
package/dist/checkTree.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { basename, join, relative } from "node:path";
|
|
1
|
+
import { basename, dirname, join, relative } from "node:path";
|
|
2
2
|
import { readdirSync } from "node:fs";
|
|
3
|
-
// src/features/tree-violations/
|
|
3
|
+
// src/features/tree-violations/TreeViolationsConfig.ts
|
|
4
4
|
const DEFAULT_CATEGORY_FOLDERS = [
|
|
5
5
|
"components",
|
|
6
6
|
"orchestrators",
|
|
@@ -116,31 +116,100 @@ const FILE_GROUPS = [
|
|
|
116
116
|
appliesIn: outsideOwnFolder("lib")
|
|
117
117
|
}
|
|
118
118
|
];
|
|
119
|
-
|
|
119
|
+
// src/features/tree-violations/features/layout/childLayer.ts
|
|
120
|
+
const SOURCE_LAYERS = /* @__PURE__ */ new Map([
|
|
121
|
+
["app", "free"],
|
|
122
|
+
["features", "features"],
|
|
123
|
+
["shared-features", "features"],
|
|
124
|
+
["shared", "shared"]
|
|
125
|
+
]);
|
|
126
|
+
const FEATURE_LAYERS = /* @__PURE__ */ new Map([
|
|
127
|
+
["features", "features"],
|
|
128
|
+
["shared-features", "features"],
|
|
129
|
+
["shared", "shared"]
|
|
130
|
+
]);
|
|
131
|
+
function kindFolder(path, categories, reason) {
|
|
132
|
+
if (categories.has(basename(path))) return {
|
|
133
|
+
layer: "kind",
|
|
134
|
+
violations: []
|
|
135
|
+
};
|
|
136
|
+
return {
|
|
137
|
+
layer: "free",
|
|
138
|
+
violations: [`${path} is not a kind folder: ${reason}`]
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
function sourceChild(path) {
|
|
142
|
+
const layer = SOURCE_LAYERS.get(basename(path));
|
|
143
|
+
if (layer !== void 0) return {
|
|
144
|
+
layer,
|
|
145
|
+
violations: []
|
|
146
|
+
};
|
|
147
|
+
return {
|
|
148
|
+
layer: "free",
|
|
149
|
+
violations: [`${path} is not a layer: ${dirname(path)}/ holds app/, features/, shared-features/ and shared/ next to its entry files, move it into one of them`]
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
function featureChild(path, categories) {
|
|
153
|
+
const layer = FEATURE_LAYERS.get(basename(path));
|
|
154
|
+
if (layer !== void 0) return {
|
|
155
|
+
layer,
|
|
156
|
+
violations: []
|
|
157
|
+
};
|
|
158
|
+
return kindFolder(path, categories, "a feature holds kind folders, features/ for its sub-features, shared-features/ for the business code they share and shared/, move it into features/ as a sub-feature or spread its files into kind folders");
|
|
159
|
+
}
|
|
160
|
+
function childLayer(layer, path, categories) {
|
|
161
|
+
if (layer === "source") return sourceChild(path);
|
|
162
|
+
if (layer === "features") return {
|
|
163
|
+
layer: "feature",
|
|
164
|
+
violations: []
|
|
165
|
+
};
|
|
166
|
+
if (layer === "feature") return featureChild(path, categories);
|
|
167
|
+
if (layer === "shared") return kindFolder(path, categories, `${dirname(path)}/ holds no domain, move its files into lib/, types/, configs/... or into a shared feature`);
|
|
168
|
+
if (layer === "kind") return kindFolder(path, categories, `${dirname(path)}/ holds its files and kind folders only`);
|
|
169
|
+
return {
|
|
170
|
+
layer: "free",
|
|
171
|
+
violations: []
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
// src/features/tree-violations/treeViolations.ts
|
|
175
|
+
function groupViolations(name, folderName, listing, categoryFolders) {
|
|
120
176
|
const violations = [];
|
|
121
177
|
for (const { folder, belongs, appliesIn } of FILE_GROUPS) {
|
|
122
178
|
if (!categoryFolders.has(folder) || !appliesIn(folderName)) continue;
|
|
123
|
-
const loose =
|
|
179
|
+
const loose = listing.files.filter(belongs);
|
|
124
180
|
if (loose.length === 0) continue;
|
|
125
|
-
if (
|
|
181
|
+
if (listing.folders.has(folder)) violations.push(`${name} has a ${folder}/ folder: move ${loose.join(", ")} into it`);
|
|
126
182
|
else if (loose.length > 1) violations.push(`${name} holds ${loose.length} ${folder} files (${loose.join(", ")}): move them into ${folder}/`);
|
|
127
183
|
}
|
|
128
184
|
return violations;
|
|
129
185
|
}
|
|
186
|
+
function folderListing(entries) {
|
|
187
|
+
return {
|
|
188
|
+
files: entries.filter((entry) => entry.isFile()).map((entry) => entry.name).toSorted(),
|
|
189
|
+
folders: new Set(entries.filter((entry) => entry.isDirectory()).map((entry) => entry.name))
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
function shapeViolations(name, count, isCategory) {
|
|
193
|
+
if (count === 0) return [`${name} is empty: delete it`];
|
|
194
|
+
if (count === 1 && isCategory) return [`${name} holds a single child: move it up one level and delete the folder`];
|
|
195
|
+
return [];
|
|
196
|
+
}
|
|
130
197
|
function folderViolations(folder, name, categoryFolders, groupsFiles) {
|
|
131
198
|
const children = readdirSync(folder, { withFileTypes: true }).filter((entry) => !TEST_FILE.test(entry.name));
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
if (children.length === 1 && categoryFolders.has(basename(folder))) violations.push(`${name} holds a single child: move it up one level and delete the folder`);
|
|
137
|
-
return groupsFiles ? [...violations, ...groupViolations(name, basename(folder), fileNames, folderNames, categoryFolders)] : violations;
|
|
199
|
+
const violations = shapeViolations(name, children.length, categoryFolders.has(basename(folder)));
|
|
200
|
+
if (!groupsFiles) return violations;
|
|
201
|
+
const listing = folderListing(children);
|
|
202
|
+
return [...violations, ...groupViolations(name, basename(folder), listing, categoryFolders)];
|
|
138
203
|
}
|
|
139
204
|
function treeViolations(root, categoryFolders = DEFAULT_CATEGORY_FOLDERS, options = {}) {
|
|
140
205
|
const categories = new Set(categoryFolders);
|
|
141
|
-
function walk(folder) {
|
|
142
|
-
|
|
206
|
+
function walk(folder, layer) {
|
|
207
|
+
const name = relative(join(root, ".."), folder);
|
|
208
|
+
return [...folderViolations(folder, name, categories, folder !== root || options.rootEntries !== true), ...readdirSync(folder, { withFileTypes: true }).filter((entry) => entry.isDirectory()).flatMap((entry) => {
|
|
209
|
+
const child = childLayer(layer, `${name}/${entry.name}`, categories);
|
|
210
|
+
return [...child.violations, ...walk(join(folder, entry.name), child.layer)];
|
|
211
|
+
})];
|
|
143
212
|
}
|
|
144
|
-
return walk(root);
|
|
213
|
+
return walk(root, "source");
|
|
145
214
|
}
|
|
146
215
|
export { treeViolations as t };
|
package/dist/treeViolations.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { t as treeViolations } from "./treeViolations-
|
|
1
|
+
export { t as treeViolations } from "./treeViolations-CeTXg9WP.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: no empty folder, no category folder holding a single child, stylesheets grouped in styles/.",
|
|
3
|
+
"version": "0.6.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/.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lint",
|
|
7
7
|
"folder-structure",
|
|
@@ -34,25 +34,41 @@
|
|
|
34
34
|
"build": "tsdown",
|
|
35
35
|
"typecheck": "tsc -p .",
|
|
36
36
|
"lint": "oxlint --type-aware src oxlint.config.ts tsdown.config.ts",
|
|
37
|
-
"deps": "depcruise src",
|
|
37
|
+
"deps": "depcruise --output-type err-long src",
|
|
38
38
|
"tree": "node src/checkTree.ts src --root-entries",
|
|
39
39
|
"duplicates": "jscpd src",
|
|
40
|
-
"check": "npm run typecheck && npm run lint && npm run deps && npm run tree && npm run duplicates",
|
|
41
|
-
"test": "node --test 'src/**/*.test.ts'"
|
|
40
|
+
"check": "prettier --check . && npm run typecheck && npm run lint && npm run deps && npm run tree && npm run duplicates && npm run unused",
|
|
41
|
+
"test": "node --test 'src/**/*.test.ts'",
|
|
42
|
+
"format": "prettier --write .",
|
|
43
|
+
"unused": "knip"
|
|
42
44
|
},
|
|
43
45
|
"devDependencies": {
|
|
44
46
|
"@brutalway/dependency-cruiser-config": "0.5.0",
|
|
45
|
-
"@brutalway/oxlint-config-node": "0.2.
|
|
47
|
+
"@brutalway/oxlint-config-node": "0.2.1",
|
|
46
48
|
"@brutalway/oxlint-config-node-test": "0.1.0",
|
|
47
|
-
"@brutalway/
|
|
49
|
+
"@brutalway/prettier-config": "0.3.0",
|
|
50
|
+
"@brutalway/tsconfig": "0.4.0",
|
|
51
|
+
"@swc/core": "1.16.22",
|
|
52
|
+
"@types/node": "26.6.6",
|
|
48
53
|
"dependency-cruiser": "18.7.2",
|
|
49
|
-
"jscpd": "5.4.
|
|
50
|
-
"
|
|
51
|
-
"oxlint
|
|
52
|
-
"
|
|
54
|
+
"jscpd": "5.4.3",
|
|
55
|
+
"knip": "6.44.2",
|
|
56
|
+
"oxlint": "1.92.0",
|
|
57
|
+
"oxlint-tsgolint": "7.0.2006",
|
|
58
|
+
"prettier": "3.9.10",
|
|
59
|
+
"tsdown": "0.23.7",
|
|
53
60
|
"typescript": "7.0.2"
|
|
54
61
|
},
|
|
55
62
|
"imports": {
|
|
56
63
|
"#src/*": "./src/*"
|
|
64
|
+
},
|
|
65
|
+
"prettier": "@brutalway/prettier-config",
|
|
66
|
+
"allowScripts": {
|
|
67
|
+
"@swc/core": false
|
|
68
|
+
},
|
|
69
|
+
"overrides": {
|
|
70
|
+
"eslint-plugin-no-comments": {
|
|
71
|
+
"minimatch": "^11.0.1"
|
|
72
|
+
}
|
|
57
73
|
}
|
|
58
74
|
}
|