@brutalway/check-tree 0.5.0 → 0.5.1

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,6 +1,45 @@
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: 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
+
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
+ 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
4
43
 
5
44
  - no empty folder
6
45
  - a category folder (`components`, `hooks`, `stores`…) does not hold a single child: move the child up and delete the folder
@@ -29,33 +68,14 @@ So a feature with two components puts them in `components/`, and a `components/`
29
68
 
30
69
  Test files (`*.test.ts`, `*.test.tsx`) are not counted.
31
70
 
32
- ```sh
33
- npm install --save-dev @brutalway/check-tree
34
- npx check-tree src
35
- ```
71
+ ### Output
36
72
 
37
73
  It prints one line per problem and exits with code 1 if it found any.
38
74
 
39
- The default category folders are `components`, `orchestrators`, `presenters`, `hooks`, `styles`, `types`, `configs`, `stores`, `routes`, `services`, `queries`, `keys`, `schemas`, `clients`, `workers` and `lib`. Pass your own list with `--categories`:
75
+ ### Options
40
76
 
41
- ```sh
42
- npx check-tree src --categories components,hooks,utils
43
- ```
44
-
45
- The grouping rule only applies to the kinds listed in the table that are also in this list.
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`.
77
+ | Option | Default |
78
+ | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
79
+ | folder | `src` |
80
+ | `--categories <list>` | `components`, `orchestrators`, `presenters`, `hooks`, `styles`, `types`, `configs`, `stores`, `routes`, `services`, `queries`, `keys`, `schemas`, `clients`, `workers`, `lib` |
81
+ | `--root-entries` | off |
package/dist/checkTree.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { t as treeViolations } from "./treeViolations-DewMNsBO.js";
2
+ import { t as treeViolations } from "./treeViolations-DzriZgEc.js";
3
3
  import { resolve } from "node:path";
4
4
  import { parseArgs } from "node:util";
5
5
  // src/checkTree.ts
@@ -1,6 +1,6 @@
1
1
  import { basename, join, relative } from "node:path";
2
2
  import { readdirSync } from "node:fs";
3
- // src/features/tree-violations/treeViolations.ts
3
+ // src/features/tree-violations/TreeViolationsConfig.ts
4
4
  const DEFAULT_CATEGORY_FOLDERS = [
5
5
  "components",
6
6
  "orchestrators",
@@ -116,25 +116,35 @@ const FILE_GROUPS = [
116
116
  appliesIn: outsideOwnFolder("lib")
117
117
  }
118
118
  ];
119
- function groupViolations(name, folderName, fileNames, folderNames, categoryFolders) {
119
+ // src/features/tree-violations/treeViolations.ts
120
+ function groupViolations(name, folderName, listing, categoryFolders) {
120
121
  const violations = [];
121
122
  for (const { folder, belongs, appliesIn } of FILE_GROUPS) {
122
123
  if (!categoryFolders.has(folder) || !appliesIn(folderName)) continue;
123
- const loose = fileNames.filter(belongs);
124
+ const loose = listing.files.filter(belongs);
124
125
  if (loose.length === 0) continue;
125
- if (folderNames.has(folder)) violations.push(`${name} has a ${folder}/ folder: move ${loose.join(", ")} into it`);
126
+ if (listing.folders.has(folder)) violations.push(`${name} has a ${folder}/ folder: move ${loose.join(", ")} into it`);
126
127
  else if (loose.length > 1) violations.push(`${name} holds ${loose.length} ${folder} files (${loose.join(", ")}): move them into ${folder}/`);
127
128
  }
128
129
  return violations;
129
130
  }
131
+ function folderListing(entries) {
132
+ return {
133
+ files: entries.filter((entry) => entry.isFile()).map((entry) => entry.name).toSorted(),
134
+ folders: new Set(entries.filter((entry) => entry.isDirectory()).map((entry) => entry.name))
135
+ };
136
+ }
137
+ function shapeViolations(name, count, isCategory) {
138
+ if (count === 0) return [`${name} is empty: delete it`];
139
+ if (count === 1 && isCategory) return [`${name} holds a single child: move it up one level and delete the folder`];
140
+ return [];
141
+ }
130
142
  function folderViolations(folder, name, categoryFolders, groupsFiles) {
131
143
  const children = readdirSync(folder, { withFileTypes: true }).filter((entry) => !TEST_FILE.test(entry.name));
132
- const fileNames = children.filter((entry) => entry.isFile()).map((entry) => entry.name).toSorted();
133
- const folderNames = new Set(children.filter((entry) => entry.isDirectory()).map((entry) => entry.name));
134
- const violations = [];
135
- if (children.length === 0) violations.push(`${name} is empty: delete it`);
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;
144
+ const violations = shapeViolations(name, children.length, categoryFolders.has(basename(folder)));
145
+ if (!groupsFiles) return violations;
146
+ const listing = folderListing(children);
147
+ return [...violations, ...groupViolations(name, basename(folder), listing, categoryFolders)];
138
148
  }
139
149
  function treeViolations(root, categoryFolders = DEFAULT_CATEGORY_FOLDERS, options = {}) {
140
150
  const categories = new Set(categoryFolders);
@@ -1 +1 @@
1
- export { t as treeViolations } from "./treeViolations-DewMNsBO.js";
1
+ export { t as treeViolations } from "./treeViolations-DzriZgEc.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brutalway/check-tree",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Checks a source tree: no empty folder, no category folder holding a single child, stylesheets grouped in styles/.",
5
5
  "keywords": [
6
6
  "lint",
@@ -34,7 +34,7 @@
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
40
  "check": "npm run typecheck && npm run lint && npm run deps && npm run tree && npm run duplicates",
@@ -42,9 +42,9 @@
42
42
  },
43
43
  "devDependencies": {
44
44
  "@brutalway/dependency-cruiser-config": "0.5.0",
45
- "@brutalway/oxlint-config-node": "0.2.0",
45
+ "@brutalway/oxlint-config-node": "0.2.1",
46
46
  "@brutalway/oxlint-config-node-test": "0.1.0",
47
- "@brutalway/tsconfig": "0.3.0",
47
+ "@brutalway/tsconfig": "0.4.0",
48
48
  "dependency-cruiser": "18.7.2",
49
49
  "jscpd": "5.4.2",
50
50
  "oxlint": "1.91.0",