| 1 | // Enforces the mechanical rules of docs/code-standards.md. Each error names the rule to read. |
| 2 | import { existsSync, readFileSync |
| 3 | |
| 4 | process.chdir(`${import |
| 5 | |
| 6 | type Check |
| 7 | rule: string |
| 8 | applies: (path |
| 9 | find: (source |
| 10 | }; |
| 11 | |
| 12 | const linesMatching = ( |
| 13 | source.split("\n |
| 14 | |
| 15 | const checks: Check |
| 16 | { |
| 17 | rule: "3. Strings are never concatenated for layout", |
| 18 | applies: () => |
| 19 | find: (source) |
| 20 | }, |
| 21 | { |
| 22 | rule: "5. No wrapped comments or strings", |
| 23 | applies: () => |
| 24 | find: (source) |
| 25 | source.split("\n |
| 26 | const comment = |
| 27 | const wrappedLineComment = |
| 28 | i > 0 && |
| 29 | const wrappedJsxComment = |
| 30 | return wrappedLineComment || |
| 31 | }), |
| 32 | }, |
| 33 | { |
| 34 | rule: "6. `services/` holds services", |
| 35 | applies: (path) |
| 36 | path.includes("/services/ |
| 37 | find: (source) |
| 38 | ...(source.includes |
| 39 | ...linesMatching(source |
| 40 | ], |
| 41 | }, |
| 42 | { |
| 43 | rule: "6. Error classes live under `lib/`", |
| 44 | applies: (path) |
| 45 | path.startsWith("apps/api/src/ |
| 46 | path.endsWith(".errors.ts |
| 47 | !path.startsWith(" |
| 48 | find: () => |
| 49 | }, |
| 50 | { |
| 51 | rule: "7. Production quality is the default (no `any`)", |
| 52 | applies: () => |
| 53 | find: (source) |
| 54 | linesMatching(source, / |
| 55 | }, |
| 56 | { |
| 57 | rule: "8. One file holds one component family (a page is not a family)", |
| 58 | applies: (path) |
| 59 | find: (source) |
| 60 | const components = |
| 61 | source, |
| 62 | /^(export\s+)?( |
| 63 | ); |
| 64 | return components.length |
| 65 | }, |
| 66 | }, |
| 67 | { |
| 68 | rule: "10. Every suspended boundary has a loading state", |
| 69 | applies: (path) |
| 70 | find: (source) |
| 71 | }, |
| 72 | ]; |
| 73 | |
| 74 | const files = Bun |
| 75 | .stdout.toString() |
| 76 | .trim() |
| 77 | .split("\n") |
| 78 | .filter( |
| 79 | (path) => |
| 80 | !/(^|\/)( |
| 81 | path, |
| 82 | ), |
| 83 | ); |
| 84 | |
| 85 | const failures: string |
| 86 | for (const path |
| 87 | if (!existsSync(path |
| 88 | const source = |
| 89 | for (const check |
| 90 | if (!check.applies |
| 91 | failures.push( |
| 92 | ...check |
| 93 | .find(source) |
| 94 | .map((line) |
| 95 | ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if (failures.length > |
| 100 | console.error(`${ |
| 101 | process.exit(1) |
| 102 | } |