@intelligo-dev/cli 1.0.0-beta.13 → 1.0.0-beta.14

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 (171) hide show
  1. package/README.md +41 -2
  2. package/dist/bin.js +37 -0
  3. package/dist/bin.js.map +1 -1
  4. package/dist/commands/add.d.ts +4 -0
  5. package/dist/commands/add.d.ts.map +1 -1
  6. package/dist/commands/add.js +9 -0
  7. package/dist/commands/add.js.map +1 -1
  8. package/dist/commands/create-flow.d.ts +4 -1
  9. package/dist/commands/create-flow.d.ts.map +1 -1
  10. package/dist/commands/create-flow.js +63 -31
  11. package/dist/commands/create-flow.js.map +1 -1
  12. package/dist/commands/create.d.ts +8 -4
  13. package/dist/commands/create.d.ts.map +1 -1
  14. package/dist/commands/create.js +15 -7
  15. package/dist/commands/create.js.map +1 -1
  16. package/dist/commands/doctor.d.ts.map +1 -1
  17. package/dist/commands/doctor.js +21 -19
  18. package/dist/commands/doctor.js.map +1 -1
  19. package/dist/commands/migrate-check.d.ts +20 -1
  20. package/dist/commands/migrate-check.d.ts.map +1 -1
  21. package/dist/commands/migrate-check.js +35 -6
  22. package/dist/commands/migrate-check.js.map +1 -1
  23. package/dist/commands/migrate.d.ts.map +1 -1
  24. package/dist/commands/migrate.js +2 -8
  25. package/dist/commands/migrate.js.map +1 -1
  26. package/dist/commands/sync-messages.d.ts +37 -0
  27. package/dist/commands/sync-messages.d.ts.map +1 -0
  28. package/dist/commands/sync-messages.js +88 -0
  29. package/dist/commands/sync-messages.js.map +1 -0
  30. package/dist/commands/sync-scaffold.d.ts +36 -0
  31. package/dist/commands/sync-scaffold.d.ts.map +1 -0
  32. package/dist/commands/sync-scaffold.js +53 -0
  33. package/dist/commands/sync-scaffold.js.map +1 -0
  34. package/dist/commands/sync.d.ts +103 -0
  35. package/dist/commands/sync.d.ts.map +1 -0
  36. package/dist/commands/sync.js +361 -0
  37. package/dist/commands/sync.js.map +1 -0
  38. package/dist/commands/upgrade-check.d.ts.map +1 -1
  39. package/dist/commands/upgrade-check.js +4 -1
  40. package/dist/commands/upgrade-check.js.map +1 -1
  41. package/dist/manifest.d.ts +27 -0
  42. package/dist/manifest.d.ts.map +1 -1
  43. package/dist/manifest.js +30 -3
  44. package/dist/manifest.js.map +1 -1
  45. package/dist/module-exports.d.ts +63 -0
  46. package/dist/module-exports.d.ts.map +1 -0
  47. package/dist/module-exports.js +231 -0
  48. package/dist/module-exports.js.map +1 -0
  49. package/dist/registry-bundle.d.ts +43 -0
  50. package/dist/registry-bundle.d.ts.map +1 -0
  51. package/dist/registry-bundle.js +74 -0
  52. package/dist/registry-bundle.js.map +1 -0
  53. package/dist/registry-items.d.ts +41 -13
  54. package/dist/registry-items.d.ts.map +1 -1
  55. package/dist/registry-items.js +117 -33
  56. package/dist/registry-items.js.map +1 -1
  57. package/package.json +1 -1
  58. package/src/bin.ts +52 -0
  59. package/src/commands/add.ts +11 -0
  60. package/src/commands/create-flow.ts +82 -26
  61. package/src/commands/create.ts +17 -7
  62. package/src/commands/doctor.ts +27 -27
  63. package/src/commands/migrate-check.ts +51 -10
  64. package/src/commands/migrate.ts +2 -8
  65. package/src/commands/sync-messages.ts +114 -0
  66. package/src/commands/sync-scaffold.ts +83 -0
  67. package/src/commands/sync.ts +534 -0
  68. package/src/commands/upgrade-check.ts +4 -1
  69. package/src/manifest.ts +57 -3
  70. package/src/module-exports.ts +266 -0
  71. package/src/registry-bundle.ts +103 -0
  72. package/src/registry-items.ts +141 -36
  73. package/templates/app-scaffold/intelligo.ts.tpl +6 -47
  74. package/templates/manifest.json +19 -0
  75. package/templates/registry/ai-agent-activity.json +23 -0
  76. package/templates/registry/ai-agent-progress.json +21 -0
  77. package/templates/registry/ai-approval-card.json +27 -0
  78. package/templates/registry/ai-artifact.json +22 -0
  79. package/templates/registry/ai-branch.json +21 -0
  80. package/templates/registry/ai-citations.json +24 -0
  81. package/templates/registry/ai-code-block.json +23 -0
  82. package/templates/registry/ai-composer-menu.json +19 -0
  83. package/templates/registry/ai-file-diff.json +25 -0
  84. package/templates/registry/ai-image-generation.json +23 -0
  85. package/templates/registry/ai-markdown.json +23 -0
  86. package/templates/registry/ai-message-bubble.json +23 -0
  87. package/templates/registry/ai-message-scroller.json +22 -0
  88. package/templates/registry/ai-message.json +21 -0
  89. package/templates/registry/ai-motion.json +19 -0
  90. package/templates/registry/ai-prompt-input.json +28 -0
  91. package/templates/registry/ai-reasoning-text.json +22 -0
  92. package/templates/registry/ai-reasoning.json +22 -0
  93. package/templates/registry/ai-shimmer-text.json +19 -0
  94. package/templates/registry/ai-sidebar.json +25 -0
  95. package/templates/registry/ai-speech-input.json +21 -0
  96. package/templates/registry/ai-streaming-response.json +24 -0
  97. package/templates/registry/ai-suggestion.json +19 -0
  98. package/templates/registry/ai-todo-list.json +22 -0
  99. package/templates/registry/ai-tool-approval.json +26 -0
  100. package/templates/registry/ai-tool-result.json +25 -0
  101. package/templates/registry/alert-dialog.json +23 -0
  102. package/templates/registry/animated-list.json +21 -0
  103. package/templates/registry/app-shell.json +93 -0
  104. package/templates/registry/artifacts.json +82 -0
  105. package/templates/registry/attachment.json +22 -0
  106. package/templates/registry/auth-email-verification.json +39 -0
  107. package/templates/registry/auth-login.json +72 -0
  108. package/templates/registry/auth-password-reset.json +53 -0
  109. package/templates/registry/auth-signup.json +41 -0
  110. package/templates/registry/billing-settings.json +60 -0
  111. package/templates/registry/button.json +22 -0
  112. package/templates/registry/chat-eve.json +19 -0
  113. package/templates/registry/chat-panel.json +30 -0
  114. package/templates/registry/chat-share.json +42 -0
  115. package/templates/registry/chat-widget.json +34 -0
  116. package/templates/registry/chat.json +326 -0
  117. package/templates/registry/checkbox.json +22 -0
  118. package/templates/registry/checkout.json +40 -0
  119. package/templates/registry/collapsible.json +19 -0
  120. package/templates/registry/command.json +23 -0
  121. package/templates/registry/copy-button.json +21 -0
  122. package/templates/registry/dashboard.json +69 -0
  123. package/templates/registry/dialog.json +24 -0
  124. package/templates/registry/document-viewer.json +22 -0
  125. package/templates/registry/dropdown-menu.json +23 -0
  126. package/templates/registry/expandable-tabs.json +22 -0
  127. package/templates/registry/feature-gating.json +73 -0
  128. package/templates/registry/file-upload.json +24 -0
  129. package/templates/registry/hold-action-button.json +22 -0
  130. package/templates/registry/input-group.json +23 -0
  131. package/templates/registry/input.json +19 -0
  132. package/templates/registry/intelligo.json +187 -0
  133. package/templates/registry/invitation-accept.json +64 -0
  134. package/templates/registry/language-switcher.json +29 -0
  135. package/templates/registry/morphing-modal.json +24 -0
  136. package/templates/registry/notification-stack.json +24 -0
  137. package/templates/registry/notifications.json +87 -0
  138. package/templates/registry/onboarding.json +83 -0
  139. package/templates/registry/otp-input.json +22 -0
  140. package/templates/registry/page-header.json +15 -0
  141. package/templates/registry/payment-poll.json +52 -0
  142. package/templates/registry/popover-morph.json +22 -0
  143. package/templates/registry/popover.json +22 -0
  144. package/templates/registry/pricing.json +111 -0
  145. package/templates/registry/privacy-settings.json +65 -0
  146. package/templates/registry/profile-settings.json +68 -0
  147. package/templates/registry/progress.json +19 -0
  148. package/templates/registry/radio-group.json +22 -0
  149. package/templates/registry/registry.json +2945 -0
  150. package/templates/registry/route-error.json +65 -0
  151. package/templates/registry/select-morph.json +23 -0
  152. package/templates/registry/select.json +23 -0
  153. package/templates/registry/settings-shell.json +47 -0
  154. package/templates/registry/sheet.json +24 -0
  155. package/templates/registry/sidebar.json +28 -0
  156. package/templates/registry/smoke.json +34 -0
  157. package/templates/registry/spinner.json +16 -0
  158. package/templates/registry/stat-card.json +18 -0
  159. package/templates/registry/status-badge.json +18 -0
  160. package/templates/registry/switch.json +20 -0
  161. package/templates/registry/tabs.json +23 -0
  162. package/templates/registry/team-settings.json +97 -0
  163. package/templates/registry/textarea.json +16 -0
  164. package/templates/registry/tooltip.json +22 -0
  165. package/templates/registry/trial-banner.json +43 -0
  166. package/templates/registry/usage.json +84 -0
  167. package/templates/registry/workspace-settings.json +71 -0
  168. package/templates/registry-items.json +1 -1
  169. package/templates/registry-requires.json +26 -2
  170. package/templates/vitest/server-only.ts.tpl +7 -0
  171. package/templates/vitest/vitest.config.ts.tpl +37 -0
@@ -0,0 +1,534 @@
1
+ /**
2
+ * `intelligo sync [items…] [--check] [--force]`
3
+ *
4
+ * Keeps an app's installed registry pages exactly what the framework
5
+ * ships. Installing stays the shadcn CLI's job — every item goes in with
6
+ * `shadcn add <item> --yes --overwrite` — and this command adds what
7
+ * shadcn cannot know:
8
+ *
9
+ * - the version: items come from the registry bundled with this CLI, so
10
+ * they match the `@intelligo-dev/*` packages of the same release;
11
+ * - the order: an item that imports a sibling's files lands after it
12
+ * (requires.json `items`);
13
+ * - the seams: files an item ships once for the deployment to own
14
+ * (requires.json `seams`) are put back after the install, and message
15
+ * files are merged key by key, the app's copy winning;
16
+ * - the record: intelligo.manifest.json keeps each installed file's
17
+ * hash, so `--check` can tell a file edited by hand from one a newer
18
+ * registry replaced. Scaffold files an install replaces (globals.css,
19
+ * the theme provider) leave the `app-scaffold` record for this one,
20
+ * so `upgrade --check` stops calling them customized.
21
+ *
22
+ * `--check` installs nothing and exits 1 when any installed file is
23
+ * missing, edited or behind the registry — the gate CI runs.
24
+ */
25
+
26
+ import { spawn } from "node:child_process";
27
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
28
+ import { createServer, type Server } from "node:http";
29
+ import type { AddressInfo } from "node:net";
30
+ import path from "node:path";
31
+
32
+ import type { RegistryRequires } from "./doctor.js";
33
+ import {
34
+ emptyManifest,
35
+ handOver,
36
+ hashContents,
37
+ readManifest,
38
+ writeManifest,
39
+ type Manifest,
40
+ } from "../manifest.js";
41
+ import {
42
+ asInstalled,
43
+ hasRegistryItem,
44
+ installedPath,
45
+ readRegistryItem,
46
+ registryClosure,
47
+ } from "../registry-bundle.js";
48
+ import { withDependencies } from "../registry-items.js";
49
+ import {
50
+ isMessages,
51
+ localeEntries,
52
+ mergeMessages,
53
+ missingMessageKeys,
54
+ type Json,
55
+ } from "./sync-messages.js";
56
+ import { SCAFFOLD_FEATURE, snapshotScaffold } from "./sync-scaffold.js";
57
+
58
+ export {
59
+ appLocales,
60
+ mergeMessages,
61
+ missingMessageKeys,
62
+ } from "./sync-messages.js";
63
+ export { SCAFFOLD_FEATURE, scaffoldHandover } from "./sync-scaffold.js";
64
+
65
+ /** The design-system base: no marker file, installed before any page. */
66
+ export const BASE_ITEM = "intelligo";
67
+
68
+ export type SyncFileState =
69
+ /** identical to the registry */
70
+ | "current"
71
+ /** untouched since the last sync; the registry has a newer version */
72
+ | "outdated"
73
+ /** changed by hand since it was installed */
74
+ | "edited"
75
+ /** differs from the registry, and no sync ever recorded it */
76
+ | "differs"
77
+ /** the registry ships it and the app does not have it */
78
+ | "missing"
79
+ /** a seam: the app's own from the first install on */
80
+ | "seam"
81
+ /** a message file that lacks keys the registry's has */
82
+ | "messages-behind"
83
+ /** another locale's copy of a shipped namespace lacks keys the app's English has */
84
+ | "locale-behind";
85
+
86
+ export type SyncEntry = {
87
+ item: string;
88
+ path: string;
89
+ state: SyncFileState;
90
+ /** For `messages-behind` / `locale-behind`: the dotted keys the copy lacks. */
91
+ missingKeys?: string[];
92
+ };
93
+
94
+ export type SyncReport = {
95
+ /** The framework version of the bundled registry. */
96
+ version: string;
97
+ /** The items checked, in install order (Intelligo components included). */
98
+ items: string[];
99
+ entries: SyncEntry[];
100
+ };
101
+
102
+ export type SyncSelection =
103
+ { ok: true; items: string[] } | { ok: false; message: string };
104
+
105
+ export type SyncContext = {
106
+ appRoot: string;
107
+ registryDir: string;
108
+ requires: RegistryRequires & { seams?: Record<string, string> };
109
+ frameworkVersion: string;
110
+ };
111
+
112
+ const FAILING: ReadonlySet<SyncFileState> = new Set([
113
+ "outdated",
114
+ "edited",
115
+ "differs",
116
+ "missing",
117
+ "messages-behind",
118
+ "locale-behind",
119
+ ]);
120
+
121
+ /**
122
+ * The items to sync: the ones named, else the ones the manifest
123
+ * recorded. With neither, nothing is guessed — a page at an item's
124
+ * marker path may be the product's own (a custom dashboard), and a sync
125
+ * would replace it — so the message lists what looks installed and asks
126
+ * for names.
127
+ */
128
+ export function selectItems(
129
+ names: readonly string[],
130
+ context: SyncContext
131
+ ): SyncSelection {
132
+ const unknown = names.filter(
133
+ (n) => n === "registry" || !hasRegistryItem(context.registryDir, n)
134
+ );
135
+ if (unknown.length > 0) {
136
+ return {
137
+ ok: false,
138
+ message: `Unknown registry item(s): ${unknown.join(", ")}. Available: ${[
139
+ BASE_ITEM,
140
+ ...Object.keys(context.requires.items),
141
+ ].join(", ")}`,
142
+ };
143
+ }
144
+ if (names.length > 0) return { ok: true, items: [...names] };
145
+
146
+ const recorded = readManifest(context.appRoot)?.registry?.items;
147
+ if (recorded && recorded.length > 0) return { ok: true, items: recorded };
148
+
149
+ const looksInstalled = Object.entries(context.requires.items)
150
+ .filter(([, item]) => existsSync(path.join(context.appRoot, item.marker)))
151
+ .map(([name]) => name);
152
+ return {
153
+ ok: false,
154
+ message: [
155
+ "No synced items are recorded in intelligo.manifest.json yet.",
156
+ "Name the items this app installs from the registry, once:",
157
+ "",
158
+ ` intelligo sync ${[BASE_ITEM, ...looksInstalled].join(" ")}`,
159
+ "",
160
+ "(these are the items whose files are present — leave out any whose",
161
+ "path your product uses for a page of its own)",
162
+ ].join("\n"),
163
+ };
164
+ }
165
+
166
+ /** Blocks in requires.json order, the design-system base first. */
167
+ export function installOrder(
168
+ items: readonly string[],
169
+ requires: RegistryRequires
170
+ ): string[] {
171
+ const blocks = items.filter((n) => n in requires.items);
172
+ const others = items.filter((n) => !(n in requires.items));
173
+ const ordered = withDependencies(blocks, requires);
174
+ return [
175
+ ...others.filter((n) => n === BASE_ITEM),
176
+ ...others.filter((n) => n !== BASE_ITEM),
177
+ ...ordered,
178
+ ];
179
+ }
180
+
181
+ type ShippedFile = { item: string; target: string; content: string };
182
+
183
+ function shippedFiles(
184
+ context: SyncContext,
185
+ items: readonly string[]
186
+ ): {
187
+ closure: string[];
188
+ files: ShippedFile[];
189
+ } {
190
+ const closure = registryClosure(context.registryDir, items);
191
+ const byTarget = new Map<string, ShippedFile>();
192
+ for (const name of closure) {
193
+ const item = readRegistryItem(context.registryDir, name);
194
+ for (const file of item.files ?? []) {
195
+ const target = installedPath(item, file);
196
+ if (!target || file.content === undefined) continue;
197
+ byTarget.set(target, { item: name, target, content: file.content });
198
+ }
199
+ }
200
+ return { closure, files: [...byTarget.values()] };
201
+ }
202
+
203
+ /** Compare the app against the bundled registry; writes nothing. */
204
+ export function syncCheck(
205
+ items: readonly string[],
206
+ context: SyncContext
207
+ ): SyncReport {
208
+ const { closure, files } = shippedFiles(context, items);
209
+ const seams = context.requires.seams ?? {};
210
+ const recorded = readManifest(context.appRoot)?.registry?.files ?? {};
211
+
212
+ const entries: SyncEntry[] = files.map(({ item, target, content }) => {
213
+ const abs = path.join(context.appRoot, target);
214
+ if (!existsSync(abs)) return { item, path: target, state: "missing" };
215
+ if (target in seams) return { item, path: target, state: "seam" };
216
+ const local = readFileSync(abs, "utf8");
217
+
218
+ if (isMessages(target)) {
219
+ const missingKeys = missingMessageKeys(
220
+ JSON.parse(content) as Json,
221
+ JSON.parse(local) as Json
222
+ );
223
+ return missingKeys.length > 0
224
+ ? { item, path: target, state: "messages-behind", missingKeys }
225
+ : { item, path: target, state: "current" };
226
+ }
227
+
228
+ if (asInstalled(local) === asInstalled(content)) {
229
+ return { item, path: target, state: "current" };
230
+ }
231
+ const hash = recorded[target];
232
+ if (hash === undefined) return { item, path: target, state: "differs" };
233
+ return {
234
+ item,
235
+ path: target,
236
+ state: hashContents(asInstalled(local)) === hash ? "outdated" : "edited",
237
+ };
238
+ });
239
+
240
+ entries.push(...localeEntries(context.appRoot, files));
241
+ return { version: context.frameworkVersion, items: closure, entries };
242
+ }
243
+
244
+ export function syncCheckExitCode(report: SyncReport): number {
245
+ return report.entries.some((e) => FAILING.has(e.state)) ? 1 : 0;
246
+ }
247
+
248
+ export function formatSyncReport(report: SyncReport): string {
249
+ const counts = new Map<SyncFileState, number>();
250
+ for (const e of report.entries) {
251
+ counts.set(e.state, (counts.get(e.state) ?? 0) + 1);
252
+ }
253
+ const lines = [
254
+ `Registry ${report.version}: ${report.items.length} item(s), ${report.entries.length} file(s) — ` +
255
+ [...counts].map(([state, n]) => `${n} ${state}`).join(", "),
256
+ ];
257
+ const HINT: Partial<Record<SyncFileState, string>> = {
258
+ outdated: "a newer registry version — `intelligo sync` replaces it",
259
+ edited:
260
+ "edited by hand — move the change into a seam, or ask for one upstream",
261
+ differs: "not what the registry ships, and never synced",
262
+ missing: "not installed",
263
+ "messages-behind": "lacks registry keys — `intelligo sync` adds them",
264
+ "locale-behind":
265
+ "lacks keys the app's English copy has — translate them; sync never writes another locale",
266
+ };
267
+ for (const state of FAILING) {
268
+ const group = report.entries.filter((e) => e.state === state);
269
+ if (group.length === 0) continue;
270
+ lines.push("", `${state} (${HINT[state]}):`);
271
+ for (const e of group) {
272
+ const keys = e.missingKeys
273
+ ? ` — ${e.missingKeys.slice(0, 5).join(", ")}${e.missingKeys.length > 5 ? `, +${e.missingKeys.length - 5}` : ""}`
274
+ : "";
275
+ lines.push(` ${e.path} [${e.item}]${keys}`);
276
+ }
277
+ }
278
+ if (!report.entries.some((e) => FAILING.has(e.state))) {
279
+ lines.push("✓ every installed file is the registry's, seams aside");
280
+ }
281
+ return lines.join("\n");
282
+ }
283
+
284
+ /** The package version the app resolves for `@intelligo-dev/core`, if any. */
285
+ export function installedFrameworkVersion(appRoot: string): string | null {
286
+ const manifest = path.join(
287
+ appRoot,
288
+ "node_modules",
289
+ "@intelligo-dev",
290
+ "core",
291
+ "package.json"
292
+ );
293
+ if (!existsSync(manifest)) return null;
294
+ return (JSON.parse(readFileSync(manifest, "utf8")) as { version: string })
295
+ .version;
296
+ }
297
+
298
+ function serveRegistry(dir: string): Promise<{ server: Server; url: string }> {
299
+ return new Promise((resolve, reject) => {
300
+ const server = createServer((req, res) => {
301
+ const name = decodeURIComponent((req.url ?? "").split("?")[0]!).replace(
302
+ /^\//,
303
+ ""
304
+ );
305
+ const file = path.join(dir, name);
306
+ if (!/^[a-z0-9-]+\.json$/.test(name) || !existsSync(file)) {
307
+ res.writeHead(404);
308
+ res.end();
309
+ return;
310
+ }
311
+ res.writeHead(200, { "content-type": "application/json" });
312
+ res.end(readFileSync(file));
313
+ });
314
+ server.once("error", reject);
315
+ server.listen(0, "127.0.0.1", () => {
316
+ const { port } = server.address() as AddressInfo;
317
+ resolve({ server, url: `http://127.0.0.1:${port}` });
318
+ });
319
+ });
320
+ }
321
+
322
+ function run(
323
+ command: string,
324
+ args: string[],
325
+ cwd: string
326
+ ): Promise<{ code: number; output: string }> {
327
+ return new Promise((resolve) => {
328
+ const child = spawn(command, args, {
329
+ cwd,
330
+ // pnpm, npx and friends are .cmd shims on Windows.
331
+ shell: process.platform === "win32",
332
+ env: process.env,
333
+ });
334
+ let output = "";
335
+ child.stdout.on("data", (d) => (output += String(d)));
336
+ child.stderr.on("data", (d) => (output += String(d)));
337
+ child.on("close", (code) => resolve({ code: code ?? 1, output }));
338
+ child.on("error", (error) =>
339
+ resolve({ code: 1, output: `${output}${error.message}` })
340
+ );
341
+ });
342
+ }
343
+
344
+ /** Put the kept seams back, and merge kept messages over the registry's. */
345
+ function restoreKept(
346
+ appRoot: string,
347
+ kept: ReadonlyMap<string, string>,
348
+ shipped: ReadonlyMap<string, string>
349
+ ): void {
350
+ for (const [target, content] of kept) {
351
+ const abs = path.join(appRoot, target);
352
+ mkdirSync(path.dirname(abs), { recursive: true });
353
+ if (isMessages(target) && shipped.has(target)) {
354
+ const merged = mergeMessages(
355
+ JSON.parse(shipped.get(target)!) as Json,
356
+ JSON.parse(content) as Json
357
+ );
358
+ writeFileSync(abs, `${JSON.stringify(merged, null, 2)}\n`);
359
+ } else {
360
+ writeFileSync(abs, content);
361
+ }
362
+ }
363
+ }
364
+
365
+ /**
366
+ * Write the synced items and each installed file's hash (seams and
367
+ * message files aside) into the manifest.
368
+ */
369
+ function recordSync(
370
+ context: SyncContext,
371
+ names: readonly string[],
372
+ after: SyncReport,
373
+ { manifest }: { manifest: Manifest }
374
+ ): void {
375
+ const files: Record<string, string> = {};
376
+ for (const e of after.entries) {
377
+ if (e.state === "seam" || e.state === "missing" || isMessages(e.path)) {
378
+ continue;
379
+ }
380
+ files[e.path] = hashContents(
381
+ asInstalled(readFileSync(path.join(context.appRoot, e.path), "utf8"))
382
+ );
383
+ }
384
+ const recordedItems = new Set([
385
+ ...(manifest.registry?.items ?? []),
386
+ ...names,
387
+ ]);
388
+ writeManifest(context.appRoot, {
389
+ ...manifest,
390
+ registry: {
391
+ version: context.frameworkVersion,
392
+ items: installOrder([...recordedItems], context.requires),
393
+ files: { ...manifest.registry?.files, ...files },
394
+ },
395
+ });
396
+ }
397
+
398
+ export type SyncApplyOptions = {
399
+ force?: boolean;
400
+ log?: (line: string) => void;
401
+ };
402
+
403
+ /**
404
+ * Install `items` from the bundled registry, keep the seams, merge the
405
+ * messages and record what was written. Refuses to overwrite a file
406
+ * edited by hand unless `force` — the edit belongs in a seam, and the
407
+ * refusal says which files.
408
+ */
409
+ export async function syncApply(
410
+ names: readonly string[],
411
+ context: SyncContext,
412
+ options: SyncApplyOptions = {}
413
+ ): Promise<number> {
414
+ const log = options.log ?? console.log;
415
+ const { appRoot } = context;
416
+ const shadcn = path.join(appRoot, "node_modules", ".bin", "shadcn");
417
+ if (!existsSync(shadcn)) {
418
+ log(
419
+ "shadcn is not installed in this app — add it as a devDependency (the scaffold declares it) and install."
420
+ );
421
+ return 1;
422
+ }
423
+ const componentsPath = path.join(appRoot, "components.json");
424
+ if (!existsSync(componentsPath)) {
425
+ log("No components.json — run from the app's root.");
426
+ return 1;
427
+ }
428
+
429
+ const items = installOrder(names, context.requires);
430
+ const before = syncCheck(items, context);
431
+ const shippedTargets = new Set(
432
+ shippedFiles(context, items).files.map((f) => f.target)
433
+ );
434
+ const edited = before.entries.filter(
435
+ (e) => e.state === "edited" || e.state === "differs"
436
+ );
437
+ if (edited.length > 0 && !options.force) {
438
+ log(
439
+ [
440
+ `${edited.length} installed file(s) differ from the registry and would be overwritten:`,
441
+ ...edited.map((e) => ` ${e.path} [${e.item}]`),
442
+ "",
443
+ "Move what the change does into a seam (lib/*-config, messages/), then re-run",
444
+ "with --force to replace these files with the registry's.",
445
+ ].join("\n")
446
+ );
447
+ return 1;
448
+ }
449
+
450
+ // What the install must not lose: the app's seams and message files.
451
+ const seams = context.requires.seams ?? {};
452
+ const kept = new Map<string, string>();
453
+ for (const e of before.entries) {
454
+ if (e.state === "missing" || !shippedTargets.has(e.path)) continue;
455
+ if (e.path in seams || isMessages(e.path)) {
456
+ kept.set(e.path, readFileSync(path.join(appRoot, e.path), "utf8"));
457
+ }
458
+ }
459
+
460
+ const componentsBefore = JSON.parse(readFileSync(componentsPath, "utf8")) as {
461
+ registries?: Record<string, string>;
462
+ tailwind?: { css?: string };
463
+ };
464
+ // The scaffold's files as they are now, to see which the install replaces.
465
+ const scaffold = snapshotScaffold(appRoot);
466
+ const originalRegistry = componentsBefore.registries?.["@intelligo"];
467
+ const { server, url } = await serveRegistry(context.registryDir);
468
+
469
+ const setRegistry = (value: string | undefined) => {
470
+ // Read fresh: the base item rewrites components.json's style fields,
471
+ // and those changes are the point — only the registry URL is ours.
472
+ const current = JSON.parse(readFileSync(componentsPath, "utf8")) as {
473
+ registries?: Record<string, string>;
474
+ };
475
+ const registries = { ...current.registries };
476
+ if (value === undefined) delete registries["@intelligo"];
477
+ else registries["@intelligo"] = value;
478
+ const next: typeof current = { ...current, registries };
479
+ if (Object.keys(registries).length === 0) delete next.registries;
480
+ writeFileSync(componentsPath, `${JSON.stringify(next, null, 2)}\n`);
481
+ };
482
+
483
+ let failed: { item: string; output: string } | null = null;
484
+ try {
485
+ setRegistry(`${url}/{name}.json`);
486
+ for (const item of items) {
487
+ log(`› shadcn add ${item}`);
488
+ const result = await run(
489
+ shadcn,
490
+ ["add", `${url}/${item}.json`, "--yes", "--overwrite"],
491
+ appRoot
492
+ );
493
+ if (result.code !== 0) {
494
+ failed = { item, output: result.output };
495
+ break;
496
+ }
497
+ }
498
+ } finally {
499
+ setRegistry(originalRegistry);
500
+ server.close();
501
+ }
502
+
503
+ // Seams back as they were; messages merged over the registry's.
504
+ const shipped = new Map(
505
+ shippedFiles(context, items).files.map((f) => [f.target, f.content])
506
+ );
507
+ restoreKept(appRoot, kept, shipped);
508
+
509
+ if (failed) {
510
+ log(
511
+ `shadcn add ${failed.item} failed:\n${failed.output.trim().split("\n").slice(-20).join("\n")}`
512
+ );
513
+ return 1;
514
+ }
515
+
516
+ // Record what is on disk now, so the next check can tell an edit.
517
+ const after = syncCheck(items, context);
518
+ recordSync(context, names, after, {
519
+ manifest: handOver(
520
+ readManifest(appRoot) ?? emptyManifest(context.frameworkVersion),
521
+ SCAFFOLD_FEATURE,
522
+ scaffold.handedOver({
523
+ shipped: new Set(shipped.keys()),
524
+ seams,
525
+ css: items.includes(BASE_ITEM)
526
+ ? (componentsBefore.tailwind?.css ?? null)
527
+ : null,
528
+ })
529
+ ),
530
+ });
531
+
532
+ log(formatSyncReport(after));
533
+ return syncCheckExitCode(after);
534
+ }
@@ -61,7 +61,10 @@ export function upgradeCheck(options: UpgradeCheckOptions): UpgradeReport {
61
61
  (spec?.files ?? []).map((f) => [f.target, f.template])
62
62
  );
63
63
 
64
- for (const target of newFiles(spec, entry.files)) {
64
+ for (const target of newFiles(spec, [
65
+ ...entry.files,
66
+ ...(entry.handedOver ?? []).map((handed) => ({ path: handed })),
67
+ ])) {
65
68
  report.items.push({ feature, path: target, state: "new" });
66
69
  }
67
70
 
package/src/manifest.ts CHANGED
@@ -39,12 +39,35 @@ export type FeatureEntry = {
39
39
  * would make every substituted file look permanently outdated.
40
40
  */
41
41
  variables?: Record<string, string>;
42
+ /**
43
+ * Files this feature wrote that a registry install has since replaced
44
+ * (the scaffold's globals.css once the design-system base lands):
45
+ * `intelligo sync` owns them now, so `upgrade --check` neither
46
+ * compares them nor offers them as new, and `add` does not write them
47
+ * back.
48
+ */
49
+ handedOver?: string[];
50
+ };
51
+
52
+ /**
53
+ * Registry items `intelligo sync` installed: which items the app keeps
54
+ * in sync, the framework version they came from, and — per installed
55
+ * file that is not a seam or a message file — the hash of the file as
56
+ * written (after shadcn's normalisation, see `asInstalled`). A file
57
+ * whose hash still matches was not edited since, so a newer registry
58
+ * may replace it; one that differs was edited by hand.
59
+ */
60
+ export type RegistryEntry = {
61
+ version: string;
62
+ items: string[];
63
+ files: Record<string, string>;
42
64
  };
43
65
 
44
66
  export type Manifest = {
45
67
  schemaVersion: 1;
46
68
  frameworkVersion: string;
47
69
  features: Record<string, FeatureEntry>;
70
+ registry?: RegistryEntry;
48
71
  };
49
72
 
50
73
  export function hashContents(contents: string): string {
@@ -81,13 +104,44 @@ export function recordFeature(
81
104
  files: GeneratedFile[],
82
105
  variables?: Record<string, string>
83
106
  ): Manifest {
107
+ const handedOver = manifest.features[feature]?.handedOver;
108
+ return {
109
+ ...manifest,
110
+ features: {
111
+ ...manifest.features,
112
+ [feature]: {
113
+ templateVersion,
114
+ files,
115
+ ...(variables ? { variables } : {}),
116
+ ...(handedOver?.length ? { handedOver } : {}),
117
+ },
118
+ },
119
+ };
120
+ }
121
+
122
+ /**
123
+ * Move `paths` out of a feature's generated files into its `handedOver`
124
+ * list. A no-op for a feature the manifest does not record.
125
+ */
126
+ export function handOver(
127
+ manifest: Manifest,
128
+ feature: string,
129
+ paths: readonly string[]
130
+ ): Manifest {
131
+ const entry = manifest.features[feature];
132
+ if (!entry || paths.length === 0) return manifest;
133
+ const moved = new Set(paths);
84
134
  return {
85
135
  ...manifest,
86
136
  features: {
87
137
  ...manifest.features,
88
- [feature]: variables
89
- ? { templateVersion, files, variables }
90
- : { templateVersion, files },
138
+ [feature]: {
139
+ ...entry,
140
+ files: entry.files.filter((f) => !moved.has(f.path)),
141
+ handedOver: [
142
+ ...new Set([...(entry.handedOver ?? []), ...paths]),
143
+ ].sort(),
144
+ },
91
145
  },
92
146
  };
93
147
  }