|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +const SCAN_ROOTS = ["src", "games", "samples", "tools"]; |
| 5 | +const ALLOWED_EXTENSIONS = new Set([".js", ".mjs"]); |
| 6 | +const IGNORED_DIRS = new Set(["node_modules", ".git", "tmp"]); |
| 7 | + |
| 8 | +const LOCAL_HELPER_RULES = [ |
| 9 | + { rule: "local-helper-definition", regex: /function\s+asFiniteNumber\s*\(/g, label: "rule:helper-fn-asFiniteNumber" }, |
| 10 | + { rule: "local-helper-definition", regex: /function\s+asPositiveInteger\s*\(/g, label: "rule:helper-fn-asPositiveInteger" }, |
| 11 | + { rule: "local-helper-definition", regex: /function\s+isPlainObject\s*\(/g, label: "rule:helper-fn-isPlainObject" }, |
| 12 | + { rule: "local-helper-definition", regex: /const\s+asFiniteNumber\s*=/g, label: "rule:helper-const-asFiniteNumber" }, |
| 13 | + { rule: "local-helper-definition", regex: /const\s+asPositiveInteger\s*=/g, label: "rule:helper-const-asPositiveInteger" }, |
| 14 | + { rule: "local-helper-definition", regex: /const\s+isPlainObject\s*=/g, label: "rule:helper-const-isPlainObject" } |
| 15 | +]; |
| 16 | + |
| 17 | +const DIRECT_SHARED_IMPORT_RULES = [ |
| 18 | + { rule: "direct-shared-relative-import", regex: /\.\.\/shared\//g, label: "rule:relative-shared-depth-1" }, |
| 19 | + { rule: "direct-shared-relative-import", regex: /\.\.\/\.\.\/shared\//g, label: "rule:relative-shared-depth-2" }, |
| 20 | + { rule: "direct-shared-relative-import", regex: /\.\.\/\.\.\/\.\.\/src\/shared\//g, label: "rule:relative-shared-src-depth-3" }, |
| 21 | + { rule: "direct-shared-relative-import", regex: /\.\.\/\.\.\/\.\.\/\.\.\/shared\//g, label: "rule:relative-shared-depth-4" } |
| 22 | +]; |
| 23 | + |
| 24 | +const ALIAS_RULE = { rule: "shared-alias-import-disallowed", regex: /@shared\//g, label: "rule:shared-alias-marker" }; |
| 25 | + |
| 26 | +async function pathExists(targetPath) { |
| 27 | + try { |
| 28 | + await fs.access(targetPath); |
| 29 | + return true; |
| 30 | + } catch { |
| 31 | + return false; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +async function collectSourceFiles(startDir, outFiles) { |
| 36 | + const entries = await fs.readdir(startDir, { withFileTypes: true }); |
| 37 | + for (const entry of entries) { |
| 38 | + const entryPath = path.join(startDir, entry.name); |
| 39 | + if (entry.isDirectory()) { |
| 40 | + if (IGNORED_DIRS.has(entry.name)) continue; |
| 41 | + await collectSourceFiles(entryPath, outFiles); |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + if (!entry.isFile()) continue; |
| 46 | + if (entry.name.toLowerCase().endsWith(".zip")) continue; |
| 47 | + |
| 48 | + const ext = path.extname(entry.name); |
| 49 | + if (!ALLOWED_EXTENSIONS.has(ext)) continue; |
| 50 | + |
| 51 | + outFiles.push(entryPath); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +function findViolations(fileContent, filePathFromRoot) { |
| 56 | + const violations = []; |
| 57 | + |
| 58 | + for (const check of LOCAL_HELPER_RULES) { |
| 59 | + const matches = fileContent.match(check.regex) || []; |
| 60 | + for (const _match of matches) { |
| 61 | + violations.push({ |
| 62 | + file: filePathFromRoot, |
| 63 | + type: check.rule, |
| 64 | + match: check.label |
| 65 | + }); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + for (const check of DIRECT_SHARED_IMPORT_RULES) { |
| 70 | + const matches = fileContent.match(check.regex) || []; |
| 71 | + for (const _match of matches) { |
| 72 | + violations.push({ |
| 73 | + file: filePathFromRoot, |
| 74 | + type: check.rule, |
| 75 | + match: check.label |
| 76 | + }); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + const aliasMatches = fileContent.match(ALIAS_RULE.regex) || []; |
| 81 | + for (const _match of aliasMatches) { |
| 82 | + violations.push({ |
| 83 | + file: filePathFromRoot, |
| 84 | + type: ALIAS_RULE.rule, |
| 85 | + match: ALIAS_RULE.label |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + return violations; |
| 90 | +} |
| 91 | + |
| 92 | +async function run() { |
| 93 | + const repoRoot = process.cwd(); |
| 94 | + const filesToScan = []; |
| 95 | + |
| 96 | + for (const root of SCAN_ROOTS) { |
| 97 | + const rootPath = path.join(repoRoot, root); |
| 98 | + if (!(await pathExists(rootPath))) continue; |
| 99 | + await collectSourceFiles(rootPath, filesToScan); |
| 100 | + } |
| 101 | + |
| 102 | + const violations = []; |
| 103 | + for (const filePath of filesToScan) { |
| 104 | + const content = await fs.readFile(filePath, "utf8"); |
| 105 | + const relPath = path.relative(repoRoot, filePath).replaceAll("\\", "/"); |
| 106 | + violations.push(...findViolations(content, relPath)); |
| 107 | + } |
| 108 | + |
| 109 | + if (violations.length === 0) { |
| 110 | + console.log("Shared extraction guard passed. No violations found."); |
| 111 | + process.exit(0); |
| 112 | + } |
| 113 | + |
| 114 | + console.error(`Shared extraction guard failed with ${violations.length} violation(s).`); |
| 115 | + for (const violation of violations) { |
| 116 | + console.error( |
| 117 | + `${violation.file} | ${violation.type} | ${violation.match}` |
| 118 | + ); |
| 119 | + } |
| 120 | + process.exit(1); |
| 121 | +} |
| 122 | + |
| 123 | +run().catch((error) => { |
| 124 | + console.error("Shared extraction guard encountered an unexpected error."); |
| 125 | + console.error(error && error.stack ? error.stack : String(error)); |
| 126 | + process.exit(1); |
| 127 | +}); |
0 commit comments