|
| 1 | +import { readdir, readFile } from 'node:fs/promises'; |
| 2 | +import { extname, join, relative } from 'node:path'; |
| 3 | + |
| 4 | +const root = new URL('../', import.meta.url).pathname; |
| 5 | +const srcDir = join(root, 'src'); |
| 6 | +const allowedDirectWebComponentImporters = new Set([ |
| 7 | + 'src/components/KiloCrabIcon.tsx', |
| 8 | + 'src/components/shared/Banner.tsx', |
| 9 | +]); |
| 10 | + |
| 11 | +const allowedDirectWebComponentPrefixes = ['src/components/ui/']; |
| 12 | +const sourceExtensions = new Set(['.ts', '.tsx', '.js', '.jsx']); |
| 13 | +const webComponentImportPattern = /from\s+['"]@web\/components\//g; |
| 14 | + |
| 15 | +async function collectSourceFiles(dir) { |
| 16 | + const entries = await readdir(dir, { withFileTypes: true }); |
| 17 | + const files = await Promise.all( |
| 18 | + entries.map(async entry => { |
| 19 | + const path = join(dir, entry.name); |
| 20 | + if (entry.isDirectory()) return collectSourceFiles(path); |
| 21 | + if (!sourceExtensions.has(extname(entry.name))) return []; |
| 22 | + return [path]; |
| 23 | + }) |
| 24 | + ); |
| 25 | + |
| 26 | + return files.flat(); |
| 27 | +} |
| 28 | + |
| 29 | +function isAllowedDirectImporter(relativePath) { |
| 30 | + return ( |
| 31 | + allowedDirectWebComponentImporters.has(relativePath) || |
| 32 | + allowedDirectWebComponentPrefixes.some(prefix => relativePath.startsWith(prefix)) |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +const violations = []; |
| 37 | + |
| 38 | +for (const file of await collectSourceFiles(srcDir)) { |
| 39 | + const relativePath = relative(root, file); |
| 40 | + if (isAllowedDirectImporter(relativePath)) continue; |
| 41 | + |
| 42 | + const source = await readFile(file, 'utf8'); |
| 43 | + if (!webComponentImportPattern.test(source)) continue; |
| 44 | + |
| 45 | + violations.push(relativePath); |
| 46 | + webComponentImportPattern.lastIndex = 0; |
| 47 | +} |
| 48 | + |
| 49 | +if (violations.length > 0) { |
| 50 | + console.error( |
| 51 | + 'Prototype Routes must import production web components through prototype-local seams.' |
| 52 | + ); |
| 53 | + console.error( |
| 54 | + 'Move these imports behind src/components/ui/*, src/components/shared/*, or another approved bridge:' |
| 55 | + ); |
| 56 | + for (const violation of violations) console.error(`- ${violation}`); |
| 57 | + process.exit(1); |
| 58 | +} |
| 59 | + |
| 60 | +console.log('Prototype production web component imports use approved local seams.'); |
0 commit comments