pstream is dead; long live pstream
taciturnaxolotl.github.io/pstream-ng/
1/**
2 * This script turns output from the figma plugin "style to JSON" into a usuable theme.
3 * It expects a format of "themes/{NAME}/anythinghere"
4 */
5
6import fs from "fs";
7
8const fileLocation = "./figmaTokens.json";
9const theme = "blue";
10
11const fileContents = fs.readFileSync(fileLocation, {
12 encoding: "utf-8",
13});
14const tokens = JSON.parse(fileContents);
15
16const themeTokens = tokens.themes[theme];
17const output = {};
18
19function setKey(obj, key, defaultVal) {
20 const realKey = key.match(/^\d+$/g) ? "c" + key : key;
21 if (obj[key]) return obj[key];
22 obj[realKey] = defaultVal;
23 return obj[realKey];
24}
25
26function handleToken(token, path) {
27 if (typeof token.name === "string" && typeof token.description === "string") {
28 let ref = output;
29 const lastKey = path.pop();
30 path.forEach((v) => {
31 ref = setKey(ref, v, {});
32 });
33 setKey(ref, lastKey, token.hex);
34 return;
35 }
36
37 for (let key in token) {
38 handleToken(token[key], [...path, key]);
39 }
40}
41
42handleToken(themeTokens, []);
43console.log(JSON.stringify(output, null, 2));