@tu-design/cli 0.5.0 → 0.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 (3) hide show
  1. package/README.md +5 -0
  2. package/dist/index.mjs +137 -169
  3. package/package.json +3 -2
package/README.md CHANGED
@@ -57,6 +57,11 @@ CLI 从 registry 服务拉取每个组件的 **registry JSON**(遵循 [shadcn
57
57
 
58
58
  默认 registry 数据源是文档站 Pages 上发布的 `https://gopowerteam.github.io/tu-design/r/vue`,由 [`@tu-design/vue` 的 `generate:registry`](../vue) 生成并随 Pages 部署更新,可用 `--registry` 指向自建服务。
59
59
 
60
+ ## 开发
61
+
62
+ - 构建:`vp run build`(`vp pack` + `fix-bin`);测试:`vp test --run`;类型:`vp run typecheck`
63
+ - `src/lib/generated-sources.ts`(init 注入的 tokens.css / cn 工具模板常量)由 `scripts/generate-sources.ts` 从 `@tu-design/vue` 源码生成,勿手改:改动 `packages/vue/src/styles/tokens.css` 或 `packages/vue/src/lib/utils.ts` 后运行 `pnpm --filter @tu-design/cli generate:sources` 再生成,`init-core.test.ts` 防漂移守卫会拦截漏跑。
64
+
60
65
  ## 相关
61
66
 
62
67
  - 组件库:[@tu-design/vue](../vue)
package/dist/index.mjs CHANGED
@@ -126,6 +126,10 @@ const AGENTS_MD_SOURCE = `# tu-design AI 协作规则
126
126
  - 全量文档:https://gopowerteam.github.io/tu-design/llms-full.txt(可整页抓取给 AI)
127
127
  `;
128
128
  //#endregion
129
+ //#region src/lib/generated-sources.ts
130
+ const TOKENS_CSS_SOURCE = "/* tu-design 官方 tokens —— Tailwind CSS v4 主题(shadcn v4 集合扩展)。\n *\n * 主题桥接:语义色优先消费应用级 --app-* CSS 变量(应用主题系统写入),\n * 未定义时回落官方 oklch 值 —— 无主题系统的项目观感与上游一致,零破坏。\n * 契约(--app-* 全集见各项目主题包,如 @repo/themes):\n * - --app-bg-page/surface/raised/hover → --background/--card/--popover/--muted/--accent\n * - --app-text-1/2/3 → --foreground 及各 *-foreground/--muted-foreground\n * (text-3 上游零消费,供应用侧直接使用)\n * - --app-accent/-contrast → --primary(+foreground)/--ring\n * - --app-border → --border/--input(dark 模式建议低不透明度值,如 #FFFFFF1A,对齐上游风格)\n * - --secondary/-foreground 有意不桥接(上游定位即中性灰)\n * - --app-destructive/success/warning/info → 状态色三件套:实色、-soft 浅底(未定义回落\n * color-mix 派生)、-foreground 前景(深色实色场景如 dark destructive 白字对比 borderline,\n * 建议应用侧显式定义覆盖)\n * - .dark 块同样桥接,兜底为官方暗色 oklch;--*-soft 在 .dark 内重复声明:\n * 自定义属性按声明处求值后继承,不在 .dark 重声明将无法响应暗色的实色变化。\n */\n@import \"tw-animate-css\";\n\n@custom-variant dark (&:where(.dark, .dark *));\n\n:root {\n --background: var(--app-bg-page, oklch(1 0 0));\n --foreground: var(--app-text-1, oklch(0.145 0 0));\n --card: var(--app-bg-raised, oklch(1 0 0));\n --card-foreground: var(--app-text-1, oklch(0.145 0 0));\n --popover: var(--app-bg-raised, oklch(1 0 0));\n --popover-foreground: var(--app-text-1, oklch(0.145 0 0));\n --primary: var(--app-accent, oklch(0.205 0 0));\n --primary-foreground: var(--app-accent-contrast, oklch(0.985 0 0));\n --secondary: oklch(0.97 0 0);\n --secondary-foreground: oklch(0.205 0 0);\n --muted: var(--app-bg-surface, oklch(0.97 0 0));\n --muted-foreground: var(--app-text-2, oklch(0.556 0 0));\n --accent: var(--app-bg-hover, oklch(0.97 0 0));\n --accent-foreground: var(--app-text-1, oklch(0.205 0 0));\n --destructive: var(--app-destructive, oklch(0.577 0.245 27.325));\n --destructive-soft: var(\n --app-destructive-soft,\n color-mix(in oklab, var(--destructive) 10%, transparent)\n );\n --destructive-foreground: var(--app-destructive-foreground, oklch(0.985 0 0));\n --success: var(--app-success, oklch(0.596 0.145 163.225));\n --success-soft: var(--app-success-soft, color-mix(in oklab, var(--success) 10%, transparent));\n --success-foreground: var(--app-success-foreground, oklch(0.985 0 0));\n --warning: var(--app-warning, oklch(0.666 0.179 58.318));\n --warning-soft: var(--app-warning-soft, color-mix(in oklab, var(--warning) 12%, transparent));\n --warning-foreground: var(--app-warning-foreground, oklch(0.985 0 0));\n --info: var(--app-info, oklch(0.546 0.245 262.881));\n --info-soft: var(--app-info-soft, color-mix(in oklab, var(--info) 12%, transparent));\n --info-foreground: var(--app-info-foreground, oklch(0.985 0 0));\n --border: var(--app-border, oklch(0.922 0 0));\n --input: var(--app-border, oklch(0.922 0 0));\n --ring: var(--app-accent, oklch(0.708 0 0));\n --radius: 0.625rem;\n /* 层叠序:表体 sticky < 表头 sticky < 浮层/弹窗(同级浮层按 DOM 挂载次序叠放) */\n --z-sticky: 10;\n --z-sticky-head: 20;\n --z-overlay: 50;\n}\n\n.dark {\n --background: var(--app-bg-page, oklch(0.145 0 0));\n --foreground: var(--app-text-1, oklch(0.985 0 0));\n --card: var(--app-bg-raised, oklch(0.205 0 0));\n --card-foreground: var(--app-text-1, oklch(0.985 0 0));\n --popover: var(--app-bg-raised, oklch(0.269 0 0));\n --popover-foreground: var(--app-text-1, oklch(0.985 0 0));\n --primary: var(--app-accent, oklch(0.922 0 0));\n --primary-foreground: var(--app-accent-contrast, oklch(0.205 0 0));\n --secondary: oklch(0.269 0 0);\n --secondary-foreground: oklch(0.985 0 0);\n --muted: var(--app-bg-surface, oklch(0.269 0 0));\n --muted-foreground: var(--app-text-2, oklch(0.708 0 0));\n --accent: var(--app-bg-hover, oklch(0.371 0 0));\n --accent-foreground: var(--app-text-1, oklch(0.985 0 0));\n --destructive: var(--app-destructive, oklch(0.704 0.191 22.216));\n --destructive-soft: var(\n --app-destructive-soft,\n color-mix(in oklab, var(--destructive) 10%, transparent)\n );\n --destructive-foreground: var(--app-destructive-foreground, oklch(0.985 0 0));\n --success: var(--app-success, oklch(0.696 0.17 162.48));\n --success-soft: var(--app-success-soft, color-mix(in oklab, var(--success) 10%, transparent));\n --success-foreground: var(--app-success-foreground, oklch(0.985 0 0));\n --warning: var(--app-warning, oklch(0.769 0.188 70.08));\n --warning-soft: var(--app-warning-soft, color-mix(in oklab, var(--warning) 12%, transparent));\n --warning-foreground: var(--app-warning-foreground, oklch(0.205 0 0));\n --info: var(--app-info, oklch(0.623 0.214 259.815));\n --info-soft: var(--app-info-soft, color-mix(in oklab, var(--info) 12%, transparent));\n --info-foreground: var(--app-info-foreground, oklch(0.985 0 0));\n --border: var(--app-border, oklch(1 0 0 / 10%));\n --input: var(--app-border, oklch(1 0 0 / 15%));\n --ring: var(--app-accent, oklch(0.556 0 0));\n}\n\n@theme inline {\n --radius-sm: calc(var(--radius) - 4px);\n --radius-md: calc(var(--radius) - 2px);\n --radius-lg: var(--radius);\n --radius-xl: calc(var(--radius) + 4px);\n --radius-2xl: calc(var(--radius) + 6px);\n --color-background: var(--background);\n --color-foreground: var(--foreground);\n --color-card: var(--card);\n --color-card-foreground: var(--card-foreground);\n --color-popover: var(--popover);\n --color-popover-foreground: var(--popover-foreground);\n --color-primary: var(--primary);\n --color-primary-foreground: var(--primary-foreground);\n --color-secondary: var(--secondary);\n --color-secondary-foreground: var(--secondary-foreground);\n --color-muted: var(--muted);\n --color-muted-foreground: var(--muted-foreground);\n --color-accent: var(--accent);\n --color-accent-foreground: var(--accent-foreground);\n --color-destructive: var(--destructive);\n --color-destructive-soft: var(--destructive-soft);\n --color-destructive-foreground: var(--destructive-foreground);\n --color-success: var(--success);\n --color-success-foreground: var(--success-foreground);\n --color-success-soft: var(--success-soft);\n --color-warning: var(--warning);\n --color-warning-foreground: var(--warning-foreground);\n --color-warning-soft: var(--warning-soft);\n --color-info: var(--info);\n --color-info-foreground: var(--info-foreground);\n --color-info-soft: var(--info-soft);\n --color-border: var(--border);\n --color-input: var(--input);\n --color-ring: var(--ring);\n}\n\n/* 全局边框色兜底,对齐 shadcn v4 官方 tokens(上游另含 outline-ring/50,此处仅移植 border):\n 裸 border 类不再回退 currentColor,而是使用 --border token。 */\n@layer base {\n * {\n border-color: var(--color-border);\n }\n}\n";
131
+ const CN_UTIL_SOURCE = "import { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nexport function cn(...inputs: ClassValue[]) {\n return twMerge(clsx(inputs));\n}\n";
132
+ //#endregion
129
133
  //#region src/lib/alias.ts
130
134
  /**
131
135
  * 将 paths 合并进 tsconfig 文本。优先 JSON round-trip;
@@ -213,159 +217,6 @@ const RUNTIME_DEPS = [
213
217
  "class-variance-authority",
214
218
  "tw-animate-css"
215
219
  ];
216
- /**
217
- * 与 @tu-design/vue/src/styles/tokens.css 保持一致(init-core.test.ts 有防漂移守卫)。
218
- * 来源:packages/vue/src/styles/tokens.css
219
- */
220
- const TOKENS_CSS_SOURCE = `/* tu-design 官方 tokens —— Tailwind CSS v4 主题(shadcn v4 集合扩展)。
221
- *
222
- * 主题桥接:语义色优先消费应用级 --app-* CSS 变量(应用主题系统写入),
223
- * 未定义时回落官方 oklch 值 —— 无主题系统的项目观感与上游一致,零破坏。
224
- * 契约(--app-* 全集见各项目主题包,如 @repo/themes):
225
- * - --app-bg-page/surface/raised/hover → --background/--card/--popover/--muted/--accent
226
- * - --app-text-1/2/3 → --foreground 及各 *-foreground/--muted-foreground
227
- * (text-3 上游零消费,供应用侧直接使用)
228
- * - --app-accent/-contrast → --primary(+foreground)/--ring
229
- * - --app-border → --border/--input(dark 模式建议低不透明度值,如 #FFFFFF1A,对齐上游风格)
230
- * - --secondary/-foreground 有意不桥接(上游定位即中性灰)
231
- * - --app-destructive/success/warning/info → 状态色三件套:实色、-soft 浅底(未定义回落
232
- * color-mix 派生)、-foreground 前景(深色实色场景如 dark destructive 白字对比 borderline,
233
- * 建议应用侧显式定义覆盖)
234
- * - .dark 块同样桥接,兜底为官方暗色 oklch;--*-soft 在 .dark 内重复声明:
235
- * 自定义属性按声明处求值后继承,不在 .dark 重声明将无法响应暗色的实色变化。
236
- */
237
- @import "tw-animate-css";
238
-
239
- @custom-variant dark (&:where(.dark, .dark *));
240
-
241
- :root {
242
- --background: var(--app-bg-page, oklch(1 0 0));
243
- --foreground: var(--app-text-1, oklch(0.145 0 0));
244
- --card: var(--app-bg-raised, oklch(1 0 0));
245
- --card-foreground: var(--app-text-1, oklch(0.145 0 0));
246
- --popover: var(--app-bg-raised, oklch(1 0 0));
247
- --popover-foreground: var(--app-text-1, oklch(0.145 0 0));
248
- --primary: var(--app-accent, oklch(0.205 0 0));
249
- --primary-foreground: var(--app-accent-contrast, oklch(0.985 0 0));
250
- --secondary: oklch(0.97 0 0);
251
- --secondary-foreground: oklch(0.205 0 0);
252
- --muted: var(--app-bg-surface, oklch(0.97 0 0));
253
- --muted-foreground: var(--app-text-2, oklch(0.556 0 0));
254
- --accent: var(--app-bg-hover, oklch(0.97 0 0));
255
- --accent-foreground: var(--app-text-1, oklch(0.205 0 0));
256
- --destructive: var(--app-destructive, oklch(0.577 0.245 27.325));
257
- --destructive-soft: var(
258
- --app-destructive-soft,
259
- color-mix(in oklab, var(--destructive) 10%, transparent)
260
- );
261
- --destructive-foreground: var(--app-destructive-foreground, oklch(0.985 0 0));
262
- --success: var(--app-success, oklch(0.596 0.145 163.225));
263
- --success-soft: var(--app-success-soft, color-mix(in oklab, var(--success) 10%, transparent));
264
- --success-foreground: var(--app-success-foreground, oklch(0.985 0 0));
265
- --warning: var(--app-warning, oklch(0.666 0.179 58.318));
266
- --warning-soft: var(--app-warning-soft, color-mix(in oklab, var(--warning) 12%, transparent));
267
- --warning-foreground: var(--app-warning-foreground, oklch(0.985 0 0));
268
- --info: var(--app-info, oklch(0.546 0.245 262.881));
269
- --info-soft: var(--app-info-soft, color-mix(in oklab, var(--info) 12%, transparent));
270
- --info-foreground: var(--app-info-foreground, oklch(0.985 0 0));
271
- --border: var(--app-border, oklch(0.922 0 0));
272
- --input: var(--app-border, oklch(0.922 0 0));
273
- --ring: var(--app-accent, oklch(0.708 0 0));
274
- --radius: 0.625rem;
275
- /* 层叠序:表体 sticky < 表头 sticky < 浮层/弹窗(同级浮层按 DOM 挂载次序叠放) */
276
- --z-sticky: 10;
277
- --z-sticky-head: 20;
278
- --z-overlay: 50;
279
- }
280
-
281
- .dark {
282
- --background: var(--app-bg-page, oklch(0.145 0 0));
283
- --foreground: var(--app-text-1, oklch(0.985 0 0));
284
- --card: var(--app-bg-raised, oklch(0.205 0 0));
285
- --card-foreground: var(--app-text-1, oklch(0.985 0 0));
286
- --popover: var(--app-bg-raised, oklch(0.269 0 0));
287
- --popover-foreground: var(--app-text-1, oklch(0.985 0 0));
288
- --primary: var(--app-accent, oklch(0.922 0 0));
289
- --primary-foreground: var(--app-accent-contrast, oklch(0.205 0 0));
290
- --secondary: oklch(0.269 0 0);
291
- --secondary-foreground: oklch(0.985 0 0);
292
- --muted: var(--app-bg-surface, oklch(0.269 0 0));
293
- --muted-foreground: var(--app-text-2, oklch(0.708 0 0));
294
- --accent: var(--app-bg-hover, oklch(0.371 0 0));
295
- --accent-foreground: var(--app-text-1, oklch(0.985 0 0));
296
- --destructive: var(--app-destructive, oklch(0.704 0.191 22.216));
297
- --destructive-soft: var(
298
- --app-destructive-soft,
299
- color-mix(in oklab, var(--destructive) 10%, transparent)
300
- );
301
- --destructive-foreground: var(--app-destructive-foreground, oklch(0.985 0 0));
302
- --success: var(--app-success, oklch(0.696 0.17 162.48));
303
- --success-soft: var(--app-success-soft, color-mix(in oklab, var(--success) 10%, transparent));
304
- --success-foreground: var(--app-success-foreground, oklch(0.985 0 0));
305
- --warning: var(--app-warning, oklch(0.769 0.188 70.08));
306
- --warning-soft: var(--app-warning-soft, color-mix(in oklab, var(--warning) 12%, transparent));
307
- --warning-foreground: var(--app-warning-foreground, oklch(0.205 0 0));
308
- --info: var(--app-info, oklch(0.623 0.214 259.815));
309
- --info-soft: var(--app-info-soft, color-mix(in oklab, var(--info) 12%, transparent));
310
- --info-foreground: var(--app-info-foreground, oklch(0.985 0 0));
311
- --border: var(--app-border, oklch(1 0 0 / 10%));
312
- --input: var(--app-border, oklch(1 0 0 / 15%));
313
- --ring: var(--app-accent, oklch(0.556 0 0));
314
- }
315
-
316
- @theme inline {
317
- --radius-sm: calc(var(--radius) - 4px);
318
- --radius-md: calc(var(--radius) - 2px);
319
- --radius-lg: var(--radius);
320
- --radius-xl: calc(var(--radius) + 4px);
321
- --radius-2xl: calc(var(--radius) + 6px);
322
- --color-background: var(--background);
323
- --color-foreground: var(--foreground);
324
- --color-card: var(--card);
325
- --color-card-foreground: var(--card-foreground);
326
- --color-popover: var(--popover);
327
- --color-popover-foreground: var(--popover-foreground);
328
- --color-primary: var(--primary);
329
- --color-primary-foreground: var(--primary-foreground);
330
- --color-secondary: var(--secondary);
331
- --color-secondary-foreground: var(--secondary-foreground);
332
- --color-muted: var(--muted);
333
- --color-muted-foreground: var(--muted-foreground);
334
- --color-accent: var(--accent);
335
- --color-accent-foreground: var(--accent-foreground);
336
- --color-destructive: var(--destructive);
337
- --color-destructive-soft: var(--destructive-soft);
338
- --color-destructive-foreground: var(--destructive-foreground);
339
- --color-success: var(--success);
340
- --color-success-foreground: var(--success-foreground);
341
- --color-success-soft: var(--success-soft);
342
- --color-warning: var(--warning);
343
- --color-warning-foreground: var(--warning-foreground);
344
- --color-warning-soft: var(--warning-soft);
345
- --color-info: var(--info);
346
- --color-info-foreground: var(--info-foreground);
347
- --color-info-soft: var(--info-soft);
348
- --color-border: var(--border);
349
- --color-input: var(--input);
350
- --color-ring: var(--ring);
351
- }
352
-
353
- /* 全局边框色兜底,对齐 shadcn v4 官方 tokens(上游另含 outline-ring/50,此处仅移植 border):
354
- 裸 border 类不再回退 currentColor,而是使用 --border token。 */
355
- @layer base {
356
- * {
357
- border-color: var(--color-border);
358
- }
359
- }
360
- `;
361
- /** cn 工具模板,等价 @tu-design/vue/src/lib/utils.ts。 */
362
- const CN_UTIL_SOURCE = `import { clsx, type ClassValue } from "clsx";
363
- import { twMerge } from "tailwind-merge";
364
-
365
- export function cn(...inputs: ClassValue[]) {
366
- return twMerge(clsx(inputs));
367
- }
368
- `;
369
220
  function detectTailwindV4(pkg, globalCss) {
370
221
  const tw = {
371
222
  ...pkg.dependencies,
@@ -556,6 +407,7 @@ function validateLockfile(raw) {
556
407
  const entry = pkg;
557
408
  if (entry.version !== null && typeof entry.version !== "string") throw new Error(`${LOCKFILE_NAME} 的 packages.${name}.version 必须是字符串或 null,得到 ${typeof entry.version}`);
558
409
  if (typeof entry.files !== "object" || entry.files === null || Array.isArray(entry.files)) throw new Error(`${LOCKFILE_NAME} 的 packages.${name}.files 必须是对象`);
410
+ if (entry.relativeImports !== void 0 && typeof entry.relativeImports !== "boolean") throw new Error(`${LOCKFILE_NAME} 的 packages.${name}.relativeImports 必须是 boolean 或缺省,得到 ${typeof entry.relativeImports}`);
559
411
  const files = {};
560
412
  for (const [path, hash] of Object.entries(entry.files)) {
561
413
  if (typeof hash !== "string") throw new Error(`${LOCKFILE_NAME} 的 packages.${name}.files.${path} 必须是字符串,得到 ${typeof hash}`);
@@ -563,7 +415,8 @@ function validateLockfile(raw) {
563
415
  }
564
416
  out[name] = {
565
417
  version: entry.version,
566
- files
418
+ files,
419
+ ...entry.relativeImports === true ? { relativeImports: true } : {}
567
420
  };
568
421
  }
569
422
  return {
@@ -584,6 +437,70 @@ function writeLockfile(io, root, lockfile) {
584
437
  io.writeFile(`${root}/${LOCKFILE_NAME}`, `${JSON.stringify(lockfile, null, 2)}\n`);
585
438
  }
586
439
  //#endregion
440
+ //#region src/lib/rewrite.ts
441
+ /**
442
+ * 空白归一化:移除全部空白字符。
443
+ *
444
+ * 动机:registry 源码写入用户项目后,用户的 fmt(prettier 等)会重排空白
445
+ * (缩进、折行、行尾),逐字节 hash 会把「格式重排」误判为「内容变更」,
446
+ * 造成 up-to-date 失效与三方比对假 conflict。归一化后 hash 只对语义内容敏感。
447
+ * @param content 任意文本
448
+ * @returns 移除全部空白后的文本
449
+ */
450
+ function normalizeContent(content) {
451
+ return content.replace(/\s+/g, "");
452
+ }
453
+ /**
454
+ * 空白归一化 hash:比对基准统一走它(writeFiles upToDate / dry-run 判定 /
455
+ * lockfile 记录 / diff 三方比对),任何一处用精确 hash 都会 reintroduce 假 dirty。
456
+ * @param content 任意文本
457
+ * @returns 归一化后的 sha256(hex)
458
+ */
459
+ function hashNormalizedContent(content) {
460
+ return hashContent(normalizeContent(content));
461
+ }
462
+ /**
463
+ * registry 产出源码的 alias 公共根:defaultConfig 四个别名(components/utils/ui/lib)
464
+ * 全部以 `@/` 开头,registry 内容里的 import 规格统一是 `@/<aliasRoot 内相对路径>`。
465
+ * 改写按公共根整体处理(而非逐个别名键前缀匹配),天然规避
466
+ * `@/components/ui/...` 同时命中 components 与 ui 两个前缀的重叠判定问题。
467
+ */
468
+ const ALIAS_SPEC_ROOT = "@/";
469
+ /** 参与 import 改写的文件扩展名;css/json 等非源码文件原样返回。 */
470
+ const REWRITE_EXTENSIONS = /\.(?:ts|tsx|mts|cts|vue)$/;
471
+ /**
472
+ * 把源码中的 alias 导入改写为相对导入(--relative-imports)。
473
+ *
474
+ * 规则:`from "@/x/y"` 与动态 `import("@/x/y")` 中的规格按
475
+ * 「文件所在目录 → aliasRoot 的相对深度」改写为 `../` 前缀路径:
476
+ * - `/proj/src/components/ui/button/button.vue` 引 `@/lib/utils`
477
+ * → aliasRoot=/proj/src,深度 3 → `../../../lib/utils`
478
+ * - 规格恰为 `@/`(无余下路径,指向 aliasRoot 自身)语义不明,保守跳过不改写。
479
+ * 仅改写 import 语句内的字符串字面量,字符串/注释中的 `@/` 字面量不受影响。
480
+ * @param content registry 原文内容
481
+ * @param filePath 落盘绝对路径(POSIX),用于推导相对深度
482
+ * @param aliasRoot 项目源码根(`@/` 的实体,如 `<root>/src`)
483
+ * @returns 改写后内容;非源码扩展名或无 alias 引用时原样返回
484
+ */
485
+ function rewriteContent(content, filePath, aliasRoot) {
486
+ if (!REWRITE_EXTENSIONS.test(filePath)) return content;
487
+ const root = aliasRoot.replace(/\\/g, "/").replace(/\/+$/, "");
488
+ const file = filePath.replace(/\\/g, "/");
489
+ if (!file.startsWith(`${root}/`)) return content;
490
+ const depth = file.slice(root.length + 1, file.lastIndexOf("/")).split("/").length;
491
+ const up = "../".repeat(depth);
492
+ const rewriteSpec = (spec) => {
493
+ if (!spec.startsWith(ALIAS_SPEC_ROOT)) return null;
494
+ const rest = spec.slice(2);
495
+ if (rest === "") return null;
496
+ return `${up}${rest}`;
497
+ };
498
+ return content.replace(/(from\s*|import\s*\(\s*)(["'])(@\/[^"']*)\2/g, (whole, lead, quote, spec) => {
499
+ const rewritten = rewriteSpec(spec);
500
+ return rewritten === null ? whole : `${lead}${quote}${rewritten}${quote}`;
501
+ });
502
+ }
503
+ //#endregion
587
504
  //#region src/lib/write.ts
588
505
  function parentDir(dir) {
589
506
  if (dir === "/") return null;
@@ -643,8 +560,9 @@ function planWrites(items, io, aliasRoot, config) {
643
560
  return plans;
644
561
  }
645
562
  /**
646
- * 覆盖安全语义:内容比对(sha256)优先于存在性判断——
647
- * 内容相同的重复安装不算冲突,静默跳过;只有「存在且内容不同」才进入覆盖询问 / conflict 分支。
563
+ * 覆盖安全语义:归一化内容比对(sha256,空白不敏感)优先于存在性判断——
564
+ * 内容相同的重复安装不算冲突,静默跳过;只有「存在且归一化内容不同」才进入
565
+ * 覆盖询问 / conflict 分支。空白不敏感:用户 fmt 重排不触发覆盖询问。
648
566
  */
649
567
  async function writeFiles(plans, io, opts) {
650
568
  const result = {
@@ -656,7 +574,7 @@ async function writeFiles(plans, io, opts) {
656
574
  };
657
575
  for (const plan of plans) {
658
576
  if (io.exists(plan.path)) {
659
- if (hashContent(io.readFile(plan.path)) === hashContent(plan.content)) {
577
+ if (hashNormalizedContent(io.readFile(plan.path)) === hashNormalizedContent(plan.content)) {
660
578
  result.upToDate.push(plan.path);
661
579
  continue;
662
580
  }
@@ -723,17 +641,53 @@ function collectDeps(items) {
723
641
  function aliasRootOf(root) {
724
642
  return `${root}/src`;
725
643
  }
644
+ /**
645
+ * add / diff 共用:解析参与组件名列表。
646
+ * --all 从 lockfile 展开全部记录名;与显式组件名互斥,lockfile 为空时报错提示。
647
+ * @param io IO 通道(读 lockfile)
648
+ * @param root 项目根
649
+ * @param names 显式组件名(可为空数组)
650
+ * @param opts.all 是否全量展开
651
+ * @returns 参与组件名
652
+ */
653
+ function resolveNames(io, root, names, all) {
654
+ if (!all) {
655
+ if (names.length === 0) throw new Error("未指定组件名(或使用 --all 全量升级 lockfile 记录的全部组件)");
656
+ return names;
657
+ }
658
+ if (names.length > 0) throw new Error("--all 与显式组件名互斥,二者只取其一");
659
+ const lock = readLockfile(io, root);
660
+ const allNames = Object.keys(lock.packages);
661
+ if (allNames.length === 0) throw new Error("components.lock.json 没有记录(先 add 具体组件后才能 --all 全量升级)");
662
+ return allNames;
663
+ }
664
+ /**
665
+ * 运行 add 命令:拉取 registry item(拓扑序)、写入组件源码、更新 lockfile,
666
+ * 并按需安装依赖。--all 全量升级 lock 记录;--no-install 跳过依赖安装;
667
+ * --relative-imports 在写入前把 `@/` alias 导入改写为相对导入(lockfile 记录改写后
668
+ * 内容的归一化 hash 与 relativeImports 标记,供 diff 按同款基准三方比对)。
669
+ * @param io IO 通道(含 registry fetch 与子进程 exec)
670
+ * @param cwd 运行目录(向上查找 components.json 定位项目根)
671
+ * @param names 显式组件名;配合 opts.all 留空
672
+ * @param opts 覆盖安装/registry 通道/dry-run/all/no-install/relative-imports 选项
673
+ * @returns 写入/覆盖/冲突/最新跳过/依赖清单摘要
674
+ */
726
675
  async function runAdd(io, cwd, names, opts) {
727
676
  const root = findProjectRoot(io, cwd);
728
677
  const config = loadConfig(io, root);
729
678
  const registryUrl = resolveRegistryUrl(config, opts.registry);
730
679
  const aliasRoot = aliasRootOf(root);
680
+ const finalNames = resolveNames(io, root, names, opts.all);
731
681
  const npm = opts.npm ?? {
732
682
  hasItem: () => false,
733
683
  readItem: () => void 0
734
684
  };
735
- const items = await resolveTopo(names, (name) => fetchItem(io, registryUrl, npm, name));
736
- const plans = planWrites(items, io, aliasRoot, config);
685
+ const items = await resolveTopo(finalNames, (name) => fetchItem(io, registryUrl, npm, name));
686
+ const planned = planWrites(items, io, aliasRoot, config);
687
+ const plans = opts.relativeImports ? planned.map((p) => ({
688
+ ...p,
689
+ content: rewriteContent(p.content, p.path, aliasRoot)
690
+ })) : planned;
737
691
  const { dependencies, devDependencies } = collectDeps(items);
738
692
  if (opts.dryRun) {
739
693
  const files = [];
@@ -744,7 +698,7 @@ async function runAdd(io, cwd, names, opts) {
744
698
  files.push(plan.path);
745
699
  continue;
746
700
  }
747
- if (hashContent(io.readFile(plan.path)) === hashContent(plan.content)) upToDate.push(plan.path);
701
+ if (hashNormalizedContent(io.readFile(plan.path)) === hashNormalizedContent(plan.content)) upToDate.push(plan.path);
748
702
  else {
749
703
  files.push(plan.path);
750
704
  overwrites.push(plan.path);
@@ -767,6 +721,7 @@ async function runAdd(io, cwd, names, opts) {
767
721
  });
768
722
  const lockfile = readLockfile(io, root);
769
723
  const knownRemote = /* @__PURE__ */ new Set([...result.written, ...result.upToDate]);
724
+ const contentByPath = new Map(plans.map((p) => [p.path, p.content]));
770
725
  for (const item of items) {
771
726
  const files = {};
772
727
  let allKnown = true;
@@ -776,17 +731,20 @@ async function runAdd(io, cwd, names, opts) {
776
731
  allKnown = false;
777
732
  break;
778
733
  }
779
- files[target] = hashContent(file.content);
734
+ files[target] = hashNormalizedContent(contentByPath.get(target) ?? file.content);
780
735
  }
781
736
  if (allKnown) lockfile.packages[item.name] = {
782
737
  version: item.version ?? null,
783
- files
738
+ files,
739
+ ...opts.relativeImports ? { relativeImports: true } : {}
784
740
  };
785
741
  }
786
742
  writeLockfile(io, root, lockfile);
787
- const pm = detectPackageManagerForProject(io, root);
788
- if (dependencies.length > 0) installDependencies(io, root, dependencies, false, pm);
789
- if (devDependencies.length > 0) installDependencies(io, root, devDependencies, true, pm);
743
+ if (opts.noInstall !== true) {
744
+ const pm = detectPackageManagerForProject(io, root);
745
+ if (dependencies.length > 0) installDependencies(io, root, dependencies, false, pm);
746
+ if (devDependencies.length > 0) installDependencies(io, root, devDependencies, true, pm);
747
+ }
790
748
  return {
791
749
  files: result.written,
792
750
  overwrites: result.overwrites,
@@ -801,6 +759,8 @@ async function runAdd(io, cwd, names, opts) {
801
759
  //#region src/lib/diff-core.ts
802
760
  /**
803
761
  * 本地文件 vs 线上 registry vs lockfile 安装时快照的三方比对。
762
+ * 全部 hash 走空白归一化(hashNormalizedContent);远端基准在
763
+ * relativeImports 标记下同样经过 alias→相对导入 改写,与安装口径对齐。
804
764
  * 每文件按有序决策树判定(first-match):
805
765
  * 1. 本地 === 线上 → up-to-date
806
766
  * 2. 无安装记录(legacy)且与线上不同 → different(两方对比降级)
@@ -833,12 +793,12 @@ async function diffComponents(io, fetcher, registryUrl, npm, root, config, names
833
793
  diff.remoteVersion = item.version ?? null;
834
794
  for (const file of item.files) {
835
795
  const target = resolveTarget(file.target, config, aliasRoot);
836
- const remoteHash = hashContent(file.content);
796
+ const remoteHash = hashNormalizedContent(record?.relativeImports === true ? rewriteContent(file.content, target, aliasRoot) : file.content);
837
797
  const installedHash = record?.files[target] ?? null;
838
798
  let status;
839
799
  if (!io.exists(target)) status = "missing";
840
800
  else {
841
- const localHash = hashContent(io.readFile(target));
801
+ const localHash = hashNormalizedContent(io.readFile(target));
842
802
  if (localHash === remoteHash) status = "up-to-date";
843
803
  else if (installedHash === null) status = "different";
844
804
  else if (localHash === installedHash) status = "updatable";
@@ -904,9 +864,10 @@ program.command("init").description("初始化 components.json 并注入基础
904
864
  process.exitCode = 1;
905
865
  }
906
866
  });
907
- program.command("add <components...>").description("添加组件源码到项目").option("--registry <url>", "覆盖 components.json 中的 registry URL").option("--overwrite", "覆盖已存在的文件而不询问").option("--dry-run", "预览模式:报告将写入/覆盖/已最新的文件,不实际写入").action(async (components, opts) => {
867
+ program.command("add [components...]").description("添加组件源码到项目(--all 时省略组件名,升级 components.lock.json 记录的全部组件)").option("--registry <url>", "覆盖 components.json 中的 registry URL").option("--overwrite", "覆盖已存在的文件而不询问").option("--dry-run", "预览模式:报告将写入/覆盖/已最新的文件,不实际写入").option("--all", "全量升级 components.lock.json 记录的全部组件(与组件名互斥)").option("--no-install", "跳过依赖安装,仅输出依赖清单(workspace:/catalog: 协议场景)").option("--relative-imports", "把写入源码中的 @/ alias 导入改写为相对导入").action(async (components, opts) => {
908
868
  const cwd = process.cwd();
909
869
  try {
870
+ if (components.length === 0 && opts.all !== true) throw new Error("请指定组件名,或使用 --all 全量升级 lockfile 记录的全部组件");
910
871
  const summary = await runAdd({
911
872
  ...nodeIo,
912
873
  async fetchJson(url) {
@@ -919,6 +880,9 @@ program.command("add <components...>").description("添加组件源码到项目"
919
880
  registry: opts.registry,
920
881
  overwrite: opts.overwrite,
921
882
  dryRun: opts.dryRun,
883
+ all: opts.all,
884
+ noInstall: opts.install === false,
885
+ relativeImports: opts.relativeImports,
922
886
  npm: nodeNpmChannel,
923
887
  confirm: process.stdin.isTTY ? async (msg) => {
924
888
  const { default: prompts } = await import("prompts");
@@ -946,7 +910,7 @@ program.command("add <components...>").description("添加组件源码到项目"
946
910
  for (const f of summary.upToDate) console.log(` ${f}`);
947
911
  }
948
912
  const willInstall = [...summary.dependencies, ...summary.devDependencies];
949
- if (willInstall.length > 0) console.log(`ℹ 将安装依赖(dry-run 未执行):${willInstall.join(" ")}`);
913
+ if (willInstall.length > 0) console.log(opts.install === false ? `ℹ --no-install:请自行安装依赖:${willInstall.join(" ")}` : `ℹ 将安装依赖(dry-run 未执行):${willInstall.join(" ")}`);
950
914
  return;
951
915
  }
952
916
  if (summary.files.length > 0) {
@@ -968,6 +932,10 @@ program.command("add <components...>").description("添加组件源码到项目"
968
932
  }
969
933
  for (const d of summary.dependencies) console.log(`✔ 依赖 ${picocolors.cyan(d)}`);
970
934
  for (const d of summary.devDependencies) console.log(`✔ 开发依赖 ${picocolors.cyan(d)}`);
935
+ if (opts.install === false) {
936
+ const pending = [...summary.dependencies, ...summary.devDependencies];
937
+ if (pending.length > 0) console.log(`ℹ --no-install:请自行安装依赖:${pending.join(" ")}`);
938
+ }
971
939
  } catch (error) {
972
940
  console.error(picocolors.red(`✖ ${error instanceof Error ? error.message : String(error)}`));
973
941
  process.exitCode = 1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tu-design/cli",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "tu-design CLI:初始化并按 registry 分发组件源码到用户项目(shadcn 模式)。",
5
5
  "keywords": [
6
6
  "cli",
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "@modelcontextprotocol/sdk": "^1.32.2",
38
- "@tu-design/vue": "0.5.0",
38
+ "@tu-design/vue": "0.5.1",
39
39
  "commander": "^15",
40
40
  "picocolors": "^1",
41
41
  "prompts": "^2",
@@ -49,6 +49,7 @@
49
49
  "vitest": "^5.0.1"
50
50
  },
51
51
  "scripts": {
52
+ "generate:sources": "tsx scripts/generate-sources.ts && vp fmt src/lib/generated-sources.ts",
52
53
  "build": "vp pack && tsx scripts/fix-bin.ts",
53
54
  "typecheck": "tsc --noEmit",
54
55
  "test": "vp test --run"