@nodii/dev 0.6.112 → 0.6.113

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/README.md CHANGED
@@ -884,8 +884,17 @@ product-page catalog names the Surface by its published component-set key
884
884
 
885
885
  - **`--fetch`** reads the Surface set from Figma's REST nodes API with
886
886
  `FIGMA_PAT` (read-only, one call per 8 s, a 429 waits Retry-After or 30 s
887
- doubling), at the version the committed tree pins (or `--figma-version`, or
888
- the Surface reference's). Without it, the committed tree under
887
+ doubling), at the version the file's last library publish closed (MW8-17),
888
+ by the design system's snapshot-pin rule: the publish time is the newest
889
+ `updated_at` across the file's published components, component sets and
890
+ styles, and the version read is the newest one whose own `lastModified` is
891
+ at or before it. A later unpublished save is never read, and a committed
892
+ tree's older version does not hold a refetch back. The `Components
893
+ published` label is logged, not required, because a publish can leave its
894
+ version unlabelled. A file with no published rows, or with no version the
895
+ publish closed among its newest 40, is refused with
896
+ `SURFACE_FILE_NOT_PUBLISHED`. `--figma-version V` names a version outright.
897
+ Without `--fetch`, the committed tree under
889
898
  `docs/figma-ux-ds/figma/surface-trees/<fileKey>/<componentSetKey>.json` is
890
899
  read and Figma is not called.
891
900
  - Each DS instance is emitted through its Code Connect template exactly as the
@@ -923,8 +932,8 @@ product-page catalog names the Surface by its published component-set key
923
932
  `data.stopped`) and exits 1. A Surface named on its own fails with
924
933
  `FIGMA_RATE_LIMITED`. With `--write`, each tree is saved as soon as it is read,
925
934
  so a stopped or killed run keeps its reads. Add `--resume` to carry on: it keeps
926
- a saved tree and reads only the rest. With `--figma-version V`, it keeps only
927
- trees read at `V`.
935
+ a saved tree only when it was read at the version this run reads (the file's
936
+ last publish, or `--figma-version V`), and reads the rest.
928
937
  - **Table assembly.** A table drawn as frames goes in the DS `Table`'s anatomy. A
929
938
  cell that is no Table part is held in a `TableCell`, and a wrapped cell takes its
930
939
  row's density.
package/dist/core.js CHANGED
@@ -4,7 +4,7 @@ import { chmod, mkdir, readFile, rm, writeFile } from "node:fs/promises";
4
4
  import { homedir, platform } from "node:os";
5
5
  import { dirname, join } from "node:path";
6
6
 
7
- export const VERSION = "0.6.112";
7
+ export const VERSION = "0.6.113";
8
8
  export const configPath = () =>
9
9
  process.env.NODII_DEV_CONFIG_PATH ??
10
10
  join(homedir(), ".config/nodii-dev/config.json");
package/dist/surface.js CHANGED
@@ -6,8 +6,9 @@
6
6
  // Figma: the product-page catalog (packages/blocks/src/product-pages/
7
7
  // catalog.ts) names each Surface by its published component-set key (MW8-20,
8
8
  // never a node id); the Surface's frame tree is read from Figma's REST nodes
9
- // API at a pinned file version and committed, trimmed, under
10
- // docs/figma-ux-ds/figma/surface-trees/; every DS instance is emitted through
9
+ // API at the file's last library publish (MW8-17: generation reads the
10
+ // published Surface, never a later unpublished save) and committed, trimmed,
11
+ // under docs/figma-ux-ds/figma/surface-trees/; every DS instance is emitted through
11
12
  // its Code Connect template exactly as the template gives it (component,
12
13
  // props, import), run by the design system's own template harness
13
14
  // (./vendor/render-templates.mjs, vendored byte-for-byte); auto-layout becomes
@@ -39,7 +40,8 @@ const DS_FILE_META = "infra/figma/figma-file.json";
39
40
  const ONE_TO_ONE_SNAPSHOT = "docs/figma-ux-ds/figma/1to1/figma-file.snapshot.json";
40
41
  const COLOUR_CATALOG = "packages/tokens/src/generated/colour-v2-catalog.json";
41
42
  const TYPE_EDGES_CATALOG = "packages/tokens/src/generated/type-edges-v2-catalog.json";
42
- const SURFACE_REFERENCE = "docs/figma-ux-ds/figma/surface-reference.json";
43
+ /** How many versions to read back from the newest, looking for the one the last publish closed (8 s each). */
44
+ const MAX_VERSION_READS = 40;
43
45
  const FIGMA_API = "https://api.figma.com/v1";
44
46
  export const MIN_FIGMA_INTERVAL_MS = 8_000;
45
47
  const BACKOFF_MS = 30_000;
@@ -163,6 +165,68 @@ export async function setIndex(figmaGet, fileKey) {
163
165
  return new Map((body.meta?.component_sets ?? []).map((x) => [x.key, x.node_id]));
164
166
  }
165
167
 
168
+ /** The REST endpoints that list a file's published library (as the DS's checkPublishPin reads them). */
169
+ export const LIBRARY_ENDPOINTS = ["components", "component_sets", "styles"];
170
+
171
+ /** A file's published library: `{ components, component_sets, styles }`, each a list of rows. */
172
+ export async function fetchLibrary(figmaGet, fileKey) {
173
+ const library = {};
174
+ for (const endpoint of LIBRARY_ENDPOINTS) {
175
+ const body = await figmaGet(`/files/${encodeURIComponent(fileKey)}/${endpoint}`);
176
+ library[endpoint] = body.meta?.[endpoint] ?? [];
177
+ }
178
+ return library;
179
+ }
180
+
181
+ /** When the library was last published: the newest `updated_at` across its rows, or null. */
182
+ export function libraryPublishedAt(library) {
183
+ let newest = null;
184
+ for (const endpoint of LIBRARY_ENDPOINTS) {
185
+ for (const row of library[endpoint] ?? []) {
186
+ if (typeof row.updated_at !== "string" || !Number.isFinite(Date.parse(row.updated_at))) continue;
187
+ if (newest === null || Date.parse(row.updated_at) > Date.parse(newest)) newest = row.updated_at;
188
+ }
189
+ }
190
+ return newest;
191
+ }
192
+
193
+ /**
194
+ * The version the file's last library publish closed (MW8-17: generation reads the
195
+ * published Surface), by the design system's snapshot-pin rule (checkPublishPin,
196
+ * DS-FIGMA-1TO1-GATE-1). A library publish closes the open version and then stamps
197
+ * the library rows' `updated_at`, so the publish time is the newest `updated_at`
198
+ * across components, component sets and styles, and the pin is the newest version
199
+ * whose own `lastModified` (read with `?depth=1&version=`) is at or before it. A
200
+ * version saved after the publish can hold unpublished edits and is never taken.
201
+ * The "Components published" label is reported, not required: a publish can leave
202
+ * its version unlabelled (CS-3009). A file with no published rows, or no version
203
+ * closed by the publish, is refused.
204
+ */
205
+ export async function lastPublishedVersion(figmaGet, fileKey, { library } = {}) {
206
+ library ??= await fetchLibrary(figmaGet, fileKey);
207
+ const publishedAt = libraryPublishedAt(library);
208
+ if (!publishedAt) throw new CliError("SURFACE_FILE_NOT_PUBLISHED", `${fileKey} has no published library rows, so it has never been published; publish the Surfaces file, or name a version with --figma-version`, 1);
209
+ const at = Date.parse(publishedAt);
210
+ const key = encodeURIComponent(fileKey);
211
+ let path = `/files/${key}/versions?page_size=20`;
212
+ let reads = 0;
213
+ while (path && reads < MAX_VERSION_READS) {
214
+ const body = await figmaGet(path);
215
+ // History comes newest first, and each version closes after the one before it,
216
+ // so the first one closed at or before the publish is the one the publish closed.
217
+ for (const row of body.versions ?? []) {
218
+ if (reads++ >= MAX_VERSION_READS) break;
219
+ const head = await figmaGet(`/files/${key}?depth=1&version=${encodeURIComponent(row.id)}`);
220
+ const closed = Date.parse(head.lastModified);
221
+ if (!Number.isFinite(closed) || closed > at) continue;
222
+ return { version: String(row.id), label: row.label ?? null, closedAt: head.lastModified, publishedAt };
223
+ }
224
+ const next = body.pagination?.next_page;
225
+ path = next ? next.slice(next.indexOf("/files/")) : null;
226
+ }
227
+ throw new CliError("SURFACE_FILE_NOT_PUBLISHED", `${fileKey}: none of its newest ${MAX_VERSION_READS} versions closed at or before its last library publish (${publishedAt}); name a version with --figma-version`, 1);
228
+ }
229
+
166
230
  /** Read one Surface set at a file version and trim it to what the generator reads. */
167
231
  export async function fetchSurfaceTree(figmaGet, row, { index, version }) {
168
232
  const nodeId = index.get(row.componentSetKey);
@@ -817,15 +881,6 @@ function tableOf(node, ctx) {
817
881
  // The command
818
882
  // ---------------------------------------------------------------------------
819
883
 
820
- /** The Figma version to read: --figma-version, the committed tree's, or the Surface reference's pin. */
821
- function pinnedVersion(root, row, flag) {
822
- if (flag && flag !== true) return String(flag);
823
- const path = treePath(root, row);
824
- if (existsSync(path)) return readJson(path).figmaVersion ?? null;
825
- const reference = existsSync(join(root, SURFACE_REFERENCE)) ? readJson(join(root, SURFACE_REFERENCE)) : null;
826
- return reference?.files?.[row.fileKey]?.figmaVersion ?? null;
827
- }
828
-
829
884
  function figmaTokenFrom(env) {
830
885
  const token = env.FIGMA_PAT || env.FIGMA_TOKEN;
831
886
  if (!token) throw new CliError("FIGMA_TOKEN_REQUIRED", "--fetch reads the Surface from Figma's REST API: set FIGMA_PAT to a token that can read the Surfaces file.", 2);
@@ -966,23 +1021,38 @@ export async function surfaceCommand(action, rest, f, deps = {}) {
966
1021
  renameSync(`${path}.tmp`, path);
967
1022
  };
968
1023
  const figmaVersion = f["figma-version"] && f["figma-version"] !== true ? String(f["figma-version"]) : null;
1024
+ // --figma-version names a version outright; otherwise each file is read at its last library publish.
1025
+ // The library read that finds the publish also gives the set index, so it is one read per file.
1026
+ const versions = new Map();
1027
+ const versionFor = async (fileKey) => {
1028
+ if (figmaVersion) return figmaVersion;
1029
+ if (!versions.has(fileKey)) {
1030
+ const library = await fetchLibrary(figmaGet, fileKey);
1031
+ indexes.set(fileKey, new Map(library.component_sets.map((x) => [x.key, x.node_id])));
1032
+ const pin = await lastPublishedVersion(figmaGet, fileKey, { library });
1033
+ log(`${fileKey}: last library publish ${pin.publishedAt} closed version ${pin.version} at ${pin.closedAt}${pin.label ? ` ("${pin.label}")` : " (unlabelled)"}`);
1034
+ versions.set(fileKey, pin.version);
1035
+ }
1036
+ return versions.get(fileKey);
1037
+ };
969
1038
  let stopped = null;
970
1039
  let kept = 0;
971
1040
  for (const row of targets) {
972
1041
  const path = treePath(root, row);
973
1042
  if (f.fetch) {
974
- if (f.resume && existsSync(path)) {
975
- const saved = readJson(path);
976
- if (!figmaVersion || saved.figmaVersion === figmaVersion) {
977
- log(`keeping ${row.componentSetName ?? row.id}: read at version ${saved.figmaVersion ?? "unknown"}`);
978
- trees.set(row, saved);
979
- kept += 1;
980
- continue;
981
- }
982
- }
983
1043
  figmaGet ??= pacedFigma(figmaTokenFrom(env), { log });
984
1044
  let tree;
985
1045
  try {
1046
+ const version = await versionFor(row.fileKey);
1047
+ if (f.resume && existsSync(path)) {
1048
+ const saved = readJson(path);
1049
+ if (saved.figmaVersion === version) {
1050
+ log(`keeping ${row.componentSetName ?? row.id}: read at version ${version}`);
1051
+ trees.set(row, saved);
1052
+ kept += 1;
1053
+ continue;
1054
+ }
1055
+ }
986
1056
  if (!indexes.has(row.fileKey)) indexes.set(row.fileKey, await setIndex(figmaGet, row.fileKey));
987
1057
  // --all covers a module: one unpublished Surface is reported, and the rest still generate.
988
1058
  // A Surface named on its own stays a refusal, because the run has nothing else to do.
@@ -991,8 +1061,8 @@ export async function surfaceCommand(action, rest, f, deps = {}) {
991
1061
  unpublished.push(row);
992
1062
  continue;
993
1063
  }
994
- log(`reading ${row.componentSetName ?? row.id} from Figma`);
995
- tree = await fetchSurfaceTree(figmaGet, row, { index: indexes.get(row.fileKey), version: pinnedVersion(root, row, f["figma-version"]) });
1064
+ log(`reading ${row.componentSetName ?? row.id} from Figma at version ${version}`);
1065
+ tree = await fetchSurfaceTree(figmaGet, row, { index: indexes.get(row.fileKey), version });
996
1066
  } catch (error) {
997
1067
  // --all keeps what it read and scores it; a Surface named on its own has nothing to keep.
998
1068
  if (!f.all || error?.code !== "FIGMA_RATE_LIMITED") throw error;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nodii/dev",
3
- "version": "0.6.112",
3
+ "version": "0.6.113",
4
4
  "description": "Agent CLI for Nodii Dev, reached through the platform edge",
5
5
  "type": "module",
6
6
  "bin": {