|
| 1 | +import { readFile, writeFile } from "fs/promises"; |
| 2 | +import { resolve, join, dirname, relative } from "path"; |
| 3 | +import { Command } from "commander"; |
1 | 4 | import StyleDictionary from "style-dictionary"; |
2 | 5 | import { registerTransforms } from "@tokens-studio/sd-transforms"; |
3 | | -import { Command } from "commander"; |
4 | | -import path from "path"; |
5 | 6 | import setWith from "lodash.setwith"; |
| 7 | +import camelCase from "lodash.camelcase"; |
6 | 8 |
|
7 | 9 | const program = new Command(); |
8 | 10 |
|
9 | 11 | program |
10 | | - .option("--input <path>", "Input path for tokens") |
11 | | - .option("--output <path>", "Output path for generated file") |
| 12 | + .option("--input <path>", "input path to $themes.json") |
| 13 | + .option("--output <directory>", "output directory") |
12 | 14 | .parse(process.argv); |
13 | 15 |
|
14 | 16 | const options = program.opts(); |
15 | 17 |
|
16 | | -const inputPath = options.input || "./src/**/*.json"; |
17 | | -const outputPath = options.output || "./dist/tokens.json"; |
18 | | - |
19 | | -const outputDir = path.dirname(outputPath); |
20 | | -const outputFile = path.basename(outputPath); |
21 | | - |
22 | | -await registerTransforms(StyleDictionary, {}); |
23 | | - |
24 | | -StyleDictionary.registerFormat({ |
25 | | - name: "minimalJSON", |
26 | | - formatter: ({ dictionary }) => { |
27 | | - const out = {}; |
28 | | - dictionary.allTokens.map((t) => setWith(out, t.path, t.value, Object)); |
29 | | - return JSON.stringify(out, null, 4); |
30 | | - }, |
31 | | -}); |
32 | | - |
33 | | -const sd = new StyleDictionary({ |
34 | | - source: [inputPath], |
35 | | - platforms: { |
36 | | - js: { |
37 | | - buildPath: path.join(outputDir, "/"), |
38 | | - transformGroup: "tokens-studio", |
39 | | - files: [ |
40 | | - { |
41 | | - destination: outputFile, |
42 | | - format: "minimalJSON", |
43 | | - }, |
44 | | - ], |
| 18 | +if (!options.input || !options.output) { |
| 19 | + console.error("Both --input and --output options are required."); |
| 20 | + process.exit(1); |
| 21 | +} |
| 22 | + |
| 23 | +const inputPath = resolve(process.cwd(), options.input); |
| 24 | +const outputDir = resolve(process.cwd(), options.output); |
| 25 | + |
| 26 | +(async () => { |
| 27 | + StyleDictionary.registerTransform({ |
| 28 | + name: "name/cti/custom", |
| 29 | + type: "name", |
| 30 | + transformer: function (token, config) { |
| 31 | + return camelCase( |
| 32 | + [config.prefix] |
| 33 | + .concat(token.path.map((it) => it.replace("-", "Minus"))) |
| 34 | + .join(" ") |
| 35 | + ); |
45 | 36 | }, |
46 | | - }, |
47 | | -}); |
| 37 | + }); |
| 38 | + |
| 39 | + StyleDictionary.registerFormat({ |
| 40 | + name: "minimalJSON", |
| 41 | + formatter: ({ dictionary }) => |
| 42 | + JSON.stringify( |
| 43 | + dictionary.allTokens.reduce((out, t) => { |
| 44 | + // if (["semantics", "composites"].includes(t.path[0]) === false) { |
| 45 | + // return out; |
| 46 | + // } |
| 47 | + |
| 48 | + return setWith(out, t.path, t.value, Object); |
| 49 | + }, {}), |
| 50 | + null, |
| 51 | + 2 |
| 52 | + ), |
| 53 | + }); |
| 54 | + |
| 55 | + registerTransforms(StyleDictionary, { casing: "custom" }); |
| 56 | + |
| 57 | + try { |
| 58 | + const blob = await readFile(inputPath, "utf8"); |
| 59 | + const data = JSON.parse(blob); |
| 60 | + |
| 61 | + const result = data.map((item) => { |
| 62 | + const name = item.name; |
| 63 | + const selectedTokenSets = item.selectedTokenSets || {}; |
| 64 | + |
| 65 | + const sources = Object.keys(selectedTokenSets).map((key) => |
| 66 | + relative(process.cwd(), join(dirname(inputPath), `${key}.json`)) |
| 67 | + ); |
| 68 | + |
| 69 | + return { |
| 70 | + name, |
| 71 | + sources, |
| 72 | + }; |
| 73 | + }); |
| 74 | + |
| 75 | + const modules = await Promise.all( |
| 76 | + result.map(async ({ name, sources }) => { |
| 77 | + const exportedName = camelCase(name); |
| 78 | + const sd = new StyleDictionary({ |
| 79 | + source: sources, |
| 80 | + platforms: { |
| 81 | + js: { |
| 82 | + buildPath: join(relative(process.cwd(), outputDir), "/"), |
| 83 | + transformGroup: "tokens-studio", |
| 84 | + transform: ["name/with-minus"], |
| 85 | + files: [ |
| 86 | + { |
| 87 | + destination: `${name}.json`, |
| 88 | + format: "minimalJSON", |
| 89 | + }, |
| 90 | + ], |
| 91 | + }, |
| 92 | + }, |
| 93 | + }); |
| 94 | + |
| 95 | + await sd.cleanAllPlatforms(); |
| 96 | + await sd.buildAllPlatforms(); |
| 97 | + |
| 98 | + return { |
| 99 | + name: exportedName, |
| 100 | + file: `./${name}.json`, |
| 101 | + }; |
| 102 | + }) |
| 103 | + ); |
| 104 | + |
| 105 | + const es = modules.map( |
| 106 | + ({ name, file }) => `export { default as ${name} } from "${file}";` |
| 107 | + ); |
| 108 | + const cjs = modules.map( |
| 109 | + ({ name, file }) => `exports.${name} = require("${file}");` |
| 110 | + ); |
| 111 | + const types = modules.map( |
| 112 | + ({ name, file }) => |
| 113 | + `export declare const ${name}: typeof import("${file}");` |
| 114 | + ); |
48 | 115 |
|
49 | | -await sd.cleanAllPlatforms(); |
50 | | -await sd.buildAllPlatforms(); |
| 116 | + await Promise.all([ |
| 117 | + writeFile(join(outputDir, "index.js"), es.join("\n"), "utf8"), |
| 118 | + writeFile(join(outputDir, "index.cjs.js"), cjs.join("\n"), "utf8"), |
| 119 | + writeFile(join(outputDir, "index.d.ts"), types.join("\n"), "utf8"), |
| 120 | + ]); |
| 121 | + } catch (error) { |
| 122 | + console.error("Error processing the files:", error); |
| 123 | + } |
| 124 | +})(); |
0 commit comments