@spectratools/figma-cli 0.2.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.
- package/LICENSE +21 -0
- package/dist/cli.js +33 -0
- package/dist/index.js +201 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 spectra-the-bot
|
|
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/dist/cli.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/cli.ts
|
|
4
|
+
import { readFileSync, realpathSync } from "fs";
|
|
5
|
+
import { dirname, resolve } from "path";
|
|
6
|
+
import { fileURLToPath } from "url";
|
|
7
|
+
import { initTelemetry, shutdownTelemetry } from "@spectratools/cli-shared/telemetry";
|
|
8
|
+
import { Cli } from "incur";
|
|
9
|
+
var __dirname = dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
var pkg = JSON.parse(readFileSync(resolve(__dirname, "../package.json"), "utf8"));
|
|
11
|
+
var cli = Cli.create("figma", {
|
|
12
|
+
version: pkg.version,
|
|
13
|
+
description: "Query Figma REST API data from the command line."
|
|
14
|
+
});
|
|
15
|
+
var isMain = (() => {
|
|
16
|
+
const entrypoint = process.argv[1];
|
|
17
|
+
if (!entrypoint) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
return realpathSync(entrypoint) === realpathSync(fileURLToPath(import.meta.url));
|
|
22
|
+
} catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
})();
|
|
26
|
+
if (isMain) {
|
|
27
|
+
initTelemetry("figma");
|
|
28
|
+
process.on("beforeExit", () => shutdownTelemetry());
|
|
29
|
+
cli.serve();
|
|
30
|
+
}
|
|
31
|
+
export {
|
|
32
|
+
cli
|
|
33
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/cli.ts
|
|
4
|
+
import { readFileSync, realpathSync } from "fs";
|
|
5
|
+
import { dirname, resolve } from "path";
|
|
6
|
+
import { fileURLToPath } from "url";
|
|
7
|
+
import { initTelemetry, shutdownTelemetry } from "@spectratools/cli-shared/telemetry";
|
|
8
|
+
import { Cli } from "incur";
|
|
9
|
+
var __dirname = dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
var pkg = JSON.parse(readFileSync(resolve(__dirname, "../package.json"), "utf8"));
|
|
11
|
+
var cli = Cli.create("figma", {
|
|
12
|
+
version: pkg.version,
|
|
13
|
+
description: "Query Figma REST API data from the command line."
|
|
14
|
+
});
|
|
15
|
+
var isMain = (() => {
|
|
16
|
+
const entrypoint = process.argv[1];
|
|
17
|
+
if (!entrypoint) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
return realpathSync(entrypoint) === realpathSync(fileURLToPath(import.meta.url));
|
|
22
|
+
} catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
})();
|
|
26
|
+
if (isMain) {
|
|
27
|
+
initTelemetry("figma");
|
|
28
|
+
process.on("beforeExit", () => shutdownTelemetry());
|
|
29
|
+
cli.serve();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/api.ts
|
|
33
|
+
import {
|
|
34
|
+
createHttpClient,
|
|
35
|
+
createRateLimiter,
|
|
36
|
+
withRateLimit,
|
|
37
|
+
withRetry
|
|
38
|
+
} from "@spectratools/cli-shared";
|
|
39
|
+
import { z } from "incur";
|
|
40
|
+
var BASE_URL = "https://api.figma.com/v1";
|
|
41
|
+
var RETRY_OPTIONS = { maxRetries: 3, baseMs: 1e3, maxMs: 15e3 };
|
|
42
|
+
var colorSchema = z.object({
|
|
43
|
+
r: z.number(),
|
|
44
|
+
g: z.number(),
|
|
45
|
+
b: z.number(),
|
|
46
|
+
a: z.number()
|
|
47
|
+
});
|
|
48
|
+
var userSchema = z.object({
|
|
49
|
+
id: z.string(),
|
|
50
|
+
handle: z.string(),
|
|
51
|
+
img_url: z.string().optional()
|
|
52
|
+
});
|
|
53
|
+
var componentSchema = z.object({
|
|
54
|
+
key: z.string(),
|
|
55
|
+
name: z.string(),
|
|
56
|
+
description: z.string()
|
|
57
|
+
});
|
|
58
|
+
var styleSchema = z.object({
|
|
59
|
+
key: z.string(),
|
|
60
|
+
name: z.string(),
|
|
61
|
+
style_type: z.string(),
|
|
62
|
+
description: z.string().optional()
|
|
63
|
+
});
|
|
64
|
+
var commentSchema = z.object({
|
|
65
|
+
id: z.string(),
|
|
66
|
+
message: z.string(),
|
|
67
|
+
created_at: z.string(),
|
|
68
|
+
user: userSchema,
|
|
69
|
+
order_id: z.union([z.string(), z.number()]).optional()
|
|
70
|
+
});
|
|
71
|
+
var fileMetaSchema = z.object({
|
|
72
|
+
name: z.string(),
|
|
73
|
+
lastModified: z.string(),
|
|
74
|
+
version: z.string(),
|
|
75
|
+
role: z.string().optional()
|
|
76
|
+
});
|
|
77
|
+
var projectFileSchema = z.object({
|
|
78
|
+
key: z.string(),
|
|
79
|
+
name: z.string(),
|
|
80
|
+
thumbnail_url: z.string().optional(),
|
|
81
|
+
last_modified: z.string()
|
|
82
|
+
});
|
|
83
|
+
var getFileResponseSchema = z.object({
|
|
84
|
+
name: z.string(),
|
|
85
|
+
lastModified: z.string(),
|
|
86
|
+
version: z.string(),
|
|
87
|
+
role: z.string().optional(),
|
|
88
|
+
document: z.unknown(),
|
|
89
|
+
components: z.record(z.string(), componentSchema).optional(),
|
|
90
|
+
styles: z.record(z.string(), styleSchema).optional()
|
|
91
|
+
});
|
|
92
|
+
var getFileNodesResponseSchema = z.object({
|
|
93
|
+
name: z.string(),
|
|
94
|
+
lastModified: z.string(),
|
|
95
|
+
version: z.string(),
|
|
96
|
+
nodes: z.record(z.string(), z.unknown())
|
|
97
|
+
});
|
|
98
|
+
var getImagesResponseSchema = z.object({
|
|
99
|
+
images: z.record(z.string(), z.string().nullable()),
|
|
100
|
+
err: z.string().nullable().optional()
|
|
101
|
+
});
|
|
102
|
+
var getFileStylesResponseSchema = z.object({
|
|
103
|
+
meta: z.object({
|
|
104
|
+
styles: z.array(styleSchema)
|
|
105
|
+
})
|
|
106
|
+
});
|
|
107
|
+
var getFileComponentsResponseSchema = z.object({
|
|
108
|
+
meta: z.object({
|
|
109
|
+
components: z.array(componentSchema)
|
|
110
|
+
})
|
|
111
|
+
});
|
|
112
|
+
var getCommentsResponseSchema = z.object({
|
|
113
|
+
comments: z.array(commentSchema)
|
|
114
|
+
});
|
|
115
|
+
var postCommentResponseSchema = commentSchema;
|
|
116
|
+
var getProjectFilesResponseSchema = z.object({
|
|
117
|
+
name: z.string(),
|
|
118
|
+
files: z.array(projectFileSchema)
|
|
119
|
+
});
|
|
120
|
+
function createFigmaClient(apiKey) {
|
|
121
|
+
const http = createHttpClient({
|
|
122
|
+
baseUrl: BASE_URL,
|
|
123
|
+
defaultHeaders: { "X-Figma-Token": apiKey }
|
|
124
|
+
});
|
|
125
|
+
const acquire = createRateLimiter({ requestsPerSecond: 2 });
|
|
126
|
+
function request(path, schema, query) {
|
|
127
|
+
const opts = query ? { query } : {};
|
|
128
|
+
return withRetry(
|
|
129
|
+
() => withRateLimit(() => http.request(path, opts), acquire),
|
|
130
|
+
RETRY_OPTIONS
|
|
131
|
+
).then((raw) => schema.parse(raw));
|
|
132
|
+
}
|
|
133
|
+
function post(path, schema, body) {
|
|
134
|
+
return withRetry(
|
|
135
|
+
() => withRateLimit(() => http.request(path, { method: "POST", body }), acquire),
|
|
136
|
+
RETRY_OPTIONS
|
|
137
|
+
).then((raw) => schema.parse(raw));
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
/** Fetch a full Figma file. */
|
|
141
|
+
getFile(fileKey, opts) {
|
|
142
|
+
const query = {};
|
|
143
|
+
if (opts?.depth !== void 0) query.depth = opts.depth;
|
|
144
|
+
if (opts?.ids !== void 0) query.ids = opts.ids;
|
|
145
|
+
if (opts?.version !== void 0) query.version = opts.version;
|
|
146
|
+
if (opts?.geometry !== void 0) query.geometry = opts.geometry;
|
|
147
|
+
return request(`/files/${fileKey}`, getFileResponseSchema, query);
|
|
148
|
+
},
|
|
149
|
+
/** Fetch specific nodes from a file. */
|
|
150
|
+
getFileNodes(fileKey, nodeIds) {
|
|
151
|
+
return request(`/files/${fileKey}/nodes`, getFileNodesResponseSchema, {
|
|
152
|
+
ids: nodeIds.join(",")
|
|
153
|
+
});
|
|
154
|
+
},
|
|
155
|
+
/** Export images for the given node IDs. */
|
|
156
|
+
getImages(fileKey, nodeIds, opts) {
|
|
157
|
+
const query = {
|
|
158
|
+
ids: nodeIds.join(",")
|
|
159
|
+
};
|
|
160
|
+
if (opts?.scale !== void 0) query.scale = opts.scale;
|
|
161
|
+
if (opts?.format !== void 0) query.format = opts.format;
|
|
162
|
+
if (opts?.version !== void 0) query.version = opts.version;
|
|
163
|
+
return request(`/images/${fileKey}`, getImagesResponseSchema, query);
|
|
164
|
+
},
|
|
165
|
+
/** List published styles in a file. */
|
|
166
|
+
getFileStyles(fileKey) {
|
|
167
|
+
return request(`/files/${fileKey}/styles`, getFileStylesResponseSchema);
|
|
168
|
+
},
|
|
169
|
+
/** List published components in a file. */
|
|
170
|
+
getFileComponents(fileKey) {
|
|
171
|
+
return request(`/files/${fileKey}/components`, getFileComponentsResponseSchema);
|
|
172
|
+
},
|
|
173
|
+
/** List comments on a file. */
|
|
174
|
+
getComments(fileKey) {
|
|
175
|
+
return request(`/files/${fileKey}/comments`, getCommentsResponseSchema);
|
|
176
|
+
},
|
|
177
|
+
/** Post a comment on a file. */
|
|
178
|
+
postComment(fileKey, message, nodeId) {
|
|
179
|
+
const body = { message };
|
|
180
|
+
if (nodeId !== void 0) {
|
|
181
|
+
body.client_meta = { node_id: nodeId };
|
|
182
|
+
}
|
|
183
|
+
return post(`/files/${fileKey}/comments`, postCommentResponseSchema, body);
|
|
184
|
+
},
|
|
185
|
+
/** List files in a project. */
|
|
186
|
+
getProjectFiles(projectId) {
|
|
187
|
+
return request(`/projects/${projectId}/files`, getProjectFilesResponseSchema);
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// src/auth.ts
|
|
193
|
+
import { z as z2 } from "incur";
|
|
194
|
+
var figmaEnv = z2.object({
|
|
195
|
+
FIGMA_API_KEY: z2.string().describe("Figma personal access token")
|
|
196
|
+
});
|
|
197
|
+
export {
|
|
198
|
+
cli,
|
|
199
|
+
createFigmaClient,
|
|
200
|
+
figmaEnv
|
|
201
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@spectratools/figma-cli",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Figma REST API CLI for spectra-the-bot",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "spectra-the-bot",
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=20"
|
|
10
|
+
},
|
|
11
|
+
"bin": {
|
|
12
|
+
"figma": "./dist/cli.js"
|
|
13
|
+
},
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"incur": "^0.3.0",
|
|
22
|
+
"@spectratools/cli-shared": "0.4.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"typescript": "5.7.3",
|
|
26
|
+
"vitest": "2.1.8"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"README.md"
|
|
31
|
+
],
|
|
32
|
+
"main": "./dist/cli.js",
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsup",
|
|
35
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
36
|
+
"test": "vitest run"
|
|
37
|
+
}
|
|
38
|
+
}
|