figma-token 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +40 -0
  3. package/dist/index.js +109 -0
  4. package/package.json +31 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 lee090626
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # figma-token
2
+
3
+ `figma-token` exports Figma Variables to design token files.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install --global figma-token
9
+ ```
10
+
11
+ ## Help
12
+
13
+ ```bash
14
+ figma-token --help
15
+ figma-token sync --help
16
+ ```
17
+
18
+ ## Basic usage
19
+
20
+ Export a local Figma Variables JSON file:
21
+
22
+ ```bash
23
+ figma-token sync --input ./figma-variables.json --format theme-ts --output ./theme.ts
24
+ ```
25
+
26
+ Preview token changes without writing output or snapshot files:
27
+
28
+ ```bash
29
+ figma-token sync --input ./figma-variables.json --dry-run
30
+ ```
31
+
32
+ `--input` accepts a Figma Variables JSON response or a normalized `tokens.json` array. Without `--input`, provide `--figma-token` and `--file-key`, or set `FIGMA_TOKEN` and `FIGMA_FILE_KEY`.
33
+
34
+ ## Options
35
+
36
+ `sync` supports `--input`, `--output`, `--snapshot`, `--format`, `--export-name`, `--figma-token`, `--file-key`, and `--dry-run`.
37
+
38
+ Supported formats are `tokens-json`, `theme-ts`, `variables-css`, `tokens-scss`, `tailwind-css`, and `tokens-dtcg-json`.
39
+
40
+ For Plugin usage and project-wide documentation, see <https://github.com/lee090626/Project-F>.
package/dist/index.js ADDED
@@ -0,0 +1,109 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/index.ts
4
+ import "dotenv/config";
5
+ import { Command, InvalidArgumentError } from "commander";
6
+
7
+ // src/sync.ts
8
+ import { mkdir, readFile, writeFile } from "fs/promises";
9
+ import { dirname } from "path";
10
+ import {
11
+ diffTokens,
12
+ isDesignTokenArray,
13
+ normalizeFigmaVariables,
14
+ renderCssVariables,
15
+ renderDtcgJson,
16
+ renderScssVariables,
17
+ renderTailwindTheme,
18
+ renderTheme,
19
+ renderTokensJson
20
+ } from "@lee090626/core";
21
+
22
+ // src/figma/fetchFigmaVariables.ts
23
+ var FIGMA_API = "https://api.figma.com/v1";
24
+ async function fetchFigmaVariables(fileKey, token) {
25
+ const response = await fetch(`${FIGMA_API}/files/${encodeURIComponent(fileKey)}/variables/local`, {
26
+ headers: { "X-Figma-Token": token }
27
+ });
28
+ if (response.ok) {
29
+ try {
30
+ return await response.json();
31
+ } catch {
32
+ throw new Error("Figma API \uC751\uB2F5\uC744 JSON\uC73C\uB85C \uD30C\uC2F1\uD560 \uC218 \uC5C6\uC2B5\uB2C8\uB2E4.");
33
+ }
34
+ }
35
+ if (response.status === 403) throw new Error("Figma Variables API \uC811\uADFC\uC774 \uAC70\uBD80\uB418\uC5C8\uC2B5\uB2C8\uB2E4. Figma plan, \uD574\uB2F9 file \uC811\uADFC \uAD8C\uD55C, token scope(file_variables:read)\uB97C \uD655\uC778\uD574 \uC8FC\uC138\uC694.");
36
+ if (response.status === 429) throw new Error("Figma API rate limit\uC5D0 \uB3C4\uB2EC\uD588\uC2B5\uB2C8\uB2E4. \uC7A0\uC2DC \uD6C4 \uB2E4\uC2DC \uC2E4\uD589\uD574 \uC8FC\uC138\uC694.");
37
+ throw new Error(`Figma API \uC694\uCCAD\uC5D0 \uC2E4\uD328\uD588\uC2B5\uB2C8\uB2E4. (status ${response.status})`);
38
+ }
39
+
40
+ // src/sync.ts
41
+ var readJson = async (path) => {
42
+ try {
43
+ return JSON.parse(await readFile(path, "utf8"));
44
+ } catch (error) {
45
+ if (error instanceof SyntaxError) throw new Error(`Invalid JSON: ${path}`);
46
+ throw error;
47
+ }
48
+ };
49
+ var readSnapshot = async (path) => {
50
+ try {
51
+ const value = await readJson(path);
52
+ if (!isDesignTokenArray(value)) throw new Error("snapshot\uC740 DesignToken \uBC30\uC5F4\uC774\uC5B4\uC57C \uD569\uB2C8\uB2E4.");
53
+ return value;
54
+ } catch (error) {
55
+ if (error.code === "ENOENT") return [];
56
+ throw error;
57
+ }
58
+ };
59
+ var save = async (path, contents) => {
60
+ await mkdir(dirname(path), { recursive: true });
61
+ await writeFile(path, contents);
62
+ };
63
+ var render = (tokens, options) => ({
64
+ "tokens-json": renderTokensJson,
65
+ "theme-ts": (value) => renderTheme(value, options.exportName),
66
+ "variables-css": renderCssVariables,
67
+ "tokens-scss": renderScssVariables,
68
+ "tailwind-css": renderTailwindTheme,
69
+ "tokens-dtcg-json": renderDtcgJson
70
+ })[options.format](tokens);
71
+ var skippedMessage = (name, reason, collection) => {
72
+ const suffix = collection ? ` (collection: ${collection})` : "";
73
+ return reason === "unclassified-float" ? `unclassified FLOAT variable skipped: ${name}${suffix}` : `unsupported type skipped: ${name}${suffix}`;
74
+ };
75
+ var aliasSkippedMessage = (name, reason, collection) => {
76
+ const suffix = collection ? ` (collection: ${collection})` : "";
77
+ return `alias ${reason.replace(/^alias-/, "").replace(/-/g, " ")} skipped: ${name}${suffix}`;
78
+ };
79
+ async function sync(options, log = console.log, warn = console.warn) {
80
+ if (!options.input && (!options.figmaToken || !options.fileKey)) throw new Error("--input \uB610\uB294 FIGMA_TOKEN\uACFC FIGMA_FILE_KEY\uAC00 \uD544\uC694\uD569\uB2C8\uB2E4.");
81
+ const raw = options.input ? await readJson(options.input) : await fetchFigmaVariables(options.fileKey, options.figmaToken);
82
+ const current = isDesignTokenArray(raw) ? raw : normalizeFigmaVariables(raw, {
83
+ onUnsupported: (name, reason, collection) => warn(skippedMessage(name, reason, collection)),
84
+ onAliasWarning: (name, reason, collection) => warn(aliasSkippedMessage(name, reason, collection))
85
+ });
86
+ const diffs = diffTokens(await readSnapshot(options.snapshot), current);
87
+ for (const type of ["added", "changed", "removed"]) log(`${type[0].toUpperCase()}${type.slice(1)}: ${diffs.filter((diff) => diff.type === type).length}`);
88
+ diffs.forEach((diff) => log(`${diff.type} ${diff.token.path.join("/")}`));
89
+ if (options.dryRun) return;
90
+ await save(options.output, render(current, options));
91
+ await save(options.snapshot, renderTokensJson(current));
92
+ }
93
+
94
+ // src/index.ts
95
+ var format = (value) => {
96
+ const formats = ["tokens-json", "theme-ts", "variables-css", "tokens-scss", "tailwind-css", "tokens-dtcg-json"];
97
+ if (!formats.includes(value)) throw new InvalidArgumentError(`format\uC740 ${formats.join(", ")} \uC911 \uD558\uB098\uC5EC\uC57C \uD569\uB2C8\uB2E4.`);
98
+ return value;
99
+ };
100
+ var program = new Command().name("figma-token").description("Figma Variables\uB97C \uB85C\uCEEC \uB514\uC790\uC778 \uD1A0\uD070\uC73C\uB85C \uB3D9\uAE30\uD654\uD569\uB2C8\uB2E4.");
101
+ program.command("sync").option("--input <path>", "\uB85C\uCEEC Figma Variables JSON").option("--output <path>", "\uCD9C\uB825 \uD30C\uC77C", "./tokens.json").option("--snapshot <path>", "snapshot \uD30C\uC77C", ".figma-token/snapshot.json").option("--format <format>", "\uCD9C\uB825 \uD3EC\uB9F7", format, "tokens-json").option("--export-name <name>", "theme.ts export \uC774\uB984", "theme").option("--figma-token <token>", "Figma token").option("--file-key <key>", "Figma file key").option("--dry-run", "\uD30C\uC77C\uC744 \uC4F0\uC9C0 \uC54A\uACE0 diff\uB9CC \uCD9C\uB825", false).action(async (options) => sync({
102
+ ...options,
103
+ figmaToken: options.figmaToken ?? process.env.FIGMA_TOKEN,
104
+ fileKey: options.fileKey ?? process.env.FIGMA_FILE_KEY
105
+ }));
106
+ program.parseAsync().catch((error) => {
107
+ console.error(`Error: ${error instanceof Error ? error.message : String(error)}`);
108
+ process.exitCode = 1;
109
+ });
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "figma-token",
3
+ "version": "0.1.0",
4
+ "description": "Export Figma Variables to design token files.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "bin": {
8
+ "figma-token": "dist/index.js"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/lee090626/Project-F.git",
13
+ "directory": "packages/cli"
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "README.md"
18
+ ],
19
+ "dependencies": {
20
+ "commander": "^14.0.0",
21
+ "dotenv": "^17.0.0",
22
+ "@lee090626/core": "0.1.0"
23
+ },
24
+ "devDependencies": {
25
+ "tsup": "^8.5.0"
26
+ },
27
+ "scripts": {
28
+ "build": "tsup",
29
+ "test": "vitest run"
30
+ }
31
+ }