ShtukbaseStandard
файл пакета→ .shtukbase/packages/design-tokens/actions/build.ts

actions/build.ts

39 строк, 2.1 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // design-tokens `build`: writes the CSS custom properties of the tokens (DTCG) to `out` of /design/tokens.md.
  2. // Runs by itself after install and update; by hand: `shtuk run design-tokens build`. Invalid tokens: nothing is
  3. // written, the problems are printed, exit 1.
  4. // `--style <id>` builds a style package's tokens instead: /design/<id>/tokens.json and tokens.dark.json into
  5. // /design/<id>/tokens.css, the dark scheme on `.dark, [data-theme="dark"]` (the token contract, /design/contract.md).
  6. // Style packages call `buildStyle(id)` from their own build action. `--source`, `--dark`, `--out` set one file each.
  7. import { mkdirSync, writeFileSync } from 'node:fs';
  8. import { dirname, join } from 'node:path';
  9. import { type Config, build, config, styleConfig } from './tokenslib.ts';
  10. /** Builds and writes one set of tokens; returns the message and whether it worked. */
  11. export function write(root: string, over: Partial<Config>): { ok: boolean; message: string } {
  12. const c = config(root, over);
  13. const { text, problems } = build(root, over);
  14. if (problems.length) return { ok: false, message: `design-tokens: ${c.out} is not built — tokens with errors:\n${problems.join('\n')}` };
  15. mkdirSync(dirname(join(root, c.out)), { recursive: true });
  16. writeFileSync(join(root, c.out), text);
  17. return { ok: true, message: `design-tokens: ${c.out} — ${text.split('\n').filter((l) => l.startsWith(' --')).length} variables` };
  18. }
  19. /** The tokens of the style package `id`, built into /design/<id>/tokens.css. */
  20. export const buildStyle = (id: string, root = process.cwd()) => write(root, styleConfig(id));
  21. if (import.meta.main) {
  22. const args = process.argv.slice(2);
  23. const opt = (name: string) => {
  24. const i = args.indexOf(name);
  25. return i >= 0 ? args[i + 1] : undefined;
  26. };
  27. const style = opt('--style');
  28. const over: Partial<Config> = style ? styleConfig(style) : {};
  29. for (const [flag, key] of [['--source', 'source'], ['--dark', 'dark'], ['--out', 'out']] as const) {
  30. const v = opt(flag);
  31. if (v !== undefined) over[key] = v;
  32. }
  33. const r = write(process.cwd(), over);
  34. console.log(r.message);
  35. if (!r.ok) process.exitCode = 1;
  36. }