The JS API is a lower-level API than the CLI that can be used to build Design Token systems. The JS API runs anywhere JavaScript does, including in Node.js, the browser, and serverless functions.
npm i -D @terrazzo/parser
And here’s a basic example showing config
, parse
, and build
steps:
import { defineConfig, parse, build } from "@terrazzo/parser";
const config = defineConfig(
{
// config options
},
{ cwd: new URL(import.meta.url) }
);
const rawTokens = "(any JSON or YAML)";
const { tokens, sources } = await parse(
[{ filename: new URL("file:///tokens.json"), src: rawTokens }],
{ config }
);
const buildResult = await build(tokens, { sources, config });
It’s worth noting the JS API is a little more manual work than the CLI:
-
parse()
andbuild()
are distinct steps that each do some of the work. -
defineConfig()
needs a cwd so it can resolve files (this can even be a remote URL, so long as it’s a URL()) - The AST generated from
parse()
must get passed intobuild()
so the error messages can point to the right lines in the source file. - The
build()
step only returns a final array ofoutputFiles
in memory but doesn’t write them to disk. It’s up to you to write them to disk, upload them somewhere, etc.