@nodii/dev 0.6.172 → 0.6.174

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/dist/surface.js CHANGED
@@ -22,13 +22,15 @@
22
22
  // committed trees (no Figma call) and refuses any product page whose
23
23
  // generated file differs.
24
24
  //
25
- // Figma reads (only with --fetch) are paced to one call per 8 s or slower;
26
- // a 429 waits out Retry-After, or 30 s doubling when Figma sends none.
25
+ // Figma reads (only with --fetch) go through the machine's shared client
26
+ // (./figma-rest.js, cli-170): one call per 8 s per token across the machine, a
27
+ // 429 retried at most twice after Retry-After, pinned tree reads cached.
27
28
  import { execFileSync } from "node:child_process";
28
29
  import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from "node:fs";
29
30
  import { tmpdir } from "node:os";
30
31
  import { basename, dirname, join, relative, resolve } from "node:path";
31
32
  import { CliError, VERSION, devRequest, readConfig } from "./core.js";
33
+ import { MIN_FIGMA_INTERVAL_MS, figmaRest } from "./figma-rest.js";
32
34
  import { createRenderer } from "./vendor/render-templates.mjs";
33
35
 
34
36
  /** Bumped whenever the emitted code changes shape for the same inputs. */
@@ -47,9 +49,7 @@ const COLOUR_CATALOG = "packages/tokens/src/generated/colour-v2-catalog.json";
47
49
  const TYPE_EDGES_CATALOG = "packages/tokens/src/generated/type-edges-v2-catalog.json";
48
50
  /** How many versions to read back from the newest, looking for the one the last publish closed (8 s each). */
49
51
  const MAX_VERSION_READS = 40;
50
- const FIGMA_API = "https://api.figma.com/v1";
51
- export const MIN_FIGMA_INTERVAL_MS = 8_000;
52
- const BACKOFF_MS = 30_000;
52
+ export { MIN_FIGMA_INTERVAL_MS };
53
53
  const SET_KEY = /^[0-9a-f]{40}$/;
54
54
  /** A set's node id, as the REST API (5:0) or a Figma URL (5-0) writes it. */
55
55
  const NODE_ID = /^(\d+)[:-](\d+)$/;
@@ -161,38 +161,12 @@ export const generatedPath = (root, row) => join(root, row.sourcePath.replace(/\
161
161
  // ---------------------------------------------------------------------------
162
162
 
163
163
  /**
164
- * A Figma REST GET paced to one call per `minIntervalMs` across the whole run.
165
- * A 429 waits Retry-After seconds, or 30 s doubling; a 5xx waits 30 s doubling.
164
+ * The Figma REST GET of a Surface read: the machine's shared client (cli-170),
165
+ * with this command's waits. Calls on one token are queued across the machine,
166
+ * one per `minIntervalMs`; a 429 waits Retry-After and is sent again at most
167
+ * twice; a 5xx waits 30 s doubling; a read at a pinned version is cached.
166
168
  */
167
- export function pacedFigma(token, { fetchImpl = fetch, sleep = (ms) => new Promise((r) => setTimeout(r, ms)), now = () => Date.now(), minIntervalMs = MIN_FIGMA_INTERVAL_MS, attempts = 5, log = () => {} } = {}) {
168
- let last = -Infinity;
169
- return async function figmaGet(path) {
170
- for (let attempt = 1; ; attempt++) {
171
- const wait = last + minIntervalMs - now();
172
- if (wait > 0) await sleep(wait);
173
- last = now();
174
- let response;
175
- try {
176
- response = await fetchImpl(`${FIGMA_API}${path}`, { headers: { "X-Figma-Token": token }, signal: AbortSignal.timeout(180_000) });
177
- } catch (error) {
178
- if (attempt >= attempts) throw new CliError("FIGMA_UNREACHABLE", `Cannot reach Figma: ${error.message}`);
179
- await sleep(BACKOFF_MS);
180
- continue;
181
- }
182
- if (response.ok) return response.json();
183
- // A 429 is never retried: it means stop reading (DS-249 ruling), so it falls through to the throw.
184
- if (response.status >= 500 && attempt < attempts) {
185
- const ms = BACKOFF_MS * 2 ** (attempt - 1);
186
- log(`Figma answered ${response.status} for ${path.split("?")[0]}; waiting ${Math.round(ms / 1000)} s`);
187
- await sleep(ms);
188
- continue;
189
- }
190
- const code = { 404: "FIGMA_NOT_FOUND", 403: "FIGMA_FORBIDDEN", 429: "FIGMA_RATE_LIMITED" }[response.status] ?? "FIGMA_ERROR";
191
- const retryAfter = Number(response.headers?.get?.("retry-after"));
192
- throw new CliError(code, `Figma answered ${response.status} for ${path.split("?")[0]}`, 1, { status: response.status, ...(retryAfter > 0 ? { retryAfterSeconds: retryAfter } : {}) });
193
- }
194
- };
195
- }
169
+ export const pacedFigma = (token, options = {}) => figmaRest(token, options);
196
170
 
197
171
  /** component-set key → node id in one Surfaces file, one call per file per run. */
198
172
  export async function setIndex(figmaGet, fileKey) {
@@ -3500,6 +3474,23 @@ export async function surfaceCommand(action, rest, f, deps = {}) {
3500
3474
  const notYetRows = f.all && !f.fetch ? selected.filter((row) => !generated(row)) : [];
3501
3475
  const notYet = notYetRows.map((row) => row.id);
3502
3476
 
3477
+ // Preflight every target before fetching/saving trees or writing any page. Even an
3478
+ // invalid mark can contain a user's patch, so it must not be silently erased.
3479
+ if (f.write) {
3480
+ const protectedFiles = [];
3481
+ for (const row of targets) {
3482
+ const path = generatedPath(root, row);
3483
+ if (!existsSync(path)) continue;
3484
+ const marks = readFileSync(path, "utf8").split(/\r?\n/).flatMap((line, index) => {
3485
+ if (!/\/\*\s*@handrolled\b/.test(line)) return [];
3486
+ const key = /@handrolled\s+([A-Z][A-Z0-9]*(?:-[A-Z0-9]+)*-\d+)\s*:/.exec(line)?.[1];
3487
+ return [`${key ?? "missing issue key"} at line ${index + 1}`];
3488
+ });
3489
+ if (marks.length) protectedFiles.push(`${relative(root, path)}: ${marks.join(", ")}`);
3490
+ }
3491
+ if (protectedFiles.length) throw new CliError("SURFACE_HANDROLLED", `Refusing to overwrite handrolled Surfaces:\n${protectedFiles.join("\n")}\nGenerate without --write to inspect the replacement and resolve the marked patches explicitly.`, 1);
3492
+ }
3493
+
3503
3494
  let figmaGet = injected;
3504
3495
  const indexes = new Map();
3505
3496
  const trees = new Map();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nodii/dev",
3
- "version": "0.6.172",
3
+ "version": "0.6.174",
4
4
  "description": "Agent CLI for Nodii Dev, reached through the platform edge",
5
5
  "type": "module",
6
6
  "bin": {
@@ -16,9 +16,9 @@
16
16
  },
17
17
  "scripts": {
18
18
  "build": "node build.mjs",
19
- "lint": "node --check src/*.js && node --check test/*.test.js && node --check test/helpers/*.js && node --check scripts/*.mjs",
19
+ "lint": "node --check src/*.js && node --check test/*.test.js && node --check test/setup.mjs && node --check test/helpers/*.js && node --check scripts/*.mjs",
20
20
  "format": "git diff --check",
21
- "test": "node --test test/*.test.js",
21
+ "test": "node --test --import ./test/setup.mjs test/*.test.js",
22
22
  "pack:smoke": "node scripts/pack-smoke.mjs",
23
23
  "edge-routes:refresh": "node scripts/edge-routes.mjs",
24
24
  "edge-routes:check": "node scripts/edge-routes.mjs --check",