@md-code/follow-architecture 0.1.0 → 0.1.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
@@ -7,56 +7,73 @@ Two kinds of checks:
7
7
  - **nodes** — where files may live and what names they may have (a folder tree with closed/open lists and per-name reports).
8
8
  - **invariants** — per-file content rules: forbidden text, forbidden imports, and conditional checks driven by the file's AST.
9
9
 
10
+ ```mermaid
11
+ flowchart LR
12
+ A["architecture.json"] --> N["Nodes<br/>where files may live"]
13
+ A --> I["Invariants<br/>what files may contain"]
14
+ F["Source files"] --> N
15
+ F --> I
16
+ N --> R["Report"]
17
+ I --> R
18
+ ```
19
+
20
+ ## Quick start
21
+
22
+ ```sh
23
+ bun add -D @md-code/follow-architecture
24
+ bunx follow-architecture --config architecture.json --out reports/follow-architecture.md
25
+ ```
26
+
10
27
  ## `architecture.json`
11
28
 
12
29
  ```jsonc
13
30
  {
14
- "aliases": { "#_db": "core/ehr/_db" },
31
+ "aliases": { "#db": "src/db" },
15
32
  "nodes": [
16
33
  {
17
- "slug": "slice",
18
- "name": "{slice}.v",
19
- "parent": "ehr",
34
+ "slug": "feature",
35
+ "name": "{feature}",
36
+ "parent": "features",
20
37
  "closed": true,
21
38
  "report": { "message": "…", "fix": "…" },
22
39
  "children": [
23
40
  "{kebab-case}.types.ts",
24
41
  "index.ts",
25
- { "name": "{kebab-case}.resource.tsx", "forbidden": true, "report": { "message": "…", "fix": "…" } }
42
+ { "name": "{kebab-case}.store.tsx", "forbidden": true, "report": { "message": "…", "fix": "…" } }
26
43
  ]
27
44
  },
28
- { "slug": "ehr", "name": "core/ehr", "children": ["slice", "db"] }
45
+ { "slug": "features", "name": "src/features", "children": ["feature", "db"] }
29
46
  ],
30
47
  "invariants": [
31
48
  {
32
- "id": "rm-no-bypass",
33
- "where": "core/ehr/{slice}.v/**/*.rm.ts",
49
+ "id": "service-no-bypass",
50
+ "where": "src/features/{feature}/**/*.service.ts",
34
51
  "forbid": [
35
- { "key": "client", "pattern": "new AppMedplumClient", "report": { "message": "…", "fix": "…" }, "except": ["**/legacy/**"] },
52
+ { "key": "client", "pattern": "new HttpClient", "report": { "message": "…", "fix": "…" }, "except": ["**/legacy/**"] },
36
53
  { "type": "imports", "key": "no-vscode", "pattern": "vscode", "report": { "message": "…" } }
37
54
  ],
38
55
  "when": "(ctx) => { /* AST-level check, returns null | true | finding | finding[] */ }",
39
- "except": ["core/ehr/catalog.v/**/*.rm.ts"],
56
+ "except": ["src/features/shared/**/*.service.ts"],
40
57
  "report": { "message": "…", "fix": "…" }
41
58
  }
42
59
  ]
43
60
  }
44
61
  ```
45
62
 
46
- Top-level options: `files` (globs scanned by the CLI, default `**/*.ts`, `**/*.tsx`), `exts` (default `.ts`, `.tsx`, `.js`, `.jsx`), `ignores` (file globs skipped by the CLI).
63
+ Top-level options: `files` (globs scanned by the CLI, default `**/*.ts`, `**/*.tsx`), `exts` (default `.ts`, `.tsx`, `.js`, `.jsx`), `ignores` (file globs skipped by the CLI), `ignoreDirs` (folder names the CLI does not enter, added to `node_modules`, `.git`, `dist`, `build`).
47
64
 
48
65
  ## Nodes
49
66
 
50
67
  A **node** is a folder (or folder prefix) in the tree. Nodes reference each other by `slug` to form the tree; the path of a node is its `parent`'s path + its `name`.
51
68
 
52
69
  - `slug` — unique name, used as `parent` by children and as `node` by invariants.
53
- - `name` — the path segment (relative to the parent). `{name}` placeholders capture path segments (e.g. `{slice}` in `{slice}.v` captures `allergies`). Captures are available in reports of every finding inside the node.
70
+ - `name` — the path segment (relative to the parent). `{name}` placeholders capture path segments (e.g. `{feature}` in a `where` of `src/features/{feature}/**` captures `orders`). Captures are available in reports of every finding inside the node.
54
71
  - `parent` (optional) — the `slug` of the containing node. A node without `parent` is a root.
55
72
  - `closed` (optional, default `false`) — when `true`, a file directly inside the node must match one of its `children`; otherwise it gets the node's `report`. A folder (a child that is a folder) that is not in the list is also a violation.
56
73
  - `report` (optional) — the finding for "file does not belong here" (closed nodes) — see [report](#report).
57
74
  - `children` (optional) — the list of allowed names inside the node. Each entry is either:
58
75
  - a **string** — a name template (`{kebab-case}`, `*`, `?` work; a name without an extension is a folder, with an extension is a file);
59
- - an **object** — `{ name, forbidden?, report?, description? }`. `forbidden: true` names a shape that must **never** exist (e.g. `{kebab-case}.resource.tsx` in a sub-feature folder) and reports `report` when it does.
76
+ - an **object** — `{ name, forbidden?, report?, description? }`. `forbidden: true` names a shape that must **never** exist (e.g. `{kebab-case}.store.tsx` in a sub-feature folder) and reports `report` when it does.
60
77
 
61
78
  A node with no `children` is **open** — anything inside is fine. The deepest matching node wins: a file is checked against the most specific node whose path prefix matches.
62
79
 
@@ -91,8 +108,7 @@ An **invariant** is a per-file rule. Fields:
91
108
  | `ctx.path` | repo-relative path (posix) |
92
109
  | `ctx.basename` | file name |
93
110
  | `ctx.dir` | directory (repo-relative) |
94
- | `ctx.slice` | the `{slice}` capture if the path matches `…/<slice>.v/…`, else `null` |
95
- | `ctx.captures` | all `{name}` captures from the invariant's `where` and the matched node |
111
+ | `ctx.captures` | all `{name}` captures from the invariant's `where` and the matched node, e.g. `ctx.captures.feature` |
96
112
  | `ctx.imports` | the file's import declarations: `{ source, named, defaultName, namespaceName, typeOnly, resolved }` — `resolved` is the repo-relative path after alias expansion, `null` for external modules |
97
113
  | `ctx.siblings` | basenames of the other files in the same folder (the folder listing the CLI walked) — for rules like "at most N files per folder" or "no duplicated basename prefixes" |
98
114
  | `ctx.arch` | the loaded architecture (nodes, invariants, aliases) |
@@ -116,7 +132,7 @@ An **invariant** is a per-file rule. Fields:
116
132
  | `{basename}` | file name without directory |
117
133
  | `{dir}` | directory of the file (repo-relative) |
118
134
 
119
- **Captures** — any `{name}` used in a node `name`, invariant `where`, or `forbid` (imports) pattern. E.g. `{slice}` in `core/ehr/{slice}.v/**/*.rm.ts` captures `allergies` and is available as `{slice}` in that file's findings.
135
+ **Captures** — any `{name}` used in a node `name`, invariant `where`, or `forbid` (imports) pattern. E.g. `{feature}` in `src/features/{feature}/**/*.service.ts` captures `orders` and is available as `{feature}` in that file's findings.
120
136
 
121
137
  **Match context** (text-forbid findings only):
122
138
 
@@ -134,14 +150,16 @@ Without `report`, the tool composes a fallback from the rule's shape.
134
150
 
135
151
  ## Aliases
136
152
 
137
- `aliases` maps import specifiers to paths for import resolution (e.g. `"#_db": "core/ehr/_db"`). Used by `type: "imports"` forbid entries and by `ctx.imports[].resolved`.
153
+ `aliases` maps import specifiers to paths for import resolution (e.g. `"#db": "src/db"`). Used by `type: "imports"` forbid entries and by `ctx.imports[].resolved`.
138
154
 
139
155
  ## CLI
140
156
 
141
157
  ```sh
142
- node follow-architecture/bin/follow-architecture.js [options]
158
+ bunx follow-architecture [options]
143
159
  ```
144
160
 
161
+ Without installing: `bunx @md-code/follow-architecture [options]`.
162
+
145
163
  | flag | default | meaning |
146
164
  | --- | --- | --- |
147
165
  | `[cwd]` | `.` | repo root (positional) |
@@ -158,11 +176,21 @@ The package exports a `flatConfig` factory:
158
176
 
159
177
  ```js
160
178
  // eslint.config.js
161
- const { flatConfig } = require("./follow-architecture/config.js");
162
-
163
- module.exports = flatConfig(require("./architecture.json"), {
164
- files: ["core/ehr/**/*.ts", "core/ehr/**/*.tsx"],
165
- });
179
+ const plugin = require("@md-code/follow-architecture");
180
+
181
+ module.exports = [
182
+ {
183
+ files: ["src/**/*.ts", "src/**/*.tsx"],
184
+ plugins: { "md-code": plugin },
185
+ rules: {
186
+ "md-code/follow-architecture": ["error", {
187
+ config: "architecture.json",
188
+ }],
189
+ },
190
+ },
191
+ ];
166
192
  ```
167
193
 
168
- `flatConfig(architecture, { parser, files, ignores })` returns a flat-config array. `architecture` is the object (or `{ file: "architecture.json" }` — it is resolved lazily per file so the repo root is found from the file being linted). `parser` defaults to `@typescript-eslint/parser`. The rule is `md-code/follow-architecture`.
194
+ The package's main export is the ESLint plugin (`{ meta, rules }`). The rule is `md-code/follow-architecture`; pass the architecture file via the `config` option (repo-root-relative, or absolute).
195
+
196
+ `require("@md-code/follow-architecture/config").flatConfig(architecture, { parser, files, ignores, plugins })` is also exported as a flat-config factory. `architecture` is the object (or `{ file: "architecture.json" }` — it is resolved lazily per file so the repo root is found from the file being linted). `parser` defaults to `@typescript-eslint/parser`.
package/config.js CHANGED
@@ -2,7 +2,8 @@
2
2
 
3
3
  const fs = require("node:fs");
4
4
  const path = require("node:path");
5
- const { buildArchitecture } = require("./main");
5
+ const { minimatch } = require("minimatch");
6
+ const { loadArchitecture } = require("./main");
6
7
  const { checkFile } = require("./utils");
7
8
  const { createTsProject } = require("./ast");
8
9
  const pkg = require("./package.json");
@@ -14,7 +15,7 @@ const findRepoRoot = (from) => {
14
15
  if (fs.existsSync(path.join(dir, name))) return dir;
15
16
  }
16
17
  const parent = path.dirname(dir);
17
- if (parent === dir) return from;
18
+ if (parent === dir) return process.cwd();
18
19
  dir = parent;
19
20
  }
20
21
  };
@@ -23,12 +24,30 @@ const rule = {
23
24
  meta: {
24
25
  type: "problem",
25
26
  docs: { description: "Enforces the vertical-slice architecture defined in architecture.json" },
26
- schema: [{ type: "object" }],
27
+ schema: [
28
+ {
29
+ type: "object",
30
+ properties: {
31
+ config: { type: "string" },
32
+ include: { type: "array" },
33
+ ignoreDirs: { type: "array" },
34
+ ignores: { type: "array" },
35
+ },
36
+ additionalProperties: false,
37
+ },
38
+ ],
27
39
  },
28
40
  create(context) {
29
- const arch = buildArchitecture(context.options[0] ?? {});
41
+ const opts = context.options[0] ?? {};
30
42
  const filePath = context.filename ?? context.getFilename();
31
43
  const repoRoot = findRepoRoot(path.dirname(filePath));
44
+ const rel = path.relative(repoRoot, filePath).split(path.sep).join("/");
45
+ const arch = loadArchitecture(opts.config ?? path.join(repoRoot, "architecture.json"));
46
+ const ignoreDirs = [...new Set(["node_modules", ".git", "dist", "build", ...(arch.ignoreDirs ?? []), ...(opts.ignoreDirs ?? [])])];
47
+ const ignoreGlobs = [...(arch.ignores ?? []), ...(opts.ignores ?? [])];
48
+ if (ignoreDirs.some((d) => rel === d || rel.startsWith(`${d}/`))) return {};
49
+ if (ignoreGlobs.some((g) => minimatch(rel, g))) return {};
50
+ if (opts.include?.length && !opts.include.some((g) => minimatch(rel, g))) return {};
32
51
  const project = createTsProject();
33
52
  const findings = checkFile(filePath, fs.readFileSync(filePath, "utf8"), repoRoot, arch, project);
34
53
  for (const f of findings) {
package/main.js CHANGED
@@ -24,6 +24,7 @@ const buildArchitecture = (raw) => {
24
24
  aliases: normalizeAliases(raw.aliases ?? {}),
25
25
  files: raw.files ?? ["**/*.ts", "**/*.tsx"],
26
26
  ignores: raw.ignores ?? [],
27
+ ignoreDirs: raw.ignoreDirs ?? [],
27
28
  exts: raw.exts ?? [".ts", ".tsx", ".js", ".jsx"],
28
29
  raw,
29
30
  };
@@ -70,7 +71,7 @@ const main = (argv) => {
70
71
  const project = createTsProject();
71
72
  const shouldCheck = matchers(arch);
72
73
  const files = pipe(
73
- collectFiles(repoRoot, { exts: arch.exts, ignoreDirs: ["node_modules", ".git", "dist", "build", "_medplum"], ignoreGlobs: [...(arch.ignores ?? []), ...(args.ignore ?? [])] }),
74
+ collectFiles(repoRoot, { exts: arch.exts, ignoreDirs: ["node_modules", ".git", "dist", "build", ...arch.ignoreDirs], ignoreGlobs: [...(arch.ignores ?? []), ...(args.ignore ?? [])] }),
74
75
  map((abs) => ({ abs, rel: toRel(repoRoot, abs) })),
75
76
  filter((f) => shouldCheck(f.rel)),
76
77
  );
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@md-code/follow-architecture",
3
- "version": "0.1.0",
4
- "description": "ESLint rule + CLI that enforces the vertical-slice architecture: every slice has a .resource.ts CRUD binding, every .rm.ts imports and calls it, and nothing bypasses it.",
3
+ "version": "0.1.1",
4
+ "description": "ESLint rule + CLI that enforces a declarative architecture: where files may live, what they may be named, and what they may contain.",
5
5
  "license": "MIT",
6
- "main": "config.js",
6
+ "main": "plugin.js",
7
7
  "engines": {
8
8
  "node": ">=18"
9
9
  },
@@ -11,6 +11,7 @@
11
11
  "follow-architecture": "bin/follow-architecture.js"
12
12
  },
13
13
  "files": [
14
+ "plugin.js",
14
15
  "config.js",
15
16
  "main.js",
16
17
  "utils.js",
@@ -21,7 +22,8 @@
21
22
  "README.md"
22
23
  ],
23
24
  "exports": {
24
- ".": "./config.js",
25
+ ".": "./plugin.js",
26
+ "./plugin": "./plugin.js",
25
27
  "./config": "./config.js",
26
28
  "./main": "./main.js",
27
29
  "./package.json": "./package.json"
@@ -42,8 +44,6 @@
42
44
  "eslint",
43
45
  "eslint-rule",
44
46
  "architecture",
45
- "vertical-slice",
46
- "fhir",
47
47
  "typescript"
48
48
  ]
49
49
  }
package/plugin.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ const { plugin } = require("./config");
4
+
5
+ module.exports = plugin;