|
18 | 18 | * element clobbers a `[data-density]` ancestor's values for the whole |
19 | 19 | * subtree. The export format has no per-layer selector, hence this |
20 | 20 | * post-export step. It runs before generate-css-bundle.mjs (chained in |
21 | | - * the `generate:css-bundle` package script). |
| 21 | + * the `generate:css-bundle` package script), and the bundler asserts |
| 22 | + * the widening happened (shared pattern in semantic-scope.mjs). |
22 | 23 | * |
23 | 24 | * Known caveat, tracked in #5221: three names (`border-focus`, |
24 | 25 | * `text-disabled`, `border-disabled`) are declared in both the |
|
27 | 28 | * (it sorts last in the bundle) — a token-content bug upstream, not a |
28 | 29 | * consequence of this step. |
29 | 30 | * |
30 | | - * Idempotent; fails loudly if the semantic file is missing or does not |
31 | | - * start with the expected selector. |
| 31 | + * Every `semantic/*.css` file is widened, matching the bundler's |
| 32 | + * directory glob — a file the export adds later must not slip through |
| 33 | + * with the narrow selector. Idempotent; fails loudly if a semantic |
| 34 | + * file does not start with either the narrow or the widened selector. |
32 | 35 | * |
33 | | - * Usage: node scripts/widen-semantic-scope.mjs [--file <path>] |
| 36 | + * Usage: node scripts/widen-semantic-scope.mjs [--dir <path>] |
34 | 37 | */ |
35 | | -import { readFile, writeFile } from 'node:fs/promises' |
| 38 | +import { readFile, readdir, writeFile } from 'node:fs/promises' |
| 39 | +import { join } from 'node:path' |
36 | 40 | import process from 'node:process' |
| 41 | +import { NARROW_RE, WIDE, WIDE_RE } from './semantic-scope.mjs' |
37 | 42 |
|
38 | 43 | const args = parseArgs(process.argv.slice(2)) |
39 | | -const FILE = args.file ?? 'src/tokens/css/semantic/default.css' |
| 44 | +const DIR = args.dir ?? 'src/tokens/css/semantic' |
40 | 45 |
|
41 | | -const NARROW = ':root {' |
42 | | -const WIDE = ':root, [data-color-scheme] {' |
| 46 | +const files = (await readdir(DIR).catch(() => fail(`cannot read ${DIR}`))) |
| 47 | + .filter((file) => file.endsWith('.css')) |
| 48 | + .sort() |
43 | 49 |
|
44 | | -const css = await readFile(FILE, 'utf8').catch(() => |
45 | | - fail(`cannot read ${FILE}`), |
46 | | -) |
| 50 | +if (files.length === 0) fail(`no CSS files found under ${DIR}`) |
47 | 51 |
|
48 | | -if (css.startsWith(WIDE)) { |
49 | | - console.log(`widen-semantic-scope: ${FILE} already widened`) |
50 | | -} else if (css.startsWith(NARROW)) { |
51 | | - await writeFile(FILE, WIDE + css.slice(NARROW.length)) |
52 | | - console.log(`widen-semantic-scope: widened ${FILE} to "${WIDE.slice(0, -2)}"`) |
53 | | -} else { |
54 | | - fail( |
55 | | - `${FILE} does not start with "${NARROW}" — the export layout changed, review #5226 before proceeding`, |
56 | | - ) |
| 52 | +for (const file of files) { |
| 53 | + const path = join(DIR, file) |
| 54 | + const css = await readFile(path, 'utf8') |
| 55 | + if (WIDE_RE.test(css)) { |
| 56 | + console.log(`widen-semantic-scope: ${path} already widened`) |
| 57 | + } else if (NARROW_RE.test(css)) { |
| 58 | + await writeFile(path, css.replace(NARROW_RE, WIDE)) |
| 59 | + console.log(`widen-semantic-scope: widened ${path}`) |
| 60 | + } else { |
| 61 | + fail( |
| 62 | + `${path} does not start with a ":root" selector — the export layout changed, review #5226 before proceeding`, |
| 63 | + ) |
| 64 | + } |
57 | 65 | } |
58 | 66 |
|
59 | 67 | function parseArgs(argv) { |
|
0 commit comments