|
| 1 | +/* eslint-disable no-console, no-undef */ |
| 2 | +const fs = require('node:fs'); |
| 3 | +const path = require('node:path'); |
| 4 | +const {pathToFileURL} = require('node:url'); |
| 5 | + |
| 6 | +const SRC_DIR = path.resolve(__dirname, '..', 'src'); |
| 7 | +const GULPFILE_URL = pathToFileURL(path.resolve(__dirname, '..', 'gulpfile.mjs')).href; |
| 8 | + |
| 9 | +// Bare `import 'pkg/path/file.css';` — non-relative, scoped or unscoped, ending in .css. |
| 10 | +const CSS_IMPORT_RE = /(?:^|\s)import\s+['"]((?:@[^'"\s/]+\/)?[^'"\s.][^'"\s]*\.css)['"]/gm; |
| 11 | + |
| 12 | +const EXCLUDED_SCOPES = ['@gravity-ui/']; |
| 13 | + |
| 14 | +async function main() { |
| 15 | + const {SHADOW_STYLE_IMPORTS} = await import(GULPFILE_URL); |
| 16 | + if (!Array.isArray(SHADOW_STYLE_IMPORTS)) { |
| 17 | + console.error('Failed to import SHADOW_STYLE_IMPORTS from gulpfile.mjs'); |
| 18 | + process.exit(1); |
| 19 | + } |
| 20 | + |
| 21 | + const declared = new Set(SHADOW_STYLE_IMPORTS); |
| 22 | + const actual = collectCssImports(SRC_DIR); |
| 23 | + |
| 24 | + const missing = [...actual].filter((x) => !declared.has(x)).sort(); |
| 25 | + const stale = [...declared].filter((x) => !actual.has(x)).sort(); |
| 26 | + |
| 27 | + if (missing.length || stale.length) { |
| 28 | + console.error('Shadow styles imports drift detected:'); |
| 29 | + if (missing.length) { |
| 30 | + console.error(' Missing in SHADOW_STYLE_IMPORTS (found in src, not in list):'); |
| 31 | + for (const item of missing) console.error(` + ${item}`); |
| 32 | + } |
| 33 | + if (stale.length) { |
| 34 | + console.error(' Stale in SHADOW_STYLE_IMPORTS (in list, not used in src):'); |
| 35 | + for (const item of stale) console.error(` - ${item}`); |
| 36 | + } |
| 37 | + console.error('Update SHADOW_STYLE_IMPORTS in packages/editor/gulpfile.mjs.'); |
| 38 | + process.exit(1); |
| 39 | + } |
| 40 | + |
| 41 | + console.log(`Shadow styles imports check passed (count: ${declared.size})`); |
| 42 | + process.exit(0); |
| 43 | +} |
| 44 | + |
| 45 | +function collectCssImports(dir) { |
| 46 | + const result = new Set(); |
| 47 | + walk(dir, (filePath) => { |
| 48 | + if (!/\.(?:ts|tsx|js|jsx|mjs|cjs)$/.test(filePath)) return; |
| 49 | + const content = fs.readFileSync(filePath, 'utf8'); |
| 50 | + for (const match of content.matchAll(CSS_IMPORT_RE)) { |
| 51 | + const spec = match[1]; |
| 52 | + if (EXCLUDED_SCOPES.some((scope) => spec.startsWith(scope))) continue; |
| 53 | + result.add(spec); |
| 54 | + } |
| 55 | + }); |
| 56 | + return result; |
| 57 | +} |
| 58 | + |
| 59 | +function walk(dir, visit) { |
| 60 | + for (const entry of fs.readdirSync(dir, {withFileTypes: true})) { |
| 61 | + const full = path.join(dir, entry.name); |
| 62 | + if (entry.isDirectory()) walk(full, visit); |
| 63 | + else if (entry.isFile()) visit(full); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +main().catch((err) => { |
| 68 | + console.error(err); |
| 69 | + process.exit(1); |
| 70 | +}); |
0 commit comments