@sun-asterisk/sungen 2.5.0 → 2.5.1

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 (129) hide show
  1. package/README.md +88 -7
  2. package/dist/cli/commands/add.d.ts.map +1 -1
  3. package/dist/cli/commands/add.js +109 -9
  4. package/dist/cli/commands/add.js.map +1 -1
  5. package/dist/cli/commands/figma.d.ts +11 -0
  6. package/dist/cli/commands/figma.d.ts.map +1 -0
  7. package/dist/cli/commands/figma.js +178 -0
  8. package/dist/cli/commands/figma.js.map +1 -0
  9. package/dist/cli/index.js +4 -2
  10. package/dist/cli/index.js.map +1 -1
  11. package/dist/orchestrator/ai-rules-updater.d.ts.map +1 -1
  12. package/dist/orchestrator/ai-rules-updater.js +2 -0
  13. package/dist/orchestrator/ai-rules-updater.js.map +1 -1
  14. package/dist/orchestrator/figma/figma-scaffolder-helpers.d.ts +33 -0
  15. package/dist/orchestrator/figma/figma-scaffolder-helpers.d.ts.map +1 -0
  16. package/dist/orchestrator/figma/figma-scaffolder-helpers.js +135 -0
  17. package/dist/orchestrator/figma/figma-scaffolder-helpers.js.map +1 -0
  18. package/dist/orchestrator/figma/figma-scaffolder-types.d.ts +25 -0
  19. package/dist/orchestrator/figma/figma-scaffolder-types.d.ts.map +1 -0
  20. package/dist/orchestrator/figma/figma-scaffolder-types.js +7 -0
  21. package/dist/orchestrator/figma/figma-scaffolder-types.js.map +1 -0
  22. package/dist/orchestrator/figma/figma-scaffolder.d.ts +23 -0
  23. package/dist/orchestrator/figma/figma-scaffolder.d.ts.map +1 -0
  24. package/dist/orchestrator/figma/figma-scaffolder.js +212 -0
  25. package/dist/orchestrator/figma/figma-scaffolder.js.map +1 -0
  26. package/dist/orchestrator/figma/node-path-collapser.d.ts +16 -0
  27. package/dist/orchestrator/figma/node-path-collapser.d.ts.map +1 -0
  28. package/dist/orchestrator/figma/node-path-collapser.js +37 -0
  29. package/dist/orchestrator/figma/node-path-collapser.js.map +1 -0
  30. package/dist/orchestrator/figma/spec-figma-renderer.d.ts +44 -0
  31. package/dist/orchestrator/figma/spec-figma-renderer.d.ts.map +1 -0
  32. package/dist/orchestrator/figma/spec-figma-renderer.js +45 -0
  33. package/dist/orchestrator/figma/spec-figma-renderer.js.map +1 -0
  34. package/dist/orchestrator/figma/spec-figma-section-renderers.d.ts +23 -0
  35. package/dist/orchestrator/figma/spec-figma-section-renderers.d.ts.map +1 -0
  36. package/dist/orchestrator/figma/spec-figma-section-renderers.js +47 -0
  37. package/dist/orchestrator/figma/spec-figma-section-renderers.js.map +1 -0
  38. package/dist/orchestrator/templates/ai-instructions/claude-cmd-add-screen.md +56 -11
  39. package/dist/orchestrator/templates/ai-instructions/claude-cmd-create-test.md +30 -17
  40. package/dist/orchestrator/templates/ai-instructions/claude-cmd-review.md +4 -3
  41. package/dist/orchestrator/templates/ai-instructions/claude-cmd-run-test.md +33 -1
  42. package/dist/orchestrator/templates/ai-instructions/claude-config.md +1 -0
  43. package/dist/orchestrator/templates/ai-instructions/claude-skill-figma-source.md +151 -0
  44. package/dist/orchestrator/templates/ai-instructions/claude-skill-tc-generation.md +39 -20
  45. package/dist/orchestrator/templates/ai-instructions/claude-skill-tc-review.md +2 -0
  46. package/dist/orchestrator/templates/ai-instructions/copilot-cmd-add-screen.md +53 -9
  47. package/dist/orchestrator/templates/ai-instructions/copilot-cmd-create-test.md +21 -16
  48. package/dist/orchestrator/templates/ai-instructions/copilot-cmd-review.md +4 -3
  49. package/dist/orchestrator/templates/ai-instructions/copilot-cmd-run-test.md +33 -1
  50. package/dist/orchestrator/templates/ai-instructions/copilot-config.md +1 -0
  51. package/dist/orchestrator/templates/ai-instructions/copilot-skill-figma-source.md +151 -0
  52. package/dist/orchestrator/templates/ai-instructions/github-skill-sungen-figma-source.md +151 -0
  53. package/dist/orchestrator/templates/ai-instructions/github-skill-sungen-selector-fix.md +61 -0
  54. package/dist/orchestrator/templates/ai-instructions/github-skill-sungen-tc-generation.md +51 -25
  55. package/dist/orchestrator/templates/ai-instructions/github-skill-sungen-tc-review.md +20 -0
  56. package/dist/tools/figma/figma-auth.d.ts +36 -0
  57. package/dist/tools/figma/figma-auth.d.ts.map +1 -0
  58. package/dist/tools/figma/figma-auth.js +182 -0
  59. package/dist/tools/figma/figma-auth.js.map +1 -0
  60. package/dist/tools/figma/figma-cache.d.ts +45 -0
  61. package/dist/tools/figma/figma-cache.d.ts.map +1 -0
  62. package/dist/tools/figma/figma-cache.js +191 -0
  63. package/dist/tools/figma/figma-cache.js.map +1 -0
  64. package/dist/tools/figma/figma-client-types.d.ts +112 -0
  65. package/dist/tools/figma/figma-client-types.d.ts.map +1 -0
  66. package/dist/tools/figma/figma-client-types.js +7 -0
  67. package/dist/tools/figma/figma-client-types.js.map +1 -0
  68. package/dist/tools/figma/figma-errors.d.ts +49 -0
  69. package/dist/tools/figma/figma-errors.d.ts.map +1 -0
  70. package/dist/tools/figma/figma-errors.js +105 -0
  71. package/dist/tools/figma/figma-errors.js.map +1 -0
  72. package/dist/tools/figma/figma-image-downloader.d.ts +25 -0
  73. package/dist/tools/figma/figma-image-downloader.d.ts.map +1 -0
  74. package/dist/tools/figma/figma-image-downloader.js +128 -0
  75. package/dist/tools/figma/figma-image-downloader.js.map +1 -0
  76. package/dist/tools/figma/figma-node-filter.d.ts +26 -0
  77. package/dist/tools/figma/figma-node-filter.d.ts.map +1 -0
  78. package/dist/tools/figma/figma-node-filter.js +164 -0
  79. package/dist/tools/figma/figma-node-filter.js.map +1 -0
  80. package/dist/tools/figma/figma-rest-client.d.ts +24 -0
  81. package/dist/tools/figma/figma-rest-client.d.ts.map +1 -0
  82. package/dist/tools/figma/figma-rest-client.js +154 -0
  83. package/dist/tools/figma/figma-rest-client.js.map +1 -0
  84. package/dist/tools/figma/figma-url-parser.d.ts +18 -0
  85. package/dist/tools/figma/figma-url-parser.d.ts.map +1 -0
  86. package/dist/tools/figma/figma-url-parser.js +51 -0
  87. package/dist/tools/figma/figma-url-parser.js.map +1 -0
  88. package/dist/utils/exec-file-no-throw.d.ts +20 -0
  89. package/dist/utils/exec-file-no-throw.d.ts.map +1 -0
  90. package/dist/utils/exec-file-no-throw.js +36 -0
  91. package/dist/utils/exec-file-no-throw.js.map +1 -0
  92. package/package.json +1 -1
  93. package/src/cli/commands/add.ts +80 -9
  94. package/src/cli/commands/figma.ts +162 -0
  95. package/src/cli/index.ts +4 -2
  96. package/src/orchestrator/ai-rules-updater.ts +2 -0
  97. package/src/orchestrator/figma/figma-scaffolder-helpers.ts +126 -0
  98. package/src/orchestrator/figma/figma-scaffolder-types.ts +26 -0
  99. package/src/orchestrator/figma/figma-scaffolder.ts +209 -0
  100. package/src/orchestrator/figma/node-path-collapser.ts +38 -0
  101. package/src/orchestrator/figma/spec-figma-renderer.ts +80 -0
  102. package/src/orchestrator/figma/spec-figma-section-renderers.ts +46 -0
  103. package/src/orchestrator/templates/ai-instructions/claude-cmd-add-screen.md +56 -11
  104. package/src/orchestrator/templates/ai-instructions/claude-cmd-create-test.md +30 -17
  105. package/src/orchestrator/templates/ai-instructions/claude-cmd-review.md +4 -3
  106. package/src/orchestrator/templates/ai-instructions/claude-cmd-run-test.md +33 -1
  107. package/src/orchestrator/templates/ai-instructions/claude-config.md +1 -0
  108. package/src/orchestrator/templates/ai-instructions/claude-skill-figma-source.md +151 -0
  109. package/src/orchestrator/templates/ai-instructions/claude-skill-tc-generation.md +39 -20
  110. package/src/orchestrator/templates/ai-instructions/claude-skill-tc-review.md +2 -0
  111. package/src/orchestrator/templates/ai-instructions/copilot-cmd-add-screen.md +53 -9
  112. package/src/orchestrator/templates/ai-instructions/copilot-cmd-create-test.md +21 -16
  113. package/src/orchestrator/templates/ai-instructions/copilot-cmd-review.md +4 -3
  114. package/src/orchestrator/templates/ai-instructions/copilot-cmd-run-test.md +33 -1
  115. package/src/orchestrator/templates/ai-instructions/copilot-config.md +1 -0
  116. package/src/orchestrator/templates/ai-instructions/copilot-skill-figma-source.md +151 -0
  117. package/src/orchestrator/templates/ai-instructions/github-skill-sungen-figma-source.md +151 -0
  118. package/src/orchestrator/templates/ai-instructions/github-skill-sungen-selector-fix.md +61 -0
  119. package/src/orchestrator/templates/ai-instructions/github-skill-sungen-tc-generation.md +51 -25
  120. package/src/orchestrator/templates/ai-instructions/github-skill-sungen-tc-review.md +20 -0
  121. package/src/tools/figma/figma-auth.ts +161 -0
  122. package/src/tools/figma/figma-cache.ts +184 -0
  123. package/src/tools/figma/figma-client-types.ts +125 -0
  124. package/src/tools/figma/figma-errors.ts +127 -0
  125. package/src/tools/figma/figma-image-downloader.ts +112 -0
  126. package/src/tools/figma/figma-node-filter.ts +198 -0
  127. package/src/tools/figma/figma-rest-client.ts +183 -0
  128. package/src/tools/figma/figma-url-parser.ts +55 -0
  129. package/src/utils/exec-file-no-throw.ts +45 -0
@@ -0,0 +1,161 @@
1
+ /**
2
+ * Figma PAT lifecycle: load, save, clear, and safety checks.
3
+ *
4
+ * Storage: <cwd>/.env key=FIGMA_PAT
5
+ * Override: process.env.FIGMA_PAT (CI path — skips file read)
6
+ *
7
+ * Safeguards enforced on every save AND every assertSafeToUse call:
8
+ * 1. .env must appear in .gitignore (abort if not).
9
+ * 2. No tracked file may contain "FIGMA_PAT=" (git grep scan).
10
+ */
11
+
12
+ import * as fs from 'fs';
13
+ import * as path from 'path';
14
+ import { execFileNoThrow } from '../../utils/exec-file-no-throw';
15
+
16
+ const PAT_KEY = 'FIGMA_PAT';
17
+ const ENV_FILE = '.env';
18
+ const GITIGNORE_FILE = '.gitignore';
19
+
20
+ // ---------------------------------------------------------------------------
21
+ // Internal helpers
22
+ // ---------------------------------------------------------------------------
23
+
24
+ function envFilePath(cwd: string): string {
25
+ return path.join(cwd, ENV_FILE);
26
+ }
27
+
28
+ function gitignorePath(cwd: string): string {
29
+ return path.join(cwd, GITIGNORE_FILE);
30
+ }
31
+
32
+ /**
33
+ * Read .env file lines, return empty array when file does not exist.
34
+ */
35
+ function readEnvLines(cwd: string): string[] {
36
+ const fp = envFilePath(cwd);
37
+ if (!fs.existsSync(fp)) return [];
38
+ return fs.readFileSync(fp, 'utf8').split('\n');
39
+ }
40
+
41
+ /**
42
+ * Write lines back to .env, preserving a trailing newline.
43
+ */
44
+ function writeEnvLines(cwd: string, lines: string[]): void {
45
+ fs.writeFileSync(envFilePath(cwd), lines.join('\n'), 'utf8');
46
+ }
47
+
48
+ /**
49
+ * Check whether .env (or .env.*) is covered by .gitignore.
50
+ */
51
+ function isEnvIgnored(cwd: string): boolean {
52
+ const gp = gitignorePath(cwd);
53
+ if (!fs.existsSync(gp)) return false;
54
+ const content = fs.readFileSync(gp, 'utf8');
55
+ // Accept ".env", ".env.local", ".env.*", or "*.env" patterns
56
+ return /^\s*(\.env[\w.*]*|\*\.env)\s*$/m.test(content);
57
+ }
58
+
59
+ /**
60
+ * Run `git grep -l "FIGMA_PAT="` in cwd.
61
+ * Returns list of matching tracked file paths (empty = safe).
62
+ */
63
+ async function findTrackedFilesWithPat(cwd: string): Promise<string[]> {
64
+ const result = await execFileNoThrow('git', ['grep', '-l', `${PAT_KEY}=`], cwd);
65
+ if (result.status === 0 && result.stdout.trim()) {
66
+ return result.stdout.trim().split('\n').filter(Boolean);
67
+ }
68
+ return [];
69
+ }
70
+
71
+ // ---------------------------------------------------------------------------
72
+ // Public API
73
+ // ---------------------------------------------------------------------------
74
+
75
+ /**
76
+ * Load the Figma PAT.
77
+ * Priority: process.env.FIGMA_PAT > .env file.
78
+ * Returns undefined when no PAT is configured.
79
+ */
80
+ export function loadPat(cwd: string): string | undefined {
81
+ if (process.env[PAT_KEY]) return process.env[PAT_KEY];
82
+
83
+ const lines = readEnvLines(cwd);
84
+ for (const line of lines) {
85
+ const trimmed = line.trim();
86
+ if (trimmed.startsWith(`${PAT_KEY}=`)) {
87
+ return trimmed.slice(PAT_KEY.length + 1).trim();
88
+ }
89
+ }
90
+ return undefined;
91
+ }
92
+
93
+ /**
94
+ * Persist PAT to <cwd>/.env, upserting only the FIGMA_PAT key.
95
+ * Aborts with an actionable error if safeguards are violated.
96
+ *
97
+ * @throws Error with remediation instructions on safeguard failure.
98
+ */
99
+ export async function savePat(cwd: string, token: string): Promise<void> {
100
+ // Safeguard 1: .env must be gitignored
101
+ if (!isEnvIgnored(cwd)) {
102
+ throw new Error(
103
+ `Refusing to save FIGMA_PAT: ".env" is not listed in ${GITIGNORE_FILE}.\n` +
104
+ `Add the following line to ${path.join(cwd, GITIGNORE_FILE)} and re-run:\n\n .env\n`,
105
+ );
106
+ }
107
+
108
+ // Safeguard 2: no tracked file may contain FIGMA_PAT=
109
+ const leakedFiles = await findTrackedFilesWithPat(cwd);
110
+ if (leakedFiles.length > 0) {
111
+ throw new Error(
112
+ `Refusing to save FIGMA_PAT: token key found in tracked file(s):\n` +
113
+ leakedFiles.map((f) => ` ${f}`).join('\n') +
114
+ `\n\nRemove FIGMA_PAT from those files, commit the removal, then re-run.\n`,
115
+ );
116
+ }
117
+
118
+ // Upsert: replace existing line or append
119
+ const lines = readEnvLines(cwd);
120
+ const idx = lines.findIndex((l) => l.trim().startsWith(`${PAT_KEY}=`));
121
+ const newLine = `${PAT_KEY}=${token}`;
122
+ if (idx >= 0) {
123
+ lines[idx] = newLine;
124
+ } else {
125
+ // Ensure file ends with newline before appending
126
+ if (lines.length > 0 && lines[lines.length - 1] !== '') {
127
+ lines.push('');
128
+ }
129
+ lines.push(newLine);
130
+ }
131
+ writeEnvLines(cwd, lines);
132
+ }
133
+
134
+ /**
135
+ * Remove FIGMA_PAT line from <cwd>/.env.
136
+ * No-op when key is not present.
137
+ */
138
+ export function clearPat(cwd: string): void {
139
+ const lines = readEnvLines(cwd);
140
+ const filtered = lines.filter((l) => !l.trim().startsWith(`${PAT_KEY}=`));
141
+ if (filtered.length !== lines.length) {
142
+ writeEnvLines(cwd, filtered);
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Run tracked-file scan on every Figma CLI / skill invocation.
148
+ * Aborts with actionable message if FIGMA_PAT= is found in any tracked file.
149
+ *
150
+ * @throws Error with remediation instructions on safeguard failure.
151
+ */
152
+ export async function assertSafeToUse(cwd: string): Promise<void> {
153
+ const leakedFiles = await findTrackedFilesWithPat(cwd);
154
+ if (leakedFiles.length > 0) {
155
+ throw new Error(
156
+ `Security check failed: FIGMA_PAT found in tracked file(s):\n` +
157
+ leakedFiles.map((f) => ` ${f}`).join('\n') +
158
+ `\n\nRemove FIGMA_PAT from those files and commit the removal before using Figma commands.\n`,
159
+ );
160
+ }
161
+ }
@@ -0,0 +1,184 @@
1
+ /**
2
+ * Version-keyed disk cache for Figma node JSON and rendered images.
3
+ *
4
+ * Layout:
5
+ * <cwd>/.sungen/figma-cache/<fileKey>/<versionId>/<safeNodeId>.json (filtered node)
6
+ * <cwd>/.sungen/figma-cache/<fileKey>/<versionId>/<safeNodeId>-raw.json (unfiltered API response)
7
+ * <cwd>/.sungen/figma-cache/<fileKey>/<versionId>/<safeNodeId>-<scale>.png
8
+ *
9
+ * Cache directory is created on first write (permissions 0o700 on Unix).
10
+ * `bustOldVersions` retains the N most recently modified version dirs.
11
+ */
12
+
13
+ import * as fs from 'node:fs';
14
+ import * as path from 'node:path';
15
+ import type { FigmaCacheKind } from './figma-client-types';
16
+
17
+ // ---------------------------------------------------------------------------
18
+ // Path helpers
19
+ // ---------------------------------------------------------------------------
20
+
21
+ const CACHE_ROOT = path.join('.sungen', 'figma-cache');
22
+
23
+ /** Replace characters unsafe for filenames (colons, slashes, spaces). */
24
+ function safeId(id: string): string {
25
+ return id.replace(/[:/\\s]/g, '_');
26
+ }
27
+
28
+ function cacheDir(cwd: string, fileKey: string, versionId: string): string {
29
+ return path.join(cwd, CACHE_ROOT, safeId(fileKey), safeId(versionId));
30
+ }
31
+
32
+ function cacheFilename(nodeId: string, kind: FigmaCacheKind): string {
33
+ const safe = safeId(nodeId);
34
+ if (kind === 'json') return `${safe}.json`;
35
+ if (kind === 'raw') return `${safe}-raw.json`;
36
+ // kind is like "2x", "1x", etc.
37
+ return `${safe}-${kind}.png`;
38
+ }
39
+
40
+ function ensureDir(dir: string): void {
41
+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
42
+ }
43
+
44
+ // ---------------------------------------------------------------------------
45
+ // Public API
46
+ // ---------------------------------------------------------------------------
47
+
48
+ /**
49
+ * Read a cached entry. Returns `undefined` on cache miss.
50
+ *
51
+ * @param kind 'json' for filtered node data, '<n>x' for PNG images.
52
+ */
53
+ export function get(
54
+ cwd: string,
55
+ fileKey: string,
56
+ versionId: string,
57
+ nodeId: string,
58
+ kind: FigmaCacheKind,
59
+ ): Buffer | undefined {
60
+ const filePath = path.join(
61
+ cacheDir(cwd, fileKey, versionId),
62
+ cacheFilename(nodeId, kind),
63
+ );
64
+ if (!fs.existsSync(filePath)) return undefined;
65
+ try {
66
+ return fs.readFileSync(filePath);
67
+ } catch {
68
+ return undefined;
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Write data to the cache.
74
+ * Creates the version directory (mode 0o700) if it does not exist.
75
+ *
76
+ * @param kind 'json' for filtered node data, '<n>x' for PNG images.
77
+ */
78
+ export function put(
79
+ cwd: string,
80
+ fileKey: string,
81
+ versionId: string,
82
+ nodeId: string,
83
+ kind: FigmaCacheKind,
84
+ data: Buffer | string,
85
+ ): void {
86
+ const dir = cacheDir(cwd, fileKey, versionId);
87
+ ensureDir(dir);
88
+ const filePath = path.join(dir, cacheFilename(nodeId, kind));
89
+ const payload = typeof data === 'string' ? Buffer.from(data, 'utf8') : data;
90
+ // Atomic write: tmp → rename
91
+ const tmpPath = `${filePath}.tmp`;
92
+ fs.writeFileSync(tmpPath, payload, { mode: 0o600 });
93
+ fs.renameSync(tmpPath, filePath);
94
+ }
95
+
96
+ /**
97
+ * Find the newest on-disk version that has a cached raw node JSON for `nodeId`.
98
+ * Returns `undefined` when no usable cache exists.
99
+ *
100
+ * Lets the scaffolder skip an expensive `/v1/files/:key/nodes` call on Starter
101
+ * plans where REST quota is severely restricted.
102
+ */
103
+ export function findLatestCachedVersion(
104
+ cwd: string,
105
+ fileKey: string,
106
+ nodeId: string,
107
+ ): string | undefined {
108
+ const keyDir = path.join(cwd, CACHE_ROOT, safeId(fileKey));
109
+ if (!fs.existsSync(keyDir)) return undefined;
110
+
111
+ let entries: fs.Dirent[];
112
+ try {
113
+ entries = fs.readdirSync(keyDir, { withFileTypes: true });
114
+ } catch {
115
+ return undefined;
116
+ }
117
+
118
+ const rawFilename = cacheFilename(nodeId, 'raw');
119
+ const candidates = entries
120
+ .filter((e) => e.isDirectory())
121
+ .map((e) => {
122
+ const rawPath = path.join(keyDir, e.name, rawFilename);
123
+ if (!fs.existsSync(rawPath)) return undefined;
124
+ let mtime = 0;
125
+ try { mtime = fs.statSync(rawPath).mtimeMs; } catch { /* ignore */ }
126
+ return { versionDir: e.name, mtime };
127
+ })
128
+ .filter((x): x is { versionDir: string; mtime: number } => x !== undefined)
129
+ .sort((a, b) => b.mtime - a.mtime);
130
+
131
+ return candidates[0]?.versionDir;
132
+ }
133
+
134
+ /**
135
+ * Remove all version directories for `fileKey` except the `keep` most recent.
136
+ *
137
+ * Directories are ordered by mtime descending; oldest are deleted first.
138
+ * No-op when fewer than `keep` versions exist.
139
+ *
140
+ * @param keep Number of versions to retain (default 3).
141
+ */
142
+ export function bustOldVersions(
143
+ cwd: string,
144
+ fileKey: string,
145
+ currentVersion: string,
146
+ options: { keep?: number } = {},
147
+ ): void {
148
+ const keep = options.keep ?? 3;
149
+ const keyDir = path.join(cwd, CACHE_ROOT, safeId(fileKey));
150
+
151
+ if (!fs.existsSync(keyDir)) return;
152
+
153
+ let entries: fs.Dirent[];
154
+ try {
155
+ entries = fs.readdirSync(keyDir, { withFileTypes: true });
156
+ } catch {
157
+ return;
158
+ }
159
+
160
+ const versionDirs = entries
161
+ .filter((e) => e.isDirectory())
162
+ .map((e) => {
163
+ const fullPath = path.join(keyDir, e.name);
164
+ let mtime = 0;
165
+ try {
166
+ mtime = fs.statSync(fullPath).mtimeMs;
167
+ } catch { /* ignore */ }
168
+ return { name: e.name, fullPath, mtime };
169
+ })
170
+ // Ensure current version is always kept (bump its mtime artificially)
171
+ .map((d) =>
172
+ d.name === safeId(currentVersion)
173
+ ? { ...d, mtime: Number.MAX_SAFE_INTEGER }
174
+ : d,
175
+ )
176
+ .sort((a, b) => b.mtime - a.mtime); // newest first
177
+
178
+ const toDelete = versionDirs.slice(keep);
179
+ for (const dir of toDelete) {
180
+ try {
181
+ fs.rmSync(dir.fullPath, { recursive: true, force: true });
182
+ } catch { /* best-effort */ }
183
+ }
184
+ }
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Shared types for Figma integration.
3
+ * Used by figma-auth.ts, figma-url-parser.ts, and REST client modules.
4
+ */
5
+
6
+ /** Authentication configuration for Figma API. */
7
+ export interface FigmaAuthConfig {
8
+ /** Personal Access Token — never log this value. */
9
+ pat: string;
10
+ }
11
+
12
+ /** Parsed reference to a specific Figma node. */
13
+ export interface FigmaNodeRef {
14
+ /** Figma file key extracted from URL (alphanumeric, 20+ chars). */
15
+ fileKey: string;
16
+ /** Node ID in API form: "1:23" (colon-separated). Absent when URL has no node-id param. */
17
+ nodeId?: string;
18
+ }
19
+
20
+ /** Minimal /v1/me response shape used for PAT validation. */
21
+ export interface FigmaMeResponse {
22
+ id: string;
23
+ email: string;
24
+ handle: string;
25
+ }
26
+
27
+ /** Result of a PAT validation call. */
28
+ export interface FigmaAuthCheckResult {
29
+ valid: boolean;
30
+ handle?: string;
31
+ email?: string;
32
+ error?: string;
33
+ }
34
+
35
+ // ---------------------------------------------------------------------------
36
+ // REST client response types (added in Phase 2)
37
+ // ---------------------------------------------------------------------------
38
+
39
+ /** Raw Figma node as returned by the API (loose — unknown fields allowed). */
40
+ export interface FigmaRawNode {
41
+ id: string;
42
+ name: string;
43
+ type: string;
44
+ /** Text content for TEXT nodes. */
45
+ characters?: string;
46
+ /** Component set variant properties. */
47
+ componentPropertyDefinitions?: Record<string, unknown>;
48
+ /** Variant property values for COMPONENT nodes. */
49
+ componentProperties?: Record<string, { value: string; type: string }>;
50
+ /** Bounding box from absoluteBoundingBox. */
51
+ absoluteBoundingBox?: { x: number; y: number; width: number; height: number };
52
+ /** Child nodes. */
53
+ children?: FigmaRawNode[];
54
+ /** Allow any additional Figma fields. */
55
+ [key: string]: unknown;
56
+ }
57
+
58
+ /** Response from GET /v1/files/:key/nodes */
59
+ export interface FigmaFileNodesResponse {
60
+ nodes: Record<string, { document: FigmaRawNode } | null>;
61
+ /** Current file version ID. */
62
+ version: string;
63
+ }
64
+
65
+ /** Response from GET /v1/images/:key */
66
+ export interface FigmaImageUrlsResponse {
67
+ /** Map of nodeId → signed S3 URL (transient — do not persist). */
68
+ images: Record<string, string | null>;
69
+ err: string | null;
70
+ }
71
+
72
+ // ---------------------------------------------------------------------------
73
+ // Filtered node types (output of figma-node-filter.ts)
74
+ // ---------------------------------------------------------------------------
75
+
76
+ /** Role inferred from component name suffix. */
77
+ export type FigmaNodeRole = 'button' | 'textbox' | 'link' | 'image' | null;
78
+
79
+ /** Pruned, minimal Figma node for downstream LLM / test generation. */
80
+ export interface FilteredFigmaNode {
81
+ id: string;
82
+ name: string;
83
+ type: string;
84
+ /** Text content (TEXT nodes only). */
85
+ text?: string;
86
+ /** Component set name (COMPONENT / COMPONENT_SET nodes). */
87
+ componentName?: string;
88
+ /** Variant property map from componentProperties. */
89
+ variantProps?: Record<string, string>;
90
+ /** Inferred accessibility / interaction role. */
91
+ role?: FigmaNodeRole;
92
+ /** Bounding rectangle in file coordinates. */
93
+ boundingBox?: { x: number; y: number; w: number; h: number };
94
+ children: FilteredFigmaNode[];
95
+ }
96
+
97
+ /** A single text label extracted from the node tree. */
98
+ export interface FigmaTextLabel {
99
+ text: string;
100
+ nodePath: string;
101
+ }
102
+
103
+ /** A single variant definition extracted from a COMPONENT_SET. */
104
+ export interface FigmaVariantDefinition {
105
+ nodeId: string;
106
+ name: string;
107
+ variantProps: Record<string, string>;
108
+ }
109
+
110
+ // ---------------------------------------------------------------------------
111
+ // Image options
112
+ // ---------------------------------------------------------------------------
113
+
114
+ export interface FigmaImageOptions {
115
+ /** Render scale. Default 2. */
116
+ scale?: number;
117
+ /** Image format. Default 'png'. */
118
+ format?: 'png' | 'jpg' | 'svg' | 'pdf';
119
+ }
120
+
121
+ // ---------------------------------------------------------------------------
122
+ // Cache types
123
+ // ---------------------------------------------------------------------------
124
+
125
+ export type FigmaCacheKind = 'json' | 'raw' | `${number}x`;
@@ -0,0 +1,127 @@
1
+ /**
2
+ * Typed error classes for Figma API failures.
3
+ * Each error carries an actionable remediation message for CLI output.
4
+ *
5
+ * Never include PAT values in error messages or remediation text.
6
+ */
7
+
8
+ // ---------------------------------------------------------------------------
9
+ // Base
10
+ // ---------------------------------------------------------------------------
11
+
12
+ /** Common base for all Figma errors. */
13
+ abstract class FigmaBaseError extends Error {
14
+ /** Human-readable instruction shown to the user on failure. */
15
+ readonly remediation: string;
16
+
17
+ constructor(message: string, remediation: string) {
18
+ super(message);
19
+ this.name = this.constructor.name;
20
+ this.remediation = remediation;
21
+ }
22
+ }
23
+
24
+ // ---------------------------------------------------------------------------
25
+ // Concrete error types
26
+ // ---------------------------------------------------------------------------
27
+
28
+ /**
29
+ * HTTP 401 — PAT is missing, expired, or invalid.
30
+ */
31
+ export class FigmaAuthError extends FigmaBaseError {
32
+ constructor(message = 'Figma authentication failed (401).') {
33
+ super(
34
+ message,
35
+ 'Your Figma PAT is invalid or expired. Re-authenticate by running: sungen figma auth set',
36
+ );
37
+ }
38
+ }
39
+
40
+ /**
41
+ * HTTP 403 / 404 — PAT valid but insufficient access, or resource not found.
42
+ */
43
+ export class FigmaAccessError extends FigmaBaseError {
44
+ readonly statusCode: 403 | 404;
45
+
46
+ constructor(statusCode: 403 | 404, message?: string) {
47
+ const defaultMsg =
48
+ statusCode === 403
49
+ ? 'Figma access denied (403).'
50
+ : 'Figma resource not found (404).';
51
+ const remediation =
52
+ statusCode === 403
53
+ ? 'Request view access to the Figma file, or ensure your PAT has the file:read scope.'
54
+ : 'Check the Figma URL — the file key or node ID may be incorrect.';
55
+ super(message ?? defaultMsg, remediation);
56
+ this.statusCode = statusCode;
57
+ }
58
+ }
59
+
60
+ /**
61
+ * HTTP 429 — Rate limited by Figma API.
62
+ */
63
+ export class FigmaRateLimitError extends FigmaBaseError {
64
+ /** Seconds to wait before retrying, parsed from Retry-After header. */
65
+ readonly retryAfter: number;
66
+ readonly planTier?: string;
67
+ readonly bucket?: string;
68
+ readonly upgradeLink?: string;
69
+
70
+ constructor(
71
+ retryAfter: number,
72
+ message?: string,
73
+ opts: { planTier?: string; bucket?: string; upgradeLink?: string } = {},
74
+ ) {
75
+ const human = formatRetryAfter(retryAfter);
76
+ const tierSuffix = opts.planTier ? ` (plan: ${opts.planTier}${opts.bucket ? `, bucket: ${opts.bucket}` : ''})` : '';
77
+ const msg = message ?? `Figma API rate limit hit (429)${tierSuffix}. Retry after ${human}.`;
78
+
79
+ // Multi-day Retry-After → almost certainly the Starter-tier daily quota.
80
+ const multiDay = retryAfter > 24 * 60 * 60;
81
+ const remediation = multiDay
82
+ ? [
83
+ `This is a Figma ${opts.planTier ?? 'Starter'}-tier daily/multi-day REST quota (not a transient burst).`,
84
+ 'To continue today you must either:',
85
+ ' 1) Re-run with a cached version: sungen add --screen <name> --figma <url> (no --refresh)',
86
+ ' → reuses .sungen/figma-cache raw JSON; skips the /nodes API call entirely.',
87
+ opts.upgradeLink ? ` 2) Upgrade the Figma account: ${opts.upgradeLink}` : ' 2) Upgrade the Figma account to a paid tier.',
88
+ ' 3) Wait for the quota window to reset.',
89
+ ].join('\n')
90
+ : 'Wait and retry, or reduce request frequency. In the meantime you can re-run without --refresh to reuse cached node JSON.';
91
+
92
+ super(msg, remediation);
93
+ this.retryAfter = retryAfter;
94
+ this.planTier = opts.planTier;
95
+ this.bucket = opts.bucket;
96
+ this.upgradeLink = opts.upgradeLink;
97
+ }
98
+ }
99
+
100
+ /** Turn `369943` → `4d 6h 45m`. */
101
+ function formatRetryAfter(secs: number): string {
102
+ if (!Number.isFinite(secs) || secs <= 0) return `${secs}s`;
103
+ const d = Math.floor(secs / 86400);
104
+ const h = Math.floor((secs % 86400) / 3600);
105
+ const m = Math.floor((secs % 3600) / 60);
106
+ const parts: string[] = [];
107
+ if (d) parts.push(`${d}d`);
108
+ if (h) parts.push(`${h}h`);
109
+ if (m && !d) parts.push(`${m}m`);
110
+ if (!parts.length) parts.push(`${secs}s`);
111
+ return `${parts.join(' ')} (${secs}s)`;
112
+ }
113
+
114
+ /**
115
+ * Network / transport failure (timeout, DNS, stream error, etc.).
116
+ */
117
+ export class FigmaNetworkError extends FigmaBaseError {
118
+ readonly cause: unknown;
119
+
120
+ constructor(message: string, cause?: unknown) {
121
+ super(
122
+ message,
123
+ 'Check your internet connection and verify api.figma.com is reachable. If the issue persists, retry.',
124
+ );
125
+ this.cause = cause;
126
+ }
127
+ }