archivo del paquete→ .shtukbase/packages/design-tokens/actions/build.ts
actions/build.ts
39 líneas, 2.1 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // design-tokens `build`: writes the CSS custom properties of the tokens (DTCG) to `out` of /design/tokens.md.
- // Runs by itself after install and update; by hand: `shtuk run design-tokens build`. Invalid tokens: nothing is
- // written, the problems are printed, exit 1.
- // `--style <id>` builds a style package's tokens instead: /design/<id>/tokens.json and tokens.dark.json into
- // /design/<id>/tokens.css, the dark scheme on `.dark, [data-theme="dark"]` (the token contract, /design/contract.md).
- // Style packages call `buildStyle(id)` from their own build action. `--source`, `--dark`, `--out` set one file each.
- import { mkdirSync, writeFileSync } from 'node:fs';
- import { dirname, join } from 'node:path';
- import { type Config, build, config, styleConfig } from './tokenslib.ts';
- /** Builds and writes one set of tokens; returns the message and whether it worked. */
- export function write(root: string, over: Partial<Config>): { ok: boolean; message: string } {
- const c = config(root, over);
- const { text, problems } = build(root, over);
- if (problems.length) return { ok: false, message: `design-tokens: ${c.out} is not built — tokens with errors:\n${problems.join('\n')}` };
- mkdirSync(dirname(join(root, c.out)), { recursive: true });
- writeFileSync(join(root, c.out), text);
- return { ok: true, message: `design-tokens: ${c.out} — ${text.split('\n').filter((l) => l.startsWith(' --')).length} variables` };
- }
- /** The tokens of the style package `id`, built into /design/<id>/tokens.css. */
- export const buildStyle = (id: string, root = process.cwd()) => write(root, styleConfig(id));
- if (import.meta.main) {
- const args = process.argv.slice(2);
- const opt = (name: string) => {
- const i = args.indexOf(name);
- return i >= 0 ? args[i + 1] : undefined;
- };
- const style = opt('--style');
- const over: Partial<Config> = style ? styleConfig(style) : {};
- for (const [flag, key] of [['--source', 'source'], ['--dark', 'dark'], ['--out', 'out']] as const) {
- const v = opt(flag);
- if (v !== undefined) over[key] = v;
- }
- const r = write(process.cwd(), over);
- console.log(r.message);
- if (!r.ok) process.exitCode = 1;
- }