@tu-design/cli 0.4.3 → 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.
- package/README.md +5 -0
- package/dist/index.mjs +137 -129
- 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,119 +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 = `@import "tw-animate-css";
|
|
221
|
-
|
|
222
|
-
@custom-variant dark (&:where(.dark, .dark *));
|
|
223
|
-
|
|
224
|
-
:root {
|
|
225
|
-
--background: oklch(1 0 0);
|
|
226
|
-
--foreground: oklch(0.145 0 0);
|
|
227
|
-
--card: oklch(1 0 0);
|
|
228
|
-
--card-foreground: oklch(0.145 0 0);
|
|
229
|
-
--popover: oklch(1 0 0);
|
|
230
|
-
--popover-foreground: oklch(0.145 0 0);
|
|
231
|
-
--primary: oklch(0.205 0 0);
|
|
232
|
-
--primary-foreground: oklch(0.985 0 0);
|
|
233
|
-
--secondary: oklch(0.97 0 0);
|
|
234
|
-
--secondary-foreground: oklch(0.205 0 0);
|
|
235
|
-
--muted: oklch(0.97 0 0);
|
|
236
|
-
--muted-foreground: oklch(0.556 0 0);
|
|
237
|
-
--accent: oklch(0.97 0 0);
|
|
238
|
-
--accent-foreground: oklch(0.205 0 0);
|
|
239
|
-
--destructive: oklch(0.577 0.245 27.325);
|
|
240
|
-
--success: oklch(0.596 0.145 163.225);
|
|
241
|
-
--success-foreground: oklch(0.985 0 0);
|
|
242
|
-
--warning: oklch(0.666 0.179 58.318);
|
|
243
|
-
--warning-foreground: oklch(0.985 0 0);
|
|
244
|
-
--info: oklch(0.546 0.245 262.881);
|
|
245
|
-
--info-foreground: oklch(0.985 0 0);
|
|
246
|
-
--border: oklch(0.922 0 0);
|
|
247
|
-
--input: oklch(0.922 0 0);
|
|
248
|
-
--ring: oklch(0.708 0 0);
|
|
249
|
-
--radius: 0.625rem;
|
|
250
|
-
/* 层叠序:表体 sticky < 表头 sticky < 浮层/弹窗(同级浮层按 DOM 挂载次序叠放) */
|
|
251
|
-
--z-sticky: 10;
|
|
252
|
-
--z-sticky-head: 20;
|
|
253
|
-
--z-overlay: 50;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
.dark {
|
|
257
|
-
--background: oklch(0.145 0 0);
|
|
258
|
-
--foreground: oklch(0.985 0 0);
|
|
259
|
-
--card: oklch(0.205 0 0);
|
|
260
|
-
--card-foreground: oklch(0.985 0 0);
|
|
261
|
-
--popover: oklch(0.269 0 0);
|
|
262
|
-
--popover-foreground: oklch(0.985 0 0);
|
|
263
|
-
--primary: oklch(0.922 0 0);
|
|
264
|
-
--primary-foreground: oklch(0.205 0 0);
|
|
265
|
-
--secondary: oklch(0.269 0 0);
|
|
266
|
-
--secondary-foreground: oklch(0.985 0 0);
|
|
267
|
-
--muted: oklch(0.269 0 0);
|
|
268
|
-
--muted-foreground: oklch(0.708 0 0);
|
|
269
|
-
--accent: oklch(0.371 0 0);
|
|
270
|
-
--accent-foreground: oklch(0.985 0 0);
|
|
271
|
-
--destructive: oklch(0.704 0.191 22.216);
|
|
272
|
-
--success: oklch(0.696 0.17 162.48);
|
|
273
|
-
--success-foreground: oklch(0.985 0 0);
|
|
274
|
-
--warning: oklch(0.769 0.188 70.08);
|
|
275
|
-
--warning-foreground: oklch(0.205 0 0);
|
|
276
|
-
--info: oklch(0.623 0.214 259.815);
|
|
277
|
-
--info-foreground: oklch(0.985 0 0);
|
|
278
|
-
--border: oklch(1 0 0 / 10%);
|
|
279
|
-
--input: oklch(1 0 0 / 15%);
|
|
280
|
-
--ring: oklch(0.556 0 0);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
@theme inline {
|
|
284
|
-
--radius-sm: calc(var(--radius) - 4px);
|
|
285
|
-
--radius-md: calc(var(--radius) - 2px);
|
|
286
|
-
--radius-lg: var(--radius);
|
|
287
|
-
--radius-xl: calc(var(--radius) + 4px);
|
|
288
|
-
--color-background: var(--background);
|
|
289
|
-
--color-foreground: var(--foreground);
|
|
290
|
-
--color-card: var(--card);
|
|
291
|
-
--color-card-foreground: var(--card-foreground);
|
|
292
|
-
--color-popover: var(--popover);
|
|
293
|
-
--color-popover-foreground: var(--popover-foreground);
|
|
294
|
-
--color-primary: var(--primary);
|
|
295
|
-
--color-primary-foreground: var(--primary-foreground);
|
|
296
|
-
--color-secondary: var(--secondary);
|
|
297
|
-
--color-secondary-foreground: var(--secondary-foreground);
|
|
298
|
-
--color-muted: var(--muted);
|
|
299
|
-
--color-muted-foreground: var(--muted-foreground);
|
|
300
|
-
--color-accent: var(--accent);
|
|
301
|
-
--color-accent-foreground: var(--accent-foreground);
|
|
302
|
-
--color-destructive: var(--destructive);
|
|
303
|
-
--color-success: var(--success);
|
|
304
|
-
--color-success-foreground: var(--success-foreground);
|
|
305
|
-
--color-warning: var(--warning);
|
|
306
|
-
--color-warning-foreground: var(--warning-foreground);
|
|
307
|
-
--color-info: var(--info);
|
|
308
|
-
--color-info-foreground: var(--info-foreground);
|
|
309
|
-
--color-border: var(--border);
|
|
310
|
-
--color-input: var(--input);
|
|
311
|
-
--color-ring: var(--ring);
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
/* 全局边框色兜底,对齐 shadcn v4 官方 tokens(上游另含 outline-ring/50,此处仅移植 border):
|
|
315
|
-
裸 border 类不再回退 currentColor,而是使用 --border token。 */
|
|
316
|
-
@layer base {
|
|
317
|
-
* {
|
|
318
|
-
border-color: var(--color-border);
|
|
319
|
-
}
|
|
320
|
-
}`;
|
|
321
|
-
/** cn 工具模板,等价 @tu-design/vue/src/lib/utils.ts。 */
|
|
322
|
-
const CN_UTIL_SOURCE = `import { clsx, type ClassValue } from "clsx";
|
|
323
|
-
import { twMerge } from "tailwind-merge";
|
|
324
|
-
|
|
325
|
-
export function cn(...inputs: ClassValue[]) {
|
|
326
|
-
return twMerge(clsx(inputs));
|
|
327
|
-
}
|
|
328
|
-
`;
|
|
329
220
|
function detectTailwindV4(pkg, globalCss) {
|
|
330
221
|
const tw = {
|
|
331
222
|
...pkg.dependencies,
|
|
@@ -516,6 +407,7 @@ function validateLockfile(raw) {
|
|
|
516
407
|
const entry = pkg;
|
|
517
408
|
if (entry.version !== null && typeof entry.version !== "string") throw new Error(`${LOCKFILE_NAME} 的 packages.${name}.version 必须是字符串或 null,得到 ${typeof entry.version}`);
|
|
518
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}`);
|
|
519
411
|
const files = {};
|
|
520
412
|
for (const [path, hash] of Object.entries(entry.files)) {
|
|
521
413
|
if (typeof hash !== "string") throw new Error(`${LOCKFILE_NAME} 的 packages.${name}.files.${path} 必须是字符串,得到 ${typeof hash}`);
|
|
@@ -523,7 +415,8 @@ function validateLockfile(raw) {
|
|
|
523
415
|
}
|
|
524
416
|
out[name] = {
|
|
525
417
|
version: entry.version,
|
|
526
|
-
files
|
|
418
|
+
files,
|
|
419
|
+
...entry.relativeImports === true ? { relativeImports: true } : {}
|
|
527
420
|
};
|
|
528
421
|
}
|
|
529
422
|
return {
|
|
@@ -544,6 +437,70 @@ function writeLockfile(io, root, lockfile) {
|
|
|
544
437
|
io.writeFile(`${root}/${LOCKFILE_NAME}`, `${JSON.stringify(lockfile, null, 2)}\n`);
|
|
545
438
|
}
|
|
546
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
|
|
547
504
|
//#region src/lib/write.ts
|
|
548
505
|
function parentDir(dir) {
|
|
549
506
|
if (dir === "/") return null;
|
|
@@ -603,8 +560,9 @@ function planWrites(items, io, aliasRoot, config) {
|
|
|
603
560
|
return plans;
|
|
604
561
|
}
|
|
605
562
|
/**
|
|
606
|
-
*
|
|
607
|
-
*
|
|
563
|
+
* 覆盖安全语义:归一化内容比对(sha256,空白不敏感)优先于存在性判断——
|
|
564
|
+
* 内容相同的重复安装不算冲突,静默跳过;只有「存在且归一化内容不同」才进入
|
|
565
|
+
* 覆盖询问 / conflict 分支。空白不敏感:用户 fmt 重排不触发覆盖询问。
|
|
608
566
|
*/
|
|
609
567
|
async function writeFiles(plans, io, opts) {
|
|
610
568
|
const result = {
|
|
@@ -616,7 +574,7 @@ async function writeFiles(plans, io, opts) {
|
|
|
616
574
|
};
|
|
617
575
|
for (const plan of plans) {
|
|
618
576
|
if (io.exists(plan.path)) {
|
|
619
|
-
if (
|
|
577
|
+
if (hashNormalizedContent(io.readFile(plan.path)) === hashNormalizedContent(plan.content)) {
|
|
620
578
|
result.upToDate.push(plan.path);
|
|
621
579
|
continue;
|
|
622
580
|
}
|
|
@@ -683,17 +641,53 @@ function collectDeps(items) {
|
|
|
683
641
|
function aliasRootOf(root) {
|
|
684
642
|
return `${root}/src`;
|
|
685
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
|
+
*/
|
|
686
675
|
async function runAdd(io, cwd, names, opts) {
|
|
687
676
|
const root = findProjectRoot(io, cwd);
|
|
688
677
|
const config = loadConfig(io, root);
|
|
689
678
|
const registryUrl = resolveRegistryUrl(config, opts.registry);
|
|
690
679
|
const aliasRoot = aliasRootOf(root);
|
|
680
|
+
const finalNames = resolveNames(io, root, names, opts.all);
|
|
691
681
|
const npm = opts.npm ?? {
|
|
692
682
|
hasItem: () => false,
|
|
693
683
|
readItem: () => void 0
|
|
694
684
|
};
|
|
695
|
-
const items = await resolveTopo(
|
|
696
|
-
const
|
|
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;
|
|
697
691
|
const { dependencies, devDependencies } = collectDeps(items);
|
|
698
692
|
if (opts.dryRun) {
|
|
699
693
|
const files = [];
|
|
@@ -704,7 +698,7 @@ async function runAdd(io, cwd, names, opts) {
|
|
|
704
698
|
files.push(plan.path);
|
|
705
699
|
continue;
|
|
706
700
|
}
|
|
707
|
-
if (
|
|
701
|
+
if (hashNormalizedContent(io.readFile(plan.path)) === hashNormalizedContent(plan.content)) upToDate.push(plan.path);
|
|
708
702
|
else {
|
|
709
703
|
files.push(plan.path);
|
|
710
704
|
overwrites.push(plan.path);
|
|
@@ -727,6 +721,7 @@ async function runAdd(io, cwd, names, opts) {
|
|
|
727
721
|
});
|
|
728
722
|
const lockfile = readLockfile(io, root);
|
|
729
723
|
const knownRemote = /* @__PURE__ */ new Set([...result.written, ...result.upToDate]);
|
|
724
|
+
const contentByPath = new Map(plans.map((p) => [p.path, p.content]));
|
|
730
725
|
for (const item of items) {
|
|
731
726
|
const files = {};
|
|
732
727
|
let allKnown = true;
|
|
@@ -736,17 +731,20 @@ async function runAdd(io, cwd, names, opts) {
|
|
|
736
731
|
allKnown = false;
|
|
737
732
|
break;
|
|
738
733
|
}
|
|
739
|
-
files[target] =
|
|
734
|
+
files[target] = hashNormalizedContent(contentByPath.get(target) ?? file.content);
|
|
740
735
|
}
|
|
741
736
|
if (allKnown) lockfile.packages[item.name] = {
|
|
742
737
|
version: item.version ?? null,
|
|
743
|
-
files
|
|
738
|
+
files,
|
|
739
|
+
...opts.relativeImports ? { relativeImports: true } : {}
|
|
744
740
|
};
|
|
745
741
|
}
|
|
746
742
|
writeLockfile(io, root, lockfile);
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
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
|
+
}
|
|
750
748
|
return {
|
|
751
749
|
files: result.written,
|
|
752
750
|
overwrites: result.overwrites,
|
|
@@ -761,6 +759,8 @@ async function runAdd(io, cwd, names, opts) {
|
|
|
761
759
|
//#region src/lib/diff-core.ts
|
|
762
760
|
/**
|
|
763
761
|
* 本地文件 vs 线上 registry vs lockfile 安装时快照的三方比对。
|
|
762
|
+
* 全部 hash 走空白归一化(hashNormalizedContent);远端基准在
|
|
763
|
+
* relativeImports 标记下同样经过 alias→相对导入 改写,与安装口径对齐。
|
|
764
764
|
* 每文件按有序决策树判定(first-match):
|
|
765
765
|
* 1. 本地 === 线上 → up-to-date
|
|
766
766
|
* 2. 无安装记录(legacy)且与线上不同 → different(两方对比降级)
|
|
@@ -793,12 +793,12 @@ async function diffComponents(io, fetcher, registryUrl, npm, root, config, names
|
|
|
793
793
|
diff.remoteVersion = item.version ?? null;
|
|
794
794
|
for (const file of item.files) {
|
|
795
795
|
const target = resolveTarget(file.target, config, aliasRoot);
|
|
796
|
-
const remoteHash =
|
|
796
|
+
const remoteHash = hashNormalizedContent(record?.relativeImports === true ? rewriteContent(file.content, target, aliasRoot) : file.content);
|
|
797
797
|
const installedHash = record?.files[target] ?? null;
|
|
798
798
|
let status;
|
|
799
799
|
if (!io.exists(target)) status = "missing";
|
|
800
800
|
else {
|
|
801
|
-
const localHash =
|
|
801
|
+
const localHash = hashNormalizedContent(io.readFile(target));
|
|
802
802
|
if (localHash === remoteHash) status = "up-to-date";
|
|
803
803
|
else if (installedHash === null) status = "different";
|
|
804
804
|
else if (localHash === installedHash) status = "updatable";
|
|
@@ -864,9 +864,10 @@ program.command("init").description("初始化 components.json 并注入基础
|
|
|
864
864
|
process.exitCode = 1;
|
|
865
865
|
}
|
|
866
866
|
});
|
|
867
|
-
program.command("add
|
|
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) => {
|
|
868
868
|
const cwd = process.cwd();
|
|
869
869
|
try {
|
|
870
|
+
if (components.length === 0 && opts.all !== true) throw new Error("请指定组件名,或使用 --all 全量升级 lockfile 记录的全部组件");
|
|
870
871
|
const summary = await runAdd({
|
|
871
872
|
...nodeIo,
|
|
872
873
|
async fetchJson(url) {
|
|
@@ -879,6 +880,9 @@ program.command("add <components...>").description("添加组件源码到项目"
|
|
|
879
880
|
registry: opts.registry,
|
|
880
881
|
overwrite: opts.overwrite,
|
|
881
882
|
dryRun: opts.dryRun,
|
|
883
|
+
all: opts.all,
|
|
884
|
+
noInstall: opts.install === false,
|
|
885
|
+
relativeImports: opts.relativeImports,
|
|
882
886
|
npm: nodeNpmChannel,
|
|
883
887
|
confirm: process.stdin.isTTY ? async (msg) => {
|
|
884
888
|
const { default: prompts } = await import("prompts");
|
|
@@ -906,7 +910,7 @@ program.command("add <components...>").description("添加组件源码到项目"
|
|
|
906
910
|
for (const f of summary.upToDate) console.log(` ${f}`);
|
|
907
911
|
}
|
|
908
912
|
const willInstall = [...summary.dependencies, ...summary.devDependencies];
|
|
909
|
-
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(" ")}`);
|
|
910
914
|
return;
|
|
911
915
|
}
|
|
912
916
|
if (summary.files.length > 0) {
|
|
@@ -928,6 +932,10 @@ program.command("add <components...>").description("添加组件源码到项目"
|
|
|
928
932
|
}
|
|
929
933
|
for (const d of summary.dependencies) console.log(`✔ 依赖 ${picocolors.cyan(d)}`);
|
|
930
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
|
+
}
|
|
931
939
|
} catch (error) {
|
|
932
940
|
console.error(picocolors.red(`✖ ${error instanceof Error ? error.message : String(error)}`));
|
|
933
941
|
process.exitCode = 1;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tu-design/cli",
|
|
3
|
-
"version": "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.
|
|
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"
|