@coze-arch/cli 0.0.1-alpha.912cd5 → 0.0.1-alpha.934b8f

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 (134) hide show
  1. package/lib/__templates__/expo/.coze +7 -2
  2. package/lib/__templates__/expo/.cozeproj/scripts/dev_build.sh +46 -0
  3. package/lib/__templates__/expo/.cozeproj/scripts/dev_run.sh +229 -0
  4. package/lib/__templates__/expo/.cozeproj/scripts/prod_build.sh +47 -0
  5. package/lib/__templates__/expo/.cozeproj/scripts/prod_run.sh +34 -0
  6. package/lib/__templates__/expo/.cozeproj/scripts/server_dev_run.sh +46 -0
  7. package/lib/__templates__/expo/README.md +68 -7
  8. package/lib/__templates__/expo/_gitignore +1 -1
  9. package/lib/__templates__/expo/_npmrc +3 -5
  10. package/lib/__templates__/expo/client/app/+not-found.tsx +15 -64
  11. package/lib/__templates__/expo/client/app/_layout.tsx +15 -12
  12. package/lib/__templates__/expo/client/app/index.tsx +1 -0
  13. package/lib/__templates__/expo/client/app.config.ts +76 -0
  14. package/lib/__templates__/expo/client/components/Screen.tsx +1 -17
  15. package/lib/__templates__/expo/client/components/ThemedText.tsx +33 -0
  16. package/lib/__templates__/expo/client/components/ThemedView.tsx +37 -0
  17. package/lib/__templates__/expo/client/constants/theme.ts +117 -58
  18. package/lib/__templates__/expo/client/contexts/AuthContext.tsx +14 -107
  19. package/lib/__templates__/expo/client/declarations.d.ts +5 -0
  20. package/lib/__templates__/expo/{eslint.config.mjs → client/eslint.config.mjs} +33 -10
  21. package/lib/__templates__/expo/client/hooks/useColorScheme.tsx +48 -0
  22. package/lib/__templates__/expo/client/hooks/useSafeRouter.ts +152 -0
  23. package/lib/__templates__/expo/client/hooks/useTheme.ts +26 -6
  24. package/lib/__templates__/expo/client/metro.config.js +121 -0
  25. package/lib/__templates__/expo/client/package.json +95 -0
  26. package/lib/__templates__/expo/client/screens/demo/index.tsx +25 -0
  27. package/lib/__templates__/expo/client/screens/demo/styles.ts +28 -0
  28. package/lib/__templates__/expo/client/scripts/install-missing-deps.js +1 -0
  29. package/lib/__templates__/expo/client/tsconfig.json +24 -0
  30. package/lib/__templates__/expo/client/utils/index.ts +23 -2
  31. package/lib/__templates__/expo/eslint-plugins/fontawesome6/index.js +9 -0
  32. package/lib/__templates__/expo/eslint-plugins/fontawesome6/names.js +1889 -0
  33. package/lib/__templates__/expo/eslint-plugins/fontawesome6/rule.js +174 -0
  34. package/lib/__templates__/expo/eslint-plugins/fontawesome6/v5-only-names.js +388 -0
  35. package/lib/__templates__/expo/eslint-plugins/react-native/index.js +9 -0
  36. package/lib/__templates__/expo/eslint-plugins/react-native/rule.js +64 -0
  37. package/lib/__templates__/expo/eslint-plugins/reanimated/index.js +9 -0
  38. package/lib/__templates__/expo/eslint-plugins/reanimated/rule.js +88 -0
  39. package/lib/__templates__/expo/package.json +16 -101
  40. package/lib/__templates__/expo/patches/expo@54.0.32.patch +45 -0
  41. package/lib/__templates__/expo/pnpm-lock.yaml +1534 -3359
  42. package/lib/__templates__/expo/pnpm-workspace.yaml +3 -0
  43. package/lib/__templates__/expo/server/build.js +21 -0
  44. package/lib/__templates__/expo/server/package.json +32 -0
  45. package/lib/__templates__/expo/server/src/index.ts +19 -0
  46. package/lib/__templates__/expo/server/tsconfig.json +24 -0
  47. package/lib/__templates__/expo/template.config.js +2 -1
  48. package/lib/__templates__/expo/tsconfig.json +1 -24
  49. package/lib/__templates__/nextjs/.coze +4 -3
  50. package/lib/__templates__/nextjs/_npmrc +2 -1
  51. package/lib/__templates__/nextjs/next.config.ts +12 -0
  52. package/lib/__templates__/nextjs/package.json +16 -1
  53. package/lib/__templates__/nextjs/pnpm-lock.yaml +3294 -1030
  54. package/lib/__templates__/nextjs/scripts/dev.sh +8 -27
  55. package/lib/__templates__/nextjs/scripts/prepare.sh +9 -0
  56. package/lib/__templates__/nextjs/src/app/globals.css +109 -89
  57. package/lib/__templates__/nextjs/src/app/layout.tsx +19 -32
  58. package/lib/__templates__/nextjs/src/app/page.tsx +16 -48
  59. package/lib/__templates__/nextjs/src/components/ui/resizable.tsx +29 -22
  60. package/lib/__templates__/nextjs/src/components/ui/sidebar.tsx +228 -230
  61. package/lib/__templates__/nextjs/template.config.js +31 -1
  62. package/lib/__templates__/taro/.coze +14 -0
  63. package/lib/__templates__/taro/.cozeproj/scripts/deploy_build.sh +19 -0
  64. package/lib/__templates__/taro/.cozeproj/scripts/deploy_run.sh +13 -0
  65. package/lib/__templates__/taro/.cozeproj/scripts/dev_build.sh +16 -0
  66. package/lib/__templates__/taro/.cozeproj/scripts/dev_run.sh +74 -0
  67. package/lib/__templates__/taro/.cozeproj/scripts/init_env.sh +5 -0
  68. package/lib/__templates__/taro/.cozeproj/scripts/pack.sh +1 -0
  69. package/lib/__templates__/taro/README.md +687 -0
  70. package/lib/__templates__/taro/_gitignore +40 -0
  71. package/lib/__templates__/taro/_npmrc +18 -0
  72. package/lib/__templates__/taro/babel.config.js +12 -0
  73. package/lib/__templates__/taro/config/dev.ts +9 -0
  74. package/lib/__templates__/taro/config/index.ts +173 -0
  75. package/lib/__templates__/taro/config/prod.ts +35 -0
  76. package/lib/__templates__/taro/eslint.config.mjs +57 -0
  77. package/lib/__templates__/taro/key/private.appid.key +0 -0
  78. package/lib/__templates__/taro/package.json +95 -0
  79. package/lib/__templates__/taro/pnpm-lock.yaml +22430 -0
  80. package/lib/__templates__/taro/pnpm-workspace.yaml +2 -0
  81. package/lib/__templates__/taro/project.config.json +15 -0
  82. package/lib/__templates__/taro/server/nest-cli.json +10 -0
  83. package/lib/__templates__/taro/server/package.json +38 -0
  84. package/lib/__templates__/taro/server/src/app.controller.ts +23 -0
  85. package/lib/__templates__/taro/server/src/app.module.ts +10 -0
  86. package/lib/__templates__/taro/server/src/app.service.ts +8 -0
  87. package/lib/__templates__/taro/server/src/interceptors/http-status.interceptor.ts +23 -0
  88. package/lib/__templates__/taro/server/src/main.ts +37 -0
  89. package/lib/__templates__/taro/server/tsconfig.json +24 -0
  90. package/lib/__templates__/taro/src/app.config.ts +11 -0
  91. package/lib/__templates__/taro/src/app.css +52 -0
  92. package/lib/__templates__/taro/src/app.ts +14 -0
  93. package/lib/__templates__/taro/src/index.html +50 -0
  94. package/lib/__templates__/taro/src/network.ts +39 -0
  95. package/lib/__templates__/taro/src/pages/index/index.config.ts +3 -0
  96. package/lib/__templates__/taro/src/pages/index/index.css +1 -0
  97. package/lib/__templates__/taro/src/pages/index/index.tsx +33 -0
  98. package/lib/__templates__/taro/src/utils/h5-styles.ts +22 -0
  99. package/lib/__templates__/taro/src/utils/wx-debug.ts +23 -0
  100. package/lib/__templates__/taro/stylelint.config.mjs +4 -0
  101. package/lib/__templates__/taro/template.config.js +68 -0
  102. package/lib/__templates__/taro/tsconfig.json +29 -0
  103. package/lib/__templates__/taro/types/global.d.ts +32 -0
  104. package/lib/__templates__/templates.json +93 -36
  105. package/lib/__templates__/vite/.coze +4 -3
  106. package/lib/__templates__/vite/README.md +204 -26
  107. package/lib/__templates__/vite/_npmrc +2 -1
  108. package/lib/__templates__/vite/eslint.config.mjs +9 -0
  109. package/lib/__templates__/vite/package.json +11 -2
  110. package/lib/__templates__/vite/pnpm-lock.yaml +3232 -243
  111. package/lib/__templates__/vite/scripts/dev.sh +7 -26
  112. package/lib/__templates__/vite/scripts/prepare.sh +9 -0
  113. package/lib/__templates__/vite/src/main.ts +17 -48
  114. package/lib/__templates__/vite/template.config.js +40 -5
  115. package/lib/__templates__/vite/vite.config.ts +3 -3
  116. package/lib/cli.js +1479 -461
  117. package/package.json +10 -4
  118. package/lib/__templates__/expo/.cozeproj/scripts/deploy_build.sh +0 -109
  119. package/lib/__templates__/expo/.cozeproj/scripts/deploy_run.sh +0 -257
  120. package/lib/__templates__/expo/app.json +0 -63
  121. package/lib/__templates__/expo/babel.config.js +0 -9
  122. package/lib/__templates__/expo/client/app/(tabs)/_layout.tsx +0 -43
  123. package/lib/__templates__/expo/client/app/(tabs)/home.tsx +0 -1
  124. package/lib/__templates__/expo/client/app/(tabs)/index.tsx +0 -7
  125. package/lib/__templates__/expo/client/hooks/useColorScheme.ts +0 -1
  126. package/lib/__templates__/expo/client/index.js +0 -12
  127. package/lib/__templates__/expo/client/screens/home/index.tsx +0 -54
  128. package/lib/__templates__/expo/client/screens/home/styles.ts +0 -332
  129. package/lib/__templates__/expo/metro.config.js +0 -53
  130. package/lib/__templates__/expo/src/index.ts +0 -12
  131. package/lib/__templates__/nextjs/.vscode/settings.json +0 -121
  132. package/lib/__templates__/nextjs/server.mjs +0 -50
  133. package/lib/__templates__/vite/.vscode/settings.json +0 -7
  134. /package/lib/__templates__/expo/{eslint-formatter-simple.mjs → client/eslint-formatter-simple.mjs} +0 -0
package/lib/cli.js CHANGED
@@ -5,12 +5,15 @@ var commander = require('commander');
5
5
  var path = require('path');
6
6
  var fs = require('fs');
7
7
  var shelljs = require('shelljs');
8
+ var perf_hooks = require('perf_hooks');
8
9
  var fs$1 = require('fs/promises');
9
- var toml = require('@iarna/toml');
10
+ var os = require('os');
10
11
  var jsYaml = require('js-yaml');
11
- var perf_hooks = require('perf_hooks');
12
+ var toml = require('@iarna/toml');
13
+ var child_process = require('child_process');
12
14
  var addFormats = require('ajv-formats');
13
15
  var Ajv = require('ajv');
16
+ var minimist = require('minimist');
14
17
  var changeCase = require('change-case');
15
18
  var ejs = require('ejs');
16
19
 
@@ -124,7 +127,7 @@ const generateTemplatesHelpText = () => {
124
127
  return lines.join('\n');
125
128
  };
126
129
 
127
- function _nullishCoalesce$2(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain$4(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var LogLevel; (function (LogLevel) {
130
+ function _nullishCoalesce$2(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain$3(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var LogLevel; (function (LogLevel) {
128
131
  const ERROR = 0; LogLevel[LogLevel["ERROR"] = ERROR] = "ERROR";
129
132
  const WARN = 1; LogLevel[LogLevel["WARN"] = WARN] = "WARN";
130
133
  const SUCCESS = 2; LogLevel[LogLevel["SUCCESS"] = SUCCESS] = "SUCCESS";
@@ -171,7 +174,7 @@ class Logger {
171
174
  return level;
172
175
  }
173
176
 
174
- const envLevel = _optionalChain$4([process, 'access', _ => _.env, 'access', _2 => _2.LOG_LEVEL, 'optionalAccess', _3 => _3.toLowerCase, 'call', _4 => _4()]);
177
+ const envLevel = _optionalChain$3([process, 'access', _ => _.env, 'access', _2 => _2.LOG_LEVEL, 'optionalAccess', _3 => _3.toLowerCase, 'call', _4 => _4()]);
175
178
  if (envLevel && envLevel in LOG_LEVEL_MAP) {
176
179
  return LOG_LEVEL_MAP[envLevel];
177
180
  }
@@ -183,7 +186,7 @@ class Logger {
183
186
  // 简单检测:Node.js 环境且支持 TTY
184
187
  return (
185
188
  typeof process !== 'undefined' &&
186
- _optionalChain$4([process, 'access', _5 => _5.stdout, 'optionalAccess', _6 => _6.isTTY]) === true &&
189
+ _optionalChain$3([process, 'access', _5 => _5.stdout, 'optionalAccess', _6 => _6.isTTY]) === true &&
187
190
  process.env.NO_COLOR === undefined
188
191
  );
189
192
  }
@@ -269,123 +272,446 @@ const createLogger = (options = {}) =>
269
272
  // 导出默认实例
270
273
  const logger = createLogger();
271
274
 
272
- function _optionalChain$3(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }/* eslint-disable @typescript-eslint/no-explicit-any */
273
- // Safe JSON parsing utilities with type safety and error handling
274
- // Provides fallback values, validation, and error monitoring capabilities
275
-
276
275
  /**
277
- * Options for safe JSON parsing
276
+ * 时间统计工具
278
277
  */
278
+ class TimeTracker {
279
+
280
+
279
281
 
282
+ constructor() {
283
+ this.startTime = perf_hooks.performance.now();
284
+ this.lastTime = this.startTime;
285
+ }
280
286
 
287
+ /**
288
+ * 记录阶段耗时
289
+ * @param phaseName 阶段名称
290
+ */
291
+ logPhase(phaseName) {
292
+ const now = perf_hooks.performance.now();
293
+ const phaseTime = now - this.lastTime;
294
+ this.lastTime = now;
281
295
 
296
+ logger.verbose(`⏱ ${phaseName}: ${phaseTime.toFixed(2)}ms`);
297
+ }
282
298
 
299
+ /**
300
+ * 记录总耗时
301
+ */
302
+ logTotal() {
303
+ const totalTime = perf_hooks.performance.now() - this.startTime;
304
+ logger.verbose(`⏱ Total time: ${totalTime.toFixed(2)}ms`);
305
+ }
283
306
 
307
+ /**
308
+ * 获取当前耗时(不输出日志)
309
+ * @returns 从开始到现在的总耗时(毫秒)
310
+ */
311
+ getElapsedTime() {
312
+ return perf_hooks.performance.now() - this.startTime;
313
+ }
314
+ }
284
315
 
316
+ /**
317
+ * 获取模板配置文件路径
318
+ * @returns templates.json 的绝对路径
319
+ */
320
+ const getTemplatesConfigPath = () => {
321
+ const configPath = path.resolve(getTemplatesDir(), 'templates.json');
322
+ logger.verbose(`Templates config path: ${configPath}`);
323
+ logger.verbose(`Config file exists: ${fs.existsSync(configPath)}`);
324
+ return configPath;
325
+ };
285
326
 
327
+ /**
328
+ * 获取模板目录路径
329
+ * @returns __templates__ 目录的绝对路径
330
+ */
331
+ const getTemplatesDir = () => {
332
+ const templatesDir = path.resolve(__dirname, './__templates__');
333
+ logger.verbose(`Templates directory: ${templatesDir}`);
334
+ logger.verbose(`Templates directory exists: ${fs.existsSync(templatesDir)}`);
335
+ return templatesDir;
336
+ };
286
337
 
338
+ /**
339
+ * 加载模板配置文件
340
+ * 支持 .ts 和 .js 文件(通过 sucrase 注册)
341
+ *
342
+ * @param templatePath - 模板目录路径
343
+ * @returns 模板配置对象
344
+ */
287
345
 
346
+ const loadTemplateConfig = async (
347
+ templatePath,
348
+ ) => {
349
+ logger.verbose(`Loading template config from: ${templatePath}`);
288
350
 
351
+ const tsConfigPath = path.join(templatePath, 'template.config.ts');
352
+ const jsConfigPath = path.join(templatePath, 'template.config.js');
289
353
 
354
+ logger.verbose('Checking for config files:');
355
+ logger.verbose(` - TypeScript: ${tsConfigPath}`);
356
+ logger.verbose(` - JavaScript: ${jsConfigPath}`);
290
357
 
358
+ let configPath;
291
359
 
360
+ const [tsExists, jsExists] = await Promise.all([
361
+ fs$1.access(tsConfigPath).then(
362
+ () => true,
363
+ () => false,
364
+ ),
365
+ fs$1.access(jsConfigPath).then(
366
+ () => true,
367
+ () => false,
368
+ ),
369
+ ]);
292
370
 
371
+ logger.verbose('Config file existence check:');
372
+ logger.verbose(` - template.config.ts: ${tsExists}`);
373
+ logger.verbose(` - template.config.js: ${jsExists}`);
293
374
 
375
+ if (tsExists) {
376
+ configPath = tsConfigPath;
377
+ } else if (jsExists) {
378
+ configPath = jsConfigPath;
379
+ } else {
380
+ throw new Error(
381
+ `Template config not found in ${templatePath}.\n` +
382
+ 'Expected: template.config.ts or template.config.js',
383
+ );
384
+ }
294
385
 
386
+ logger.verbose(`Using config file: ${configPath}`);
295
387
 
388
+ // eslint-disable-next-line @typescript-eslint/no-require-imports, security/detect-non-literal-require -- Sucrase handles .ts files at runtime, path is validated above
389
+ const config = require(configPath);
296
390
 
391
+ logger.verbose('Template config loaded successfully');
297
392
 
393
+ return config.default || config;
394
+ };
298
395
 
396
+ /**
397
+ * 加载模板列表配置
398
+ *
399
+ * @param configPath - templates.json 配置文件路径
400
+ * @returns 模板列表配置
401
+ */
402
+ const loadTemplatesConfig = async (
403
+ configPath,
404
+ ) => {
405
+ logger.verbose(`Loading templates config from: ${configPath}`);
299
406
 
407
+ const content = await fs$1.readFile(configPath, 'utf-8');
408
+ // eslint-disable-next-line no-restricted-syntax -- Static config file loaded at build time, safeJsonParse not needed
409
+ const config = JSON.parse(content) ;
300
410
 
411
+ logger.verbose(
412
+ `Found ${config.templates.length} templates: ${config.templates.map(t => t.name).join(', ')}`,
413
+ );
301
414
 
415
+ return config;
416
+ };
302
417
 
418
+ /**
419
+ * 根据模板名称查找模板元信息
420
+ *
421
+ * @param templatesConfig - 模板列表配置
422
+ * @param templateName - 模板名称
423
+ * @returns 模板元信息
424
+ */
425
+ const findTemplate = (
426
+ templatesConfig,
427
+ templateName,
428
+ ) => {
429
+ const template = templatesConfig.templates.find(t => t.name === templateName);
303
430
 
431
+ if (!template) {
432
+ const availableTemplates = templatesConfig.templates
433
+ .map(t => t.name)
434
+ .join(', ');
435
+ throw new Error(
436
+ `Template "${templateName}" not found.\n` +
437
+ `Available templates: ${availableTemplates}\n` +
438
+ 'Use --template <name> to specify a template.',
439
+ );
440
+ }
304
441
 
442
+ return template;
443
+ };
305
444
 
445
+ /**
446
+ * 获取模板的完整路径
447
+ *
448
+ * @param basePath - 模板目录(templates.json 所在目录)
449
+ * @param templateMetadata - 模板元信息
450
+ * @returns 模板完整路径
451
+ */
452
+ const getTemplatePath = async (
453
+ basePath,
454
+ templateMetadata,
455
+ ) => {
456
+ logger.verbose('Resolving template path:');
457
+ logger.verbose(` - Base path: ${basePath}`);
458
+ logger.verbose(` - Template location: ${templateMetadata.location}`);
306
459
 
460
+ // location 是相对于 templates.json 文件的路径
461
+ const templatePath = path.join(basePath, templateMetadata.location);
307
462
 
463
+ logger.verbose(` - Resolved path: ${templatePath}`);
308
464
 
465
+ try {
466
+ await fs$1.access(templatePath);
467
+ logger.verbose(' - Template directory exists: ✓');
468
+ // eslint-disable-next-line @coze-arch/use-error-in-catch -- Error handling done in throw statement
469
+ } catch (e) {
470
+ logger.error(' - Template directory does not exist: ✗');
471
+ throw new Error(`Template directory not found: ${templatePath}`);
472
+ }
309
473
 
474
+ return templatePath;
475
+ };
310
476
 
311
477
  /**
312
- * Safely parse JSON with error handling and type safety
313
- *
314
- * @example
315
- * ```ts
316
- * // Basic usage - returns unknown | undefined
317
- * const data = safeJsonParse('{"a":1}'); // { a: 1 }
318
- *
319
- * // With default value - always returns T
320
- * const config = safeJsonParse(str, {});
321
- * const user = safeJsonParse(str, null);
322
- *
323
- * // With error reporting
324
- * const data = safeJsonParse(input, null, {
325
- * onError: (error, input) => logger.error('Parse failed', { error, input })
326
- * });
327
- *
328
- * // With validation
329
- * const isUser = (data: unknown): data is User => ...;
330
- * const user = safeJsonParse<User>(input, null, { validate: isUser });
331
- * ```
478
+ * 对单个模板执行 pnpm install
332
479
  */
333
- function safeJsonParse(
334
- input,
335
- defaultValueOrOptions,
336
- optionsArg,
337
- ) {
338
- // Parse arguments
339
- let defaultValue;
340
- let options;
480
+ const warmupTemplate = (templatePath, templateName) => {
481
+ logger.info(`\nWarming up template: ${templateName}`);
482
+ logger.info(` Path: ${templatePath}`);
341
483
 
342
- if (arguments.length === 2) {
343
- // safeJsonParse(input, options) or safeJsonParse(input, defaultValue)
344
- {
345
- defaultValue = defaultValueOrOptions ;
346
- options = undefined;
347
- }
348
- } else if (arguments.length === 3) {
349
- // safeJsonParse(input, defaultValue, options)
350
- defaultValue = defaultValueOrOptions ;
351
- options = optionsArg;
484
+ const result = shelljs.exec('pnpm install', {
485
+ cwd: templatePath,
486
+ silent: true,
487
+ });
488
+
489
+ // 输出 stdout
490
+ if (result.stdout) {
491
+ process.stdout.write(result.stdout);
352
492
  }
353
493
 
354
- // If input is already an object (and not null), return it directly
355
- if (typeof input === 'object' && input !== null) {
356
- return input ;
494
+ // 输出 stderr
495
+ if (result.stderr) {
496
+ process.stderr.write(result.stderr);
357
497
  }
358
498
 
359
- try {
360
- const parsed = JSON.parse(String(input));
499
+ if (result.code === 0) {
500
+ logger.success(` ✓ ${templateName} warmed up successfully`);
501
+ } else {
502
+ const errorMessage = [
503
+ `pnpm install failed for ${templateName} with exit code ${result.code}`,
504
+ result.stderr ? `\nStderr:\n${result.stderr}` : '',
505
+ result.stdout ? `\nStdout:\n${result.stdout}` : '',
506
+ ]
507
+ .filter(Boolean)
508
+ .join('');
361
509
 
362
- // Optional validation
363
- if (_optionalChain$3([options, 'optionalAccess', _ => _.validate])) {
364
- if (options.validate(parsed)) {
365
- return parsed;
366
- } else {
367
- const validationError = new Error('JSON validation failed');
368
- _optionalChain$3([options, 'access', _2 => _2.onError, 'optionalCall', _3 => _3(validationError, input)]);
510
+ throw new Error(errorMessage);
511
+ }
512
+ };
369
513
 
370
- if (options.throwOnValidationError) {
371
- throw validationError;
372
- }
373
- return defaultValue;
374
- }
375
- }
514
+ /**
515
+ * 执行 warmup 命令的内部实现
516
+ */
517
+ const executeWarmup = async (
518
+ options
376
519
 
377
- return parsed;
378
- } catch (error) {
379
- // Re-throw validation errors when throwOnValidationError is true
380
- if (error instanceof Error && error.message === 'JSON validation failed' && _optionalChain$3([options, 'optionalAccess', _4 => _4.throwOnValidationError])) {
520
+ ,
521
+
522
+ command,
523
+ ) => {
524
+ const timer = new TimeTracker();
525
+
526
+ try {
527
+ const { template: templateFilter } = options;
528
+
529
+ logger.info('Starting template warmup...');
530
+ timer.logPhase('Initialization');
531
+
532
+ // 加载模板配置
533
+ const configPath = getTemplatesConfigPath();
534
+ const templatesConfig = await loadTemplatesConfig(configPath);
535
+
536
+ logger.verbose(
537
+ `Found ${templatesConfig.templates.length} templates in config`,
538
+ );
539
+
540
+ // 过滤模板
541
+ const templatesToWarmup = templateFilter
542
+ ? templatesConfig.templates.filter(t => t.name === templateFilter)
543
+ : templatesConfig.templates;
544
+
545
+ if (templatesToWarmup.length === 0) {
546
+ if (templateFilter) {
547
+ logger.warn(`Template "${templateFilter}" not found`);
548
+ logger.info(
549
+ `Available templates: ${templatesConfig.templates.map(t => t.name).join(', ')}`,
550
+ );
551
+ } else {
552
+ logger.warn('No templates found');
553
+ }
554
+ return;
555
+ }
556
+
557
+ logger.info(
558
+ `\nWill warm up ${templatesToWarmup.length} template(s): ${templatesToWarmup.map(t => t.name).join(', ')}`,
559
+ );
560
+
561
+ // 获取模板基础路径
562
+ const basePath = configPath.replace(/\/templates\.json$/, '');
563
+
564
+ // 对每个模板执行 pnpm install
565
+ for (const templateMetadata of templatesToWarmup) {
566
+ const templatePath = await getTemplatePath(basePath, templateMetadata);
567
+ warmupTemplate(templatePath, templateMetadata.name);
568
+ timer.logPhase(`Warmup ${templateMetadata.name}`);
569
+ }
570
+
571
+ logger.success('\n✅ All templates warmed up successfully!');
572
+ logger.info(
573
+ '\nNext time you run `coze init`, it will be much faster as node_modules are pre-installed.',
574
+ );
575
+
576
+ timer.logTotal();
577
+ } catch (error) {
578
+ timer.logTotal();
579
+ logger.error('Failed to warmup templates:');
580
+ logger.error(error instanceof Error ? error.message : String(error));
581
+ process.exit(1);
582
+ }
583
+ };
584
+
585
+ /**
586
+ * 注册 warmup 命令到 program
587
+ */
588
+ const registerCommand$4 = program => {
589
+ program
590
+ .command('warmup')
591
+ .description('Pre-install dependencies for templates to speed up init')
592
+ .option('-t, --template <name>', 'Warmup a specific template only')
593
+ .action(async (options, command) => {
594
+ await executeWarmup(options);
595
+ });
596
+ };
597
+
598
+ function _optionalChain$2(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }/* eslint-disable @typescript-eslint/no-explicit-any */
599
+ // Safe JSON parsing utilities with type safety and error handling
600
+ // Provides fallback values, validation, and error monitoring capabilities
601
+
602
+ /**
603
+ * Options for safe JSON parsing
604
+ */
605
+
606
+
607
+
608
+
609
+
610
+
611
+
612
+
613
+
614
+
615
+
616
+
617
+
618
+
619
+
620
+
621
+
622
+
623
+
624
+
625
+
626
+
627
+
628
+
629
+
630
+
631
+
632
+
633
+
634
+
635
+
636
+
637
+ /**
638
+ * Safely parse JSON with error handling and type safety
639
+ *
640
+ * @example
641
+ * ```ts
642
+ * // Basic usage - returns unknown | undefined
643
+ * const data = safeJsonParse('{"a":1}'); // { a: 1 }
644
+ *
645
+ * // With default value - always returns T
646
+ * const config = safeJsonParse(str, {});
647
+ * const user = safeJsonParse(str, null);
648
+ *
649
+ * // With error reporting
650
+ * const data = safeJsonParse(input, null, {
651
+ * onError: (error, input) => logger.error('Parse failed', { error, input })
652
+ * });
653
+ *
654
+ * // With validation
655
+ * const isUser = (data: unknown): data is User => ...;
656
+ * const user = safeJsonParse<User>(input, null, { validate: isUser });
657
+ * ```
658
+ */
659
+ function safeJsonParse(
660
+ input,
661
+ defaultValueOrOptions,
662
+ optionsArg,
663
+ ) {
664
+ // Parse arguments
665
+ let defaultValue;
666
+ let options;
667
+
668
+ if (arguments.length === 2) {
669
+ // safeJsonParse(input, options) or safeJsonParse(input, defaultValue)
670
+ {
671
+ defaultValue = defaultValueOrOptions ;
672
+ options = undefined;
673
+ }
674
+ } else if (arguments.length === 3) {
675
+ // safeJsonParse(input, defaultValue, options)
676
+ defaultValue = defaultValueOrOptions ;
677
+ options = optionsArg;
678
+ }
679
+
680
+ // If input is already an object (and not null), return it directly
681
+ if (typeof input === 'object' && input !== null) {
682
+ return input ;
683
+ }
684
+
685
+ try {
686
+ const parsed = JSON.parse(String(input));
687
+
688
+ // Optional validation
689
+ if (_optionalChain$2([options, 'optionalAccess', _ => _.validate])) {
690
+ if (options.validate(parsed)) {
691
+ return parsed;
692
+ } else {
693
+ const validationError = new Error('JSON validation failed');
694
+ _optionalChain$2([options, 'access', _2 => _2.onError, 'optionalCall', _3 => _3(validationError, input)]);
695
+
696
+ if (options.throwOnValidationError) {
697
+ throw validationError;
698
+ }
699
+ return defaultValue;
700
+ }
701
+ }
702
+
703
+ return parsed;
704
+ } catch (error) {
705
+ // Re-throw validation errors when throwOnValidationError is true
706
+ if (error instanceof Error && error.message === 'JSON validation failed' && _optionalChain$2([options, 'optionalAccess', _4 => _4.throwOnValidationError])) {
381
707
  throw error;
382
708
  }
383
- _optionalChain$3([options, 'optionalAccess', _5 => _5.onError, 'optionalCall', _6 => _6(error , input)]);
709
+ _optionalChain$2([options, 'optionalAccess', _5 => _5.onError, 'optionalCall', _6 => _6(error , input)]);
384
710
  return defaultValue;
385
711
  }
386
712
  }
387
713
 
388
- function _optionalChain$2(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
714
+ function _optionalChain$1(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
389
715
 
390
716
 
391
717
  /**
@@ -401,7 +727,7 @@ const parseConfigContent = (content) => {
401
727
  return config ;
402
728
  } catch (error) {
403
729
  // TOML 解析失败,继续尝试其他格式
404
- // eslint-disable-next-line no-console
730
+
405
731
  console.debug('TOML parse failed:', error);
406
732
  }
407
733
 
@@ -413,7 +739,7 @@ const parseConfigContent = (content) => {
413
739
  }
414
740
  } catch (error) {
415
741
  // YAML 解析失败,继续尝试其他格式
416
- // eslint-disable-next-line no-console
742
+
417
743
  console.debug('YAML parse failed:', error);
418
744
  }
419
745
 
@@ -475,13 +801,13 @@ const getCommandConfig = (
475
801
  // 根据命令名称映射到配置路径
476
802
  switch (commandName) {
477
803
  case 'dev':
478
- commandConfig = _optionalChain$2([config, 'access', _ => _.dev, 'optionalAccess', _2 => _2.run]);
804
+ commandConfig = _optionalChain$1([config, 'access', _ => _.dev, 'optionalAccess', _2 => _2.run]);
479
805
  break;
480
806
  case 'build':
481
- commandConfig = _optionalChain$2([config, 'access', _3 => _3.deploy, 'optionalAccess', _4 => _4.build]);
807
+ commandConfig = _optionalChain$1([config, 'access', _3 => _3.deploy, 'optionalAccess', _4 => _4.build]);
482
808
  break;
483
809
  case 'start':
484
- commandConfig = _optionalChain$2([config, 'access', _5 => _5.deploy, 'optionalAccess', _6 => _6.run]);
810
+ commandConfig = _optionalChain$1([config, 'access', _5 => _5.deploy, 'optionalAccess', _6 => _6.run]);
485
811
  break;
486
812
  default:
487
813
  throw new Error(`Unknown command: ${commandName}`);
@@ -497,27 +823,276 @@ const getCommandConfig = (
497
823
  return commandConfig;
498
824
  };
499
825
 
500
- function _nullishCoalesce$1(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain$1(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
826
+ // ABOUTME: Fix rule to comment out problematic outputFileTracingRoot config in Next.js projects
827
+ // ABOUTME: This config can cause issues in monorepo environments and should be removed
828
+
829
+
830
+
831
+
832
+ /**
833
+ * 检查是否为 Next.js 项目
834
+ */
835
+ const isNextProject = (projectFolder) => {
836
+ const packageJsonPath = path.join(projectFolder, 'package.json');
837
+
838
+ if (!fs.existsSync(packageJsonPath)) {
839
+ return false;
840
+ }
841
+
842
+ try {
843
+ // eslint-disable-next-line no-restricted-syntax
844
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
845
+ const deps = {
846
+ ...packageJson.dependencies,
847
+ ...packageJson.devDependencies,
848
+ };
849
+
850
+ return 'next' in deps;
851
+ } catch (e) {
852
+ return false;
853
+ }
854
+ };
501
855
 
502
856
  /**
503
- * 创建日志管理器
857
+ * 查找 Next.js 配置文件
504
858
  */
505
- const createLogManager = (logDir = '.coze-logs') => {
506
- const ensureLogDir = () => {
507
- if (!fs.existsSync(logDir)) {
508
- fs.mkdirSync(logDir, { recursive: true });
859
+ const findNextConfigFile = (projectFolder) => {
860
+ const possibleConfigs = [
861
+ 'next.config.ts',
862
+ 'next.config.js',
863
+ 'next.config.mjs',
864
+ ];
865
+
866
+ for (const configFile of possibleConfigs) {
867
+ const configPath = path.join(projectFolder, configFile);
868
+ if (fs.existsSync(configPath)) {
869
+ return configPath;
509
870
  }
510
- };
871
+ }
872
+
873
+ return null;
874
+ };
875
+
876
+ /**
877
+ * 注释掉 outputFileTracingRoot 配置
878
+ */
879
+ const commentOutOutputTracingRoot = (
880
+ configPath,
881
+ ) => {
882
+ let content = fs.readFileSync(configPath, 'utf-8');
883
+ let modified = false;
884
+ let originalLine = null;
885
+
886
+ // 匹配包含 outputFileTracingRoot 的行(尚未被注释的)
887
+ // 支持 path.resolve(...) 后面有空格,然后可选逗号
888
+ // 只匹配单行配置,不匹配跨多行的配置
889
+ const pattern =
890
+ /^(\s*)(outputFileTracingRoot:\s*path\.resolve\([^\n\r)]+\)\s*,?)\s*$/gm;
891
+
892
+ const matches = content.match(pattern);
893
+
894
+ if (matches && matches.length > 0) {
895
+ originalLine = matches[0].trim();
896
+
897
+ // 在匹配的行前添加 // 注释
898
+ content = content.replace(pattern, '$1// $2');
899
+ modified = true;
900
+
901
+ fs.writeFileSync(configPath, content, 'utf-8');
902
+ }
903
+
904
+ return { modified, originalLine };
905
+ };
906
+
907
+ // start_aigc
908
+ /**
909
+ * Fix 规则:注释掉 Next.js 项目中的 outputFileTracingRoot 配置
910
+ * 这个配置在 monorepo 环境中可能会导致问题
911
+ */
912
+ const fixNextOutputTracingRoot = context => {
913
+ const ruleName = 'next-output-tracing-root';
914
+
915
+ // 1. 检查是否为 Next.js 项目
916
+ if (!isNextProject(context.projectFolder)) {
917
+ return {
918
+ ruleName,
919
+ applied: false,
920
+ message: 'Not a Next.js project, skipping',
921
+ };
922
+ }
923
+
924
+ // 2. 查找 Next.js 配置文件
925
+ const configPath = findNextConfigFile(context.projectFolder);
926
+
927
+ if (!configPath) {
928
+ return {
929
+ ruleName,
930
+ applied: false,
931
+ message: 'Next.js config file not found, skipping',
932
+ };
933
+ }
511
934
 
512
- const getLogPath = (logFile) => path.join(logDir, logFile);
935
+ // 3. 注释掉 outputFileTracingRoot 配置
936
+ const { modified, originalLine } = commentOutOutputTracingRoot(configPath);
937
+
938
+ if (modified && originalLine) {
939
+ logger.success(
940
+ `Commented out outputFileTracingRoot in ${configPath.split('/').pop()}`,
941
+ );
942
+ logger.info(` Original: ${originalLine}`);
943
+
944
+ return {
945
+ ruleName,
946
+ applied: true,
947
+ message: `Successfully commented out: ${originalLine}`,
948
+ };
949
+ }
513
950
 
514
951
  return {
515
- createWriteStream: (logFile) => {
516
- ensureLogDir();
517
- return fs.createWriteStream(getLogPath(logFile), { flags: 'a' });
518
- },
952
+ ruleName,
953
+ applied: false,
954
+ message: 'No outputFileTracingRoot config found, skipping',
519
955
  };
520
956
  };
957
+ // end_aigc
958
+
959
+ /**
960
+ * 所有修复规则的数组
961
+ * 按顺序执行,新增规则直接添加到数组中
962
+ */
963
+ const rules = [
964
+ // Next.js related fixes
965
+ fixNextOutputTracingRoot,
966
+
967
+ // Add more rules here
968
+ ] ;
969
+
970
+ // ABOUTME: Fix command for resolving legacy issues from previous project versions
971
+ // ABOUTME: Applies a series of fix rules defined in the fix-rules directory
972
+
973
+
974
+ // start_aigc
975
+ /**
976
+ * 执行 fix 命令的内部实现
977
+ */
978
+ const executeFix = async (options = {}) => {
979
+ try {
980
+ const cwd = process.cwd();
981
+ const projectFolder = options.directory
982
+ ? path.resolve(cwd, options.directory)
983
+ : cwd;
984
+
985
+ logger.info(`Running fix command on: ${projectFolder}`);
986
+ logger.info(`Found ${rules.length} fix rule(s) to apply\n`);
987
+
988
+ const context = {
989
+ cwd,
990
+ projectFolder,
991
+ };
992
+
993
+ let appliedCount = 0;
994
+ let skippedCount = 0;
995
+
996
+ // 依次执行所有修复规则
997
+ for (const rule of rules) {
998
+ try {
999
+ const result = await Promise.resolve(rule(context));
1000
+
1001
+ if (result.applied) {
1002
+ appliedCount++;
1003
+ logger.success(`✓ ${result.ruleName}: ${result.message}`);
1004
+ } else {
1005
+ skippedCount++;
1006
+ logger.info(`○ ${result.ruleName}: ${result.message}`);
1007
+ }
1008
+ } catch (error) {
1009
+ logger.error(
1010
+ `✗ Rule execution failed: ${error instanceof Error ? error.message : String(error)}`,
1011
+ );
1012
+ }
1013
+ }
1014
+
1015
+ // 输出汇总信息
1016
+ logger.info(
1017
+ `\nSummary: ${appliedCount} fixed, ${skippedCount} skipped, ${rules.length} total`,
1018
+ );
1019
+
1020
+ if (appliedCount > 0) {
1021
+ logger.success('\nFixes applied successfully!');
1022
+ } else {
1023
+ logger.info('\nNo fixes needed');
1024
+ }
1025
+ } catch (error) {
1026
+ logger.error('Failed to run fix command:');
1027
+ logger.error(error instanceof Error ? error.message : String(error));
1028
+ process.exit(1);
1029
+ }
1030
+ };
1031
+ // end_aigc
1032
+
1033
+ /**
1034
+ * 注册 fix 命令到 program
1035
+ */
1036
+ const registerCommand$3 = program => {
1037
+ program
1038
+ .command('fix')
1039
+ .description(
1040
+ 'Fix legacy issues from previous versions (e.g., problematic configs)',
1041
+ )
1042
+ .argument(
1043
+ '[directory]',
1044
+ 'Target directory to fix (defaults to current directory)',
1045
+ )
1046
+ .action(async (directory) => {
1047
+ await executeFix({ directory });
1048
+ });
1049
+ };
1050
+
1051
+ function _nullishCoalesce$1(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
1052
+ /**
1053
+ * 日志文件名常量
1054
+ */
1055
+ const LOG_FILE_NAME$1 = 'dev.log';
1056
+
1057
+ /**
1058
+ * 获取日志目录
1059
+ * 优先使用环境变量 COZE_LOG_DIR,否则使用 ~/.coze-logs
1060
+ */
1061
+ const getLogDir$1 = () =>
1062
+ process.env.COZE_LOG_DIR || path.join(os.homedir(), '.coze-logs');
1063
+
1064
+ /**
1065
+ * 解析日志文件路径
1066
+ * - 如果是绝对路径,直接使用
1067
+ * - 如果是相对路径,基于 getLogDir() + 相对路径
1068
+ * - 如果为空,使用 getLogDir() + LOG_FILE_NAME
1069
+ */
1070
+ const resolveLogFilePath$1 = (logFile) => {
1071
+ if (!logFile) {
1072
+ return path.join(getLogDir$1(), LOG_FILE_NAME$1);
1073
+ }
1074
+
1075
+ if (path.isAbsolute(logFile)) {
1076
+ return logFile;
1077
+ }
1078
+
1079
+ return path.join(getLogDir$1(), logFile);
1080
+ };
1081
+
1082
+ /**
1083
+ * 创建日志写入流
1084
+ */
1085
+ const createLogStream$1 = (logFilePath) => {
1086
+ const logDir = path.dirname(logFilePath);
1087
+
1088
+ // 确保日志目录存在
1089
+ if (!fs.existsSync(logDir)) {
1090
+ fs.mkdirSync(logDir, { recursive: true });
1091
+ }
1092
+
1093
+ // 使用 'w' 标志覆盖之前的日志
1094
+ return fs.createWriteStream(logFilePath, { flags: 'w' });
1095
+ };
521
1096
 
522
1097
  /**
523
1098
  * 执行命令的内部实现
@@ -529,21 +1104,32 @@ const executeRun = async (
529
1104
  try {
530
1105
  logger.info(`Running ${commandName} command...`);
531
1106
 
532
- // 1. 加载 .coze 配置
1107
+ // 1. 对于 build 命令,先执行 fix 以确保项目配置正确
1108
+ if (['dev', 'build'].includes(commandName)) {
1109
+ logger.info('\n🔧 Running fix command before build...\n');
1110
+ try {
1111
+ await executeFix();
1112
+ // eslint-disable-next-line @coze-arch/no-empty-catch
1113
+ } catch (e) {
1114
+ // just ignore
1115
+ }
1116
+ logger.info('');
1117
+ }
1118
+
1119
+ // 2. 加载 .coze 配置
533
1120
  const config = await loadCozeConfig();
534
1121
  const commandArgs = getCommandConfig(config, commandName);
535
1122
 
536
- // 2. 准备日志
537
- const logManager = createLogManager();
538
- const logFile = options.logFile || `${commandName}.log`;
539
- const logStream = logManager.createWriteStream(logFile);
1123
+ // 3. 准备日志
1124
+ const logFilePath = resolveLogFilePath$1(options.logFile);
1125
+ const logStream = createLogStream$1(logFilePath);
540
1126
 
541
- // 3. 执行命令
1127
+ // 4. 执行命令
542
1128
  const commandString = commandArgs.join(' ');
543
1129
 
544
1130
  logger.info(`Executing: ${commandString}`);
545
1131
  logger.info(`Working directory: ${process.cwd()}`);
546
- logger.info(`Log file: ${logFile}`);
1132
+ logger.info(`Log file: ${logFilePath}`);
547
1133
 
548
1134
  const childProcess = shelljs.exec(commandString, {
549
1135
  async: true,
@@ -555,12 +1141,12 @@ const executeRun = async (
555
1141
  }
556
1142
 
557
1143
  // 将输出同时写入控制台和日志文件
558
- _optionalChain$1([childProcess, 'access', _ => _.stdout, 'optionalAccess', _2 => _2.on, 'call', _3 => _3('data', (data) => {
1144
+ _optionalChain([childProcess, 'access', _ => _.stdout, 'optionalAccess', _2 => _2.on, 'call', _3 => _3('data', (data) => {
559
1145
  process.stdout.write(data);
560
1146
  logStream.write(data);
561
1147
  })]);
562
1148
 
563
- _optionalChain$1([childProcess, 'access', _4 => _4.stderr, 'optionalAccess', _5 => _5.on, 'call', _6 => _6('data', (data) => {
1149
+ _optionalChain([childProcess, 'access', _4 => _4.stderr, 'optionalAccess', _5 => _5.on, 'call', _6 => _6('data', (data) => {
564
1150
  process.stderr.write(data);
565
1151
  logStream.write(data);
566
1152
  })]);
@@ -572,11 +1158,11 @@ const executeRun = async (
572
1158
  logger.error(
573
1159
  `Command exited with code ${_nullishCoalesce$1(code, () => ( 'unknown'))}${signal ? ` and signal ${signal}` : ''}`,
574
1160
  );
575
- logger.error(`Check log file for details: ${logFile}`);
1161
+ logger.error(`Check log file for details: ${logFilePath}`);
576
1162
  process.exit(code || 1);
577
1163
  } else {
578
1164
  logger.success('Command completed successfully');
579
- logger.info(`Log file: ${logFile}`);
1165
+ logger.info(`Log file: ${logFilePath}`);
580
1166
  }
581
1167
  });
582
1168
 
@@ -599,7 +1185,7 @@ const executeRun = async (
599
1185
  /**
600
1186
  * 注册 dev/build/start 命令到 program
601
1187
  */
602
- const registerCommand$1 = program => {
1188
+ const registerCommand$2 = program => {
603
1189
  // dev 命令
604
1190
  program
605
1191
  .command('dev')
@@ -629,68 +1215,44 @@ const registerCommand$1 = program => {
629
1215
  };
630
1216
 
631
1217
  /**
632
- * 时间统计工具
1218
+ * 在后台启动一个独立的子进程
1219
+ * 类似于 `setsid command args >/dev/null 2>&1 &`
1220
+ *
1221
+ * @param command - 要执行的命令 (例如: 'npm', 'node', 'bash')
1222
+ * @param args - 命令参数数组 (例如: ['run', 'dev'])
1223
+ * @param options - 配置选项
1224
+ * @returns 子进程的 PID
633
1225
  */
634
- class TimeTracker {
635
-
636
-
1226
+ function spawnDetached(
1227
+ command,
1228
+ args,
1229
+ options,
1230
+ ) {
1231
+ const { cwd, verbose = true } = options;
1232
+ const isWindows = os.platform() === 'win32';
637
1233
 
638
- constructor() {
639
- this.startTime = perf_hooks.performance.now();
640
- this.lastTime = this.startTime;
1234
+ if (verbose) {
1235
+ console.log(`Spawning detached process: ${command} ${args.join(' ')}`);
1236
+ console.log(`Working directory: ${cwd}`);
641
1237
  }
642
1238
 
643
- /**
644
- * 记录阶段耗时
645
- * @param phaseName 阶段名称
646
- */
647
- logPhase(phaseName) {
648
- const now = perf_hooks.performance.now();
649
- const phaseTime = now - this.lastTime;
650
- this.lastTime = now;
1239
+ // 使用 spawn 创建后台子进程
1240
+ const child = child_process.spawn(command, args, {
1241
+ cwd,
1242
+ detached: !isWindows, // Windows 不完全支持 detached,但仍可以使用
1243
+ stdio: 'ignore', // 忽略所有输入输出,让进程完全独立运行
1244
+ });
651
1245
 
652
- logger.verbose(`⏱ ${phaseName}: ${phaseTime.toFixed(2)}ms`);
653
- }
1246
+ // 分离父子进程引用,允许父进程退出而不等待子进程
1247
+ child.unref();
654
1248
 
655
- /**
656
- * 记录总耗时
657
- */
658
- logTotal() {
659
- const totalTime = perf_hooks.performance.now() - this.startTime;
660
- logger.verbose(`⏱ Total time: ${totalTime.toFixed(2)}ms`);
1249
+ if (verbose && child.pid) {
1250
+ console.log(`Process started with PID: ${child.pid}`);
661
1251
  }
662
1252
 
663
- /**
664
- * 获取当前耗时(不输出日志)
665
- * @returns 从开始到现在的总耗时(毫秒)
666
- */
667
- getElapsedTime() {
668
- return perf_hooks.performance.now() - this.startTime;
669
- }
1253
+ return child.pid;
670
1254
  }
671
1255
 
672
- /**
673
- * 获取模板配置文件路径
674
- * @returns templates.json 的绝对路径
675
- */
676
- const getTemplatesConfigPath = () => {
677
- const configPath = path.resolve(getTemplatesDir(), 'templates.json');
678
- logger.verbose(`Templates config path: ${configPath}`);
679
- logger.verbose(`Config file exists: ${fs.existsSync(configPath)}`);
680
- return configPath;
681
- };
682
-
683
- /**
684
- * 获取模板目录路径
685
- * @returns __templates__ 目录的绝对路径
686
- */
687
- const getTemplatesDir = () => {
688
- const templatesDir = path.resolve(__dirname, './__templates__');
689
- logger.verbose(`Templates directory: ${templatesDir}`);
690
- logger.verbose(`Templates directory exists: ${fs.existsSync(templatesDir)}`);
691
- return templatesDir;
692
- };
693
-
694
1256
  /**
695
1257
  * 创建 AJV 验证器实例
696
1258
  */
@@ -739,149 +1301,12 @@ const validateParams = (
739
1301
  return params ;
740
1302
  };
741
1303
 
742
- /**
743
- * 加载模板配置文件
744
- * 支持 .ts 和 .js 文件(通过 sucrase 注册)
745
- *
746
- * @param templatePath - 模板目录路径
747
- * @returns 模板配置对象
748
- */
749
-
750
- const loadTemplateConfig = async (
751
- templatePath,
752
- ) => {
753
- logger.verbose(`Loading template config from: ${templatePath}`);
754
-
755
- const tsConfigPath = path.join(templatePath, 'template.config.ts');
756
- const jsConfigPath = path.join(templatePath, 'template.config.js');
757
-
758
- logger.verbose('Checking for config files:');
759
- logger.verbose(` - TypeScript: ${tsConfigPath}`);
760
- logger.verbose(` - JavaScript: ${jsConfigPath}`);
761
-
762
- let configPath;
763
-
764
- const [tsExists, jsExists] = await Promise.all([
765
- fs$1.access(tsConfigPath).then(
766
- () => true,
767
- () => false,
768
- ),
769
- fs$1.access(jsConfigPath).then(
770
- () => true,
771
- () => false,
772
- ),
773
- ]);
774
-
775
- logger.verbose('Config file existence check:');
776
- logger.verbose(` - template.config.ts: ${tsExists}`);
777
- logger.verbose(` - template.config.js: ${jsExists}`);
778
-
779
- if (tsExists) {
780
- configPath = tsConfigPath;
781
- } else if (jsExists) {
782
- configPath = jsConfigPath;
783
- } else {
784
- throw new Error(
785
- `Template config not found in ${templatePath}.\n` +
786
- 'Expected: template.config.ts or template.config.js',
787
- );
788
- }
789
-
790
- logger.verbose(`Using config file: ${configPath}`);
791
-
792
- // eslint-disable-next-line @typescript-eslint/no-require-imports, security/detect-non-literal-require -- Sucrase handles .ts files at runtime, path is validated above
793
- const config = require(configPath);
794
-
795
- logger.verbose('Template config loaded successfully');
796
-
797
- return config.default || config;
798
- };
799
-
800
- /**
801
- * 加载模板列表配置
802
- *
803
- * @param configPath - templates.json 配置文件路径
804
- * @returns 模板列表配置
805
- */
806
- const loadTemplatesConfig = async (
807
- configPath,
808
- ) => {
809
- logger.verbose(`Loading templates config from: ${configPath}`);
810
-
811
- const content = await fs$1.readFile(configPath, 'utf-8');
812
- // eslint-disable-next-line no-restricted-syntax -- Static config file loaded at build time, safeJsonParse not needed
813
- const config = JSON.parse(content) ;
814
-
815
- logger.verbose(
816
- `Found ${config.templates.length} templates: ${config.templates.map(t => t.name).join(', ')}`,
817
- );
818
-
819
- return config;
820
- };
821
-
822
- /**
823
- * 根据模板名称查找模板元信息
824
- *
825
- * @param templatesConfig - 模板列表配置
826
- * @param templateName - 模板名称
827
- * @returns 模板元信息
828
- */
829
- const findTemplate = (
830
- templatesConfig,
831
- templateName,
832
- ) => {
833
- const template = templatesConfig.templates.find(t => t.name === templateName);
834
-
835
- if (!template) {
836
- const availableTemplates = templatesConfig.templates
837
- .map(t => t.name)
838
- .join(', ');
839
- throw new Error(
840
- `Template "${templateName}" not found.\n` +
841
- `Available templates: ${availableTemplates}\n` +
842
- 'Use --template <name> to specify a template.',
843
- );
844
- }
845
-
846
- return template;
847
- };
848
-
849
- /**
850
- * 获取模板的完整路径
851
- *
852
- * @param basePath - 模板目录(templates.json 所在目录)
853
- * @param templateMetadata - 模板元信息
854
- * @returns 模板完整路径
855
- */
856
- const getTemplatePath = async (
857
- basePath,
858
- templateMetadata,
859
- ) => {
860
- logger.verbose('Resolving template path:');
861
- logger.verbose(` - Base path: ${basePath}`);
862
- logger.verbose(` - Template location: ${templateMetadata.location}`);
863
-
864
- // location 是相对于 templates.json 文件的路径
865
- const templatePath = path.join(basePath, templateMetadata.location);
866
-
867
- logger.verbose(` - Resolved path: ${templatePath}`);
868
-
869
- try {
870
- await fs$1.access(templatePath);
871
- logger.verbose(' - Template directory exists: ✓');
872
- // eslint-disable-next-line @coze-arch/use-error-in-catch -- Error handling done in throw statement
873
- } catch (e) {
874
- logger.error(' - Template directory does not exist: ✗');
875
- throw new Error(`Template directory not found: ${templatePath}`);
876
- }
877
-
878
- return templatePath;
879
- };
880
-
881
1304
  /**
882
1305
  * 从 Commander 解析透传参数
883
1306
  * 将 kebab-case 的 CLI 参数转换为 camelCase
884
1307
  *
1308
+ * 使用 minimist 解析 process.argv,自动处理类型转换
1309
+ *
885
1310
  * @param command - Commander 命令实例
886
1311
  * @param knownOptions - 已知的选项集合(不需要透传的选项)
887
1312
  * @returns 参数对象
@@ -890,20 +1315,34 @@ const parsePassThroughParams = (
890
1315
  command,
891
1316
  knownOptions = new Set(),
892
1317
  ) => {
893
- const rawOptions = command.opts();
1318
+ // 使用 minimist 解析所有参数
1319
+ // eslint-disable-next-line @typescript-eslint/no-magic-numbers -- slice(2) to skip node and script path
1320
+ const parsed = minimist(process.argv.slice(2));
894
1321
 
895
- const initial = {};
896
- return Object.entries(rawOptions).reduce(
1322
+ // 过滤掉已知选项和位置参数(_)
1323
+ const filtered = Object.entries(parsed).reduce(
897
1324
  (params, [key, value]) => {
898
- if (knownOptions.has(key)) {
1325
+ // 跳过 minimist 的位置参数数组
1326
+ if (key === '_') {
1327
+ return params;
1328
+ }
1329
+
1330
+ // 跳过已知选项(支持原始格式和 camelCase 格式)
1331
+ if (knownOptions.has(key) || knownOptions.has(changeCase.camelCase(key))) {
899
1332
  return params;
900
1333
  }
901
1334
 
1335
+ // 将 kebab-case 转换为 camelCase
902
1336
  const camelKey = changeCase.camelCase(key);
903
- return { ...params, [camelKey]: value };
1337
+ // eslint-disable-next-line security/detect-object-injection -- camelKey is sanitized by camelCase
1338
+ params[camelKey] = value;
1339
+
1340
+ return params;
904
1341
  },
905
- initial,
1342
+ {},
906
1343
  );
1344
+
1345
+ return filtered;
907
1346
  };
908
1347
 
909
1348
  /**
@@ -998,6 +1437,11 @@ const shouldIgnoreFile = (filePath) => {
998
1437
  return directoryPatterns.some(dir => pathParts.includes(dir));
999
1438
  };
1000
1439
 
1440
+ // ABOUTME: File system utilities for template file processing
1441
+ // ABOUTME: Provides directory scanning, file path conversion, and node_modules copying
1442
+
1443
+
1444
+ // start_aigc
1001
1445
  /**
1002
1446
  * 递归获取目录中的所有文件
1003
1447
  *
@@ -1030,61 +1474,395 @@ const getAllFiles = async (
1030
1474
  return getAllFiles(fullPath, baseDir);
1031
1475
  }
1032
1476
 
1033
- logger.verbose(` - Found file: ${entry.name}`);
1034
- return [relativePath];
1035
- }),
1036
- );
1477
+ logger.verbose(` - Found file: ${entry.name}`);
1478
+ return [relativePath];
1479
+ }),
1480
+ );
1481
+
1482
+ return results.flat();
1483
+ };
1484
+
1485
+ /**
1486
+ * 确保目录存在
1487
+ *
1488
+ * @param dir - 目录路径
1489
+ */
1490
+ const ensureDir = async (dir) => {
1491
+ await fs$1.mkdir(dir, { recursive: true });
1492
+ };
1493
+
1494
+ /**
1495
+ * 转换模板文件名(白名单机制)
1496
+ * 只对特定文件将 _ 开头转换为 . 开头
1497
+ *
1498
+ * @param filePath - 文件相对路径
1499
+ * @returns 转换后的文件路径
1500
+ */
1501
+ const convertDotfileName = (filePath) => {
1502
+ // 白名单:需要从 _ 开头转换为 . 开头的文件
1503
+ const dotfileWhitelist = ['_gitignore', '_npmrc'];
1504
+
1505
+ const fileName = path.basename(filePath);
1506
+
1507
+ // 只对白名单中的文件进行转换(如 _gitignore -> .gitignore)
1508
+ if (dotfileWhitelist.includes(fileName)) {
1509
+ return filePath.replace(/(^|\/|\\)_([^/\\]+)$/g, '$1.$2');
1510
+ }
1511
+
1512
+ return filePath;
1513
+ };
1514
+
1515
+ /**
1516
+ * 复制 node_modules 目录(如果存在)
1517
+ *
1518
+ * @param sourceNodeModules - 源 node_modules 路径
1519
+ * @param targetNodeModules - 目标 node_modules 路径
1520
+ */
1521
+ const copyNodeModules = (
1522
+ sourceNodeModules,
1523
+ targetNodeModules,
1524
+ ) => {
1525
+ if (!fs.existsSync(sourceNodeModules)) {
1526
+ return;
1527
+ }
1528
+
1529
+ logger.info('\nCopying node_modules from pre-warmed template...');
1530
+ logger.verbose(` From: ${sourceNodeModules}`);
1531
+ logger.verbose(` To: ${targetNodeModules}`);
1532
+
1533
+ const result = shelljs.exec(`cp -R "${sourceNodeModules}" "${targetNodeModules}"`, {
1534
+ silent: true,
1535
+ });
1536
+
1537
+ if (result.stdout) {
1538
+ process.stdout.write(result.stdout);
1539
+ }
1540
+
1541
+ if (result.stderr) {
1542
+ process.stderr.write(result.stderr);
1543
+ }
1544
+
1545
+ if (result.code === 0) {
1546
+ logger.success('✓ node_modules copied successfully');
1547
+ } else {
1548
+ logger.warn(
1549
+ `Failed to copy node_modules: ${result.stderr || 'unknown error'}`,
1550
+ );
1551
+ logger.info('Will need to run pnpm install manually');
1552
+ }
1553
+ };
1554
+ // end_aigc
1555
+
1556
+ // ABOUTME: File rendering utilities for template processing
1557
+ // ABOUTME: Handles file content rendering, hook execution, and file writing
1558
+
1559
+
1560
+
1561
+
1562
+
1563
+
1564
+
1565
+
1566
+ // start_aigc
1567
+ /**
1568
+ * 执行文件渲染钩子
1569
+ *
1570
+ * @param templateConfig - 模板配置
1571
+ * @param fileInfo - 文件渲染信息
1572
+ * @param context - 模板上下文
1573
+ * @returns 处理后的文件信息,或 null 表示跳过该文件
1574
+ */
1575
+ const executeFileRenderHook = async (
1576
+ templateConfig,
1577
+ fileInfo,
1578
+ context,
1579
+ ) => {
1580
+ if (!templateConfig.onFileRender) {
1581
+ return fileInfo;
1582
+ }
1583
+
1584
+ const result = await templateConfig.onFileRender(
1585
+ fileInfo,
1586
+ context,
1587
+ );
1588
+
1589
+ // false: 跳过文件
1590
+ if (result === false) {
1591
+ return null;
1592
+ }
1593
+
1594
+ // undefined/void: 使用默认内容
1595
+ if (result === undefined || result === null) {
1596
+ return fileInfo;
1597
+ }
1598
+
1599
+ // string: 作为 content,其他不变
1600
+ if (typeof result === 'string') {
1601
+ return {
1602
+ ...fileInfo,
1603
+ content: result,
1604
+ };
1605
+ }
1606
+
1607
+ // FileRenderInfo: 使用新对象的信息
1608
+ return result;
1609
+ };
1610
+
1611
+ /**
1612
+ * 准备单个文件的渲染信息(不实际写入)
1613
+ * 用于 dry-run 阶段,收集所有将要写入的文件信息
1614
+ *
1615
+ * @param options - 准备选项
1616
+ * @returns 文件渲染信息,或 null 表示该文件被跳过
1617
+ */
1618
+ const prepareFileInfo = async (options
1619
+
1620
+
1621
+
1622
+
1623
+ ) => {
1624
+ const { file, templatePath, context, templateConfig } = options;
1625
+
1626
+ const srcPath = path.join(templatePath, file);
1627
+ const destFile = convertDotfileName(file);
1628
+
1629
+ logger.verbose(
1630
+ ` - Preparing: ${file}${destFile !== file ? ` -> ${destFile}` : ''}`,
1631
+ );
1632
+
1633
+ // 判断是否为二进制文件
1634
+ const isBinary = !shouldRenderFile(srcPath);
1635
+ let content;
1636
+ let wasRendered = false;
1637
+
1638
+ if (isBinary) {
1639
+ // 二进制文件,读取为 buffer 然后转为 base64
1640
+ const buffer = await fs$1.readFile(srcPath);
1641
+ content = buffer.toString('base64');
1642
+ } else {
1643
+ // 文本文件,渲染后的内容
1644
+ content = await renderTemplate(srcPath, context);
1645
+ wasRendered = true;
1646
+ }
1647
+
1648
+ // 构造文件信息对象
1649
+ const fileInfo = {
1650
+ path: file,
1651
+ destPath: destFile,
1652
+ content,
1653
+ isBinary,
1654
+ wasRendered,
1655
+ };
1656
+
1657
+ // 执行文件渲染钩子
1658
+ const processedFileInfo = await executeFileRenderHook(
1659
+ templateConfig,
1660
+ fileInfo,
1661
+ context,
1662
+ );
1663
+
1664
+ // 如果返回 null,表示该文件被 hook 跳过
1665
+ if (processedFileInfo === null) {
1666
+ logger.verbose(' ⊘ Skipped by onFileRender hook');
1667
+ return null;
1668
+ }
1669
+
1670
+ return processedFileInfo;
1671
+ };
1672
+
1673
+ /**
1674
+ * 写入渲染后的文件到目标路径
1675
+ *
1676
+ * @param options - 写入选项
1677
+ */
1678
+ const writeRenderedFile = async (options
1679
+
1680
+
1681
+
1682
+ ) => {
1683
+ const { fileInfo, srcPath, destPath } = options;
1684
+
1685
+ logger.verbose(` - Writing: ${fileInfo.destPath}`);
1686
+
1687
+ // 确保目标目录存在
1688
+ await ensureDir(path.dirname(destPath));
1689
+
1690
+ // 写入文件
1691
+ if (fileInfo.isBinary) {
1692
+ // 二进制文件:如果内容是原始 base64(未被 hook 修改),直接复制;否则从 base64 解码写入
1693
+ const buffer = await fs$1.readFile(srcPath);
1694
+ const originalContent = buffer.toString('base64');
1695
+
1696
+ if (fileInfo.content === originalContent) {
1697
+ await fs$1.copyFile(srcPath, destPath);
1698
+ logger.verbose(' ✓ Copied (binary)');
1699
+ } else {
1700
+ const modifiedBuffer = Buffer.from(fileInfo.content, 'base64');
1701
+ await fs$1.writeFile(destPath, modifiedBuffer);
1702
+ logger.verbose(' ✓ Written (binary, modified by hook)');
1703
+ }
1704
+ } else {
1705
+ // 文本文件
1706
+ await fs$1.writeFile(destPath, fileInfo.content, 'utf-8');
1707
+ logger.verbose(' ✓ Rendered and written');
1708
+ }
1709
+ };
1710
+ // end_aigc
1711
+
1712
+ // ABOUTME: File conflict detection utilities for template processing
1713
+ // ABOUTME: Provides dry-run file collection and conflict checking
1714
+
1715
+
1716
+
1717
+
1718
+
1719
+
1720
+
1721
+ // start_aigc
1722
+ /**
1723
+ * 收集所有将要写入的文件路径(dry-run 阶段)
1724
+ *
1725
+ * @param options - 收集选项
1726
+ * @returns 将要写入的文件路径列表
1727
+ */
1728
+ const collectFilesToRender = async (options
1729
+
1730
+
1731
+
1732
+
1733
+ ) => {
1734
+ const { files, templatePath, context, templateConfig } = options;
1735
+
1736
+ logger.verbose('\nDry-run: Collecting files to render...');
1737
+
1738
+ const fileInfos = await Promise.all(
1739
+ files.map(file =>
1740
+ prepareFileInfo({
1741
+ file,
1742
+ templatePath,
1743
+ context,
1744
+ templateConfig,
1745
+ }),
1746
+ ),
1747
+ );
1748
+
1749
+ // 过滤掉被 hook 跳过的文件,收集 destPath
1750
+ const filesToWrite = fileInfos
1751
+ .filter((info) => info !== null)
1752
+ .map(info => info.destPath);
1753
+
1754
+ logger.verbose(` - ${filesToWrite.length} files will be written`);
1755
+ logger.verbose(
1756
+ ` - ${fileInfos.length - filesToWrite.length} files skipped by hooks`,
1757
+ );
1758
+
1759
+ return filesToWrite;
1760
+ };
1761
+
1762
+ /**
1763
+ * 检测文件冲突
1764
+ *
1765
+ * @param outputPath - 输出目录路径
1766
+ * @param filesToWrite - 将要写入的文件路径列表
1767
+ * @returns 冲突的文件路径列表
1768
+ */
1769
+ const detectFileConflicts = (
1770
+ outputPath,
1771
+ filesToWrite,
1772
+ ) => {
1773
+ logger.verbose('\nChecking for file conflicts...');
1774
+
1775
+ const conflicts = [];
1776
+
1777
+ for (const file of filesToWrite) {
1778
+ const fullPath = path.join(outputPath, file);
1779
+ if (fs.existsSync(fullPath)) {
1780
+ conflicts.push(file);
1781
+ logger.verbose(` ⚠ Conflict detected: ${file}`);
1782
+ }
1783
+ }
1784
+
1785
+ if (conflicts.length === 0) {
1786
+ logger.verbose(' ✓ No conflicts detected');
1787
+ } else {
1788
+ logger.verbose(` ⚠ ${conflicts.length} conflicts detected`);
1789
+ }
1037
1790
 
1038
- return results.flat();
1791
+ return conflicts;
1039
1792
  };
1793
+ // end_aigc
1794
+
1795
+ // ABOUTME: Main file processing orchestration for template rendering
1796
+ // ABOUTME: Coordinates file system, rendering, and conflict detection layers
1040
1797
 
1041
- /**
1042
- * 确保目录存在
1043
- *
1044
- * @param dir - 目录路径
1045
- */
1046
- const ensureDir = async (dir) => {
1047
- await fs$1.mkdir(dir, { recursive: true });
1048
- };
1049
1798
 
1799
+
1800
+ // start_aigc
1050
1801
  /**
1051
- * 转换模板文件名(白名单机制)
1052
- * 只对特定文件将 _ 开头转换为 . 开头
1802
+ * 处理单个文件(准备 + 写入)
1053
1803
  *
1054
- * @param filePath - 文件相对路径
1055
- * @returns 转换后的文件路径
1804
+ * @param options - 处理选项
1056
1805
  */
1057
- const convertDotfileName = (filePath) => {
1058
- // 白名单:需要从 _ 开头转换为 . 开头的文件
1059
- const dotfileWhitelist = ['_gitignore', '_npmrc'];
1806
+ const processSingleFile = async (options
1060
1807
 
1061
- const fileName = path.basename(filePath);
1062
1808
 
1063
- // 只对白名单中的文件进行转换(如 _gitignore -> .gitignore)
1064
- if (dotfileWhitelist.includes(fileName)) {
1065
- return filePath.replace(/(^|\/|\\)_([^/\\]+)$/g, '$1.$2');
1809
+
1810
+
1811
+
1812
+ ) => {
1813
+ const { file, templatePath, outputPath, context, templateConfig } = options;
1814
+
1815
+ const srcPath = path.join(templatePath, file);
1816
+
1817
+ // 准备文件信息
1818
+ const processedFileInfo = await prepareFileInfo({
1819
+ file,
1820
+ templatePath,
1821
+ context,
1822
+ templateConfig,
1823
+ });
1824
+
1825
+ // 如果返回 null,跳过该文件
1826
+ if (processedFileInfo === null) {
1827
+ return;
1066
1828
  }
1067
1829
 
1068
- return filePath;
1830
+ // 使用处理后的目标路径
1831
+ const finalDestPath = path.join(outputPath, processedFileInfo.destPath);
1832
+
1833
+ // 写入文件
1834
+ await writeRenderedFile({
1835
+ fileInfo: processedFileInfo,
1836
+ srcPath,
1837
+ destPath: finalDestPath,
1838
+ });
1069
1839
  };
1070
1840
 
1071
1841
  /**
1072
1842
  * 复制并处理模板文件到目标目录
1073
1843
  *
1074
- * @param templatePath - 模板目录路径
1075
- * @param outputPath - 输出目录路径
1076
- * @param context - 模板上下文
1844
+ * 流程:
1845
+ * 1. 验证模板目录
1846
+ * 2. 扫描所有模板文件
1847
+ * 3. Dry-run:收集将要写入的文件列表(考虑 hooks 影响)
1848
+ * 4. 冲突检测:检查是否有文件会被覆盖
1849
+ * 5. 实际写入:渲染并写入所有文件
1850
+ * 6. 复制 node_modules(如果存在)
1851
+ *
1852
+ * @param options - 处理选项
1077
1853
  */
1078
- const processTemplateFiles = async (
1079
- templatePath,
1080
- outputPath,
1081
- context,
1854
+ const processTemplateFiles = async (options
1855
+
1856
+
1857
+
1858
+
1082
1859
  ) => {
1860
+ const { templatePath, outputPath, context, templateConfig } = options;
1083
1861
  logger.verbose('Processing template files:');
1084
1862
  logger.verbose(` - Template path: ${templatePath}`);
1085
1863
  logger.verbose(` - Output path: ${outputPath}`);
1086
1864
 
1087
- // 验证模板目录是否存在
1865
+ // 阶段 0: 验证模板目录是否存在
1088
1866
  try {
1089
1867
  const stat = await fs$1.stat(templatePath);
1090
1868
  logger.verbose(
@@ -1100,6 +1878,7 @@ const processTemplateFiles = async (
1100
1878
  throw error;
1101
1879
  }
1102
1880
 
1881
+ // 阶段 1: 扫描所有模板文件
1103
1882
  const files = await getAllFiles(templatePath);
1104
1883
 
1105
1884
  logger.verbose(` - Found ${files.length} files to process`);
@@ -1109,102 +1888,50 @@ const processTemplateFiles = async (
1109
1888
  return;
1110
1889
  }
1111
1890
 
1112
- await Promise.all(
1113
- files.map(async file => {
1114
- const srcPath = path.join(templatePath, file);
1115
- const destFile = convertDotfileName(file);
1116
- const destPath = path.join(outputPath, destFile);
1891
+ // 阶段 2: Dry-run - 收集所有将要写入的文件
1892
+ const filesToWrite = await collectFilesToRender({
1893
+ files,
1894
+ templatePath,
1895
+ context,
1896
+ templateConfig,
1897
+ });
1117
1898
 
1118
- logger.verbose(
1119
- ` - Processing: ${file}${destFile !== file ? ` -> ${destFile}` : ''}`,
1120
- );
1899
+ // 阶段 3: 冲突检测
1900
+ const conflicts = detectFileConflicts(outputPath, filesToWrite);
1121
1901
 
1122
- // 确保目标目录存在
1123
- await ensureDir(path.dirname(destPath));
1902
+ if (conflicts.length > 0) {
1903
+ // 有冲突,抛出详细的错误信息
1904
+ const conflictList = conflicts.map(f => ` - ${f}`).join('\n');
1905
+ throw new Error(
1906
+ `File conflicts detected in output directory: ${outputPath}\n\n` +
1907
+ `The following files already exist and would be overwritten:\n${conflictList}\n\n` +
1908
+ 'Please remove these files or use a different output directory.',
1909
+ );
1910
+ }
1124
1911
 
1125
- if (shouldRenderFile(srcPath)) {
1126
- // 渲染文本文件
1127
- const rendered = await renderTemplate(srcPath, context);
1128
- await fs$1.writeFile(destPath, rendered, 'utf-8');
1129
- logger.verbose(' ✓ Rendered and written');
1130
- } else {
1131
- // 直接复制二进制文件
1132
- await fs$1.copyFile(srcPath, destPath);
1133
- logger.verbose(' ✓ Copied');
1134
- }
1135
- }),
1912
+ // 阶段 4: 实际写入文件
1913
+ logger.verbose('\nWriting files...');
1914
+ await Promise.all(
1915
+ files.map(file =>
1916
+ processSingleFile({
1917
+ file,
1918
+ templatePath,
1919
+ outputPath,
1920
+ context,
1921
+ templateConfig,
1922
+ }),
1923
+ ),
1136
1924
  );
1137
1925
 
1138
1926
  logger.verbose('✓ All files processed successfully');
1139
1927
 
1140
- // 单独处理 node_modules 目录(如果存在)
1928
+ // 阶段 5: 单独处理 node_modules 目录(如果存在)
1141
1929
  const sourceNodeModules = path.join(templatePath, 'node_modules');
1142
1930
  const targetNodeModules = path.join(outputPath, 'node_modules');
1143
1931
 
1144
- if (fs.existsSync(sourceNodeModules)) {
1145
- logger.info('\nCopying node_modules from pre-warmed template...');
1146
- logger.verbose(` From: ${sourceNodeModules}`);
1147
- logger.verbose(` To: ${targetNodeModules}`);
1148
-
1149
- const result = shelljs.exec(`cp -R "${sourceNodeModules}" "${targetNodeModules}"`, {
1150
- silent: true,
1151
- });
1152
-
1153
- if (result.stdout) {
1154
- process.stdout.write(result.stdout);
1155
- }
1156
-
1157
- if (result.stderr) {
1158
- process.stderr.write(result.stderr);
1159
- }
1160
-
1161
- if (result.code === 0) {
1162
- logger.success('✓ node_modules copied successfully');
1163
- } else {
1164
- logger.warn(
1165
- `Failed to copy node_modules: ${result.stderr || 'unknown error'}`,
1166
- );
1167
- logger.info('Will need to run pnpm install manually');
1168
- }
1169
- }
1170
- };
1171
-
1172
- /**
1173
- * 检查输出目录是否为空
1174
- * 注意:.git 目录会被忽略,允许在已初始化 git 的目录中创建项目
1175
- *
1176
- * @param outputPath - 输出目录路径
1177
- * @returns 是否为空
1178
- */
1179
- const isOutputDirEmpty = async (
1180
- outputPath,
1181
- ) => {
1182
- try {
1183
- const entries = await fs$1.readdir(outputPath);
1184
- // 过滤掉 .git 目录,允许在已初始化 git 的目录中创建项目
1185
- const filteredEntries = entries.filter(entry => entry !== '.git');
1186
- return filteredEntries.length === 0;
1187
- } catch (e) {
1188
- // 目录不存在,视为空
1189
- return true;
1190
- }
1191
- };
1192
-
1193
- /**
1194
- * 验证输出目录
1195
- *
1196
- * @param outputPath - 输出目录路径
1197
- * @throws 如果目录不为空则抛出错误
1198
- */
1199
- const validateOutputDir = async (outputPath) => {
1200
- const isEmpty = await isOutputDirEmpty(outputPath);
1201
- if (!isEmpty) {
1202
- throw new Error(
1203
- `Output directory is not empty: ${outputPath}\n` +
1204
- 'Please use an empty directory or remove existing files.',
1205
- );
1206
- }
1932
+ copyNodeModules(sourceNodeModules, targetNodeModules);
1207
1933
  };
1934
+ // end_aigc
1208
1935
 
1209
1936
  /**
1210
1937
  * 模板引擎执行选项
@@ -1287,9 +2014,9 @@ const executeAfterRenderHook = async (
1287
2014
  /**
1288
2015
  * 准备输出目录
1289
2016
  */
1290
- const prepareOutputDirectory = async (outputPath) => {
2017
+ const prepareOutputDirectory = (outputPath) => {
1291
2018
  const absolutePath = path.resolve(process.cwd(), outputPath);
1292
- await validateOutputDir(absolutePath);
2019
+ // 不再在这里验证目录是否为空,冲突检测已移至 processTemplateFiles 中
1293
2020
  return absolutePath;
1294
2021
  };
1295
2022
 
@@ -1316,10 +2043,15 @@ const execute = async (
1316
2043
  });
1317
2044
 
1318
2045
  // 5. 准备输出目录
1319
- const absoluteOutputPath = await prepareOutputDirectory(outputPath);
2046
+ const absoluteOutputPath = prepareOutputDirectory(outputPath);
1320
2047
 
1321
2048
  // 6. 处理模板文件
1322
- await processTemplateFiles(templatePath, absoluteOutputPath, context);
2049
+ await processTemplateFiles({
2050
+ templatePath,
2051
+ outputPath: absoluteOutputPath,
2052
+ context,
2053
+ templateConfig,
2054
+ });
1323
2055
 
1324
2056
  // 7. 执行 onAfterRender 钩子
1325
2057
  await executeAfterRenderHook(templateConfig, context, absoluteOutputPath);
@@ -1327,7 +2059,7 @@ const execute = async (
1327
2059
  return absoluteOutputPath;
1328
2060
  };
1329
2061
 
1330
- function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2062
+ function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
1331
2063
  /**
1332
2064
  * 运行 pnpm install
1333
2065
  */
@@ -1430,45 +2162,33 @@ const runGitInit = (projectPath) => {
1430
2162
  };
1431
2163
 
1432
2164
  /**
1433
- * 运行开发服务器
2165
+ * 运行开发服务器(后台模式)
2166
+ * 启动后台子进程运行开发服务器,父进程可以直接退出
2167
+ * 使用 CLI 自己的 dev 命令(定义在 run.ts)而不是直接运行 npm run dev
1434
2168
  */
1435
- const runNpmDev = (projectPath) => {
1436
- logger.info('\nStarting development server...');
1437
- logger.info(`Executing: npm run dev in ${projectPath}`);
1438
- logger.info('Press Ctrl+C to stop the server\n');
1439
-
1440
- // 使用 async: true 异步执行,不阻塞进程
1441
- const child = shelljs.exec('npm run dev', {
1442
- cwd: projectPath,
1443
- async: true,
1444
- silent: true, // 手动处理输出以便显示详细信息
1445
- });
2169
+ const runDev = (projectPath) => {
2170
+ logger.info('\nStarting development server in background...');
1446
2171
 
1447
- if (child) {
1448
- // 输出 stdout
1449
- _optionalChain([child, 'access', _ => _.stdout, 'optionalAccess', _2 => _2.on, 'call', _3 => _3('data', (data) => {
1450
- process.stdout.write(data);
1451
- })]);
2172
+ // 获取当前 CLI 的可执行文件路径
2173
+ // process.argv[0] 是 node,process.argv[1] 是 CLI 入口文件
2174
+ const cliPath = process.argv[1];
1452
2175
 
1453
- // 输出 stderr
1454
- _optionalChain([child, 'access', _4 => _4.stderr, 'optionalAccess', _5 => _5.on, 'call', _6 => _6('data', (data) => {
1455
- process.stderr.write(data);
1456
- })]);
2176
+ logger.info(`Executing: ${cliPath} dev in ${projectPath}`);
1457
2177
 
1458
- // 监听错误
1459
- child.on('error', (error) => {
1460
- logger.error(`Failed to start dev server: ${error.message}`);
1461
- logger.error(`Error stack: ${error.stack}`);
1462
- });
2178
+ // 使用通用的后台执行函数启动开发服务器
2179
+ // 调用 CLI 自己的 dev 命令
2180
+ const pid = spawnDetached(process.argv[0], [cliPath, 'dev'], {
2181
+ cwd: projectPath,
2182
+ verbose: false, // 不输出额外的进程信息,由 logger 统一处理
2183
+ });
1463
2184
 
1464
- // 监听退出
1465
- child.on('exit', (code, signal) => {
1466
- if (code !== 0 && code !== null) {
1467
- logger.error(
1468
- `Dev server exited with code ${code}${signal ? ` and signal ${signal}` : ''}`,
1469
- );
1470
- }
1471
- });
2185
+ logger.success('Development server started in background!');
2186
+ if (pid) {
2187
+ logger.info(`Process ID: ${pid}`);
2188
+ logger.info(
2189
+ '\nThe dev server is running independently. You can close this terminal.',
2190
+ );
2191
+ logger.info(`To stop the server later, use: kill ${pid}`);
1472
2192
  }
1473
2193
  };
1474
2194
 
@@ -1533,7 +2253,7 @@ const executeInit = async (
1533
2253
 
1534
2254
  // 如果没有跳过 dev,则启动开发服务器
1535
2255
  if (!skipDev) {
1536
- runNpmDev(absoluteOutputPath);
2256
+ runDev(absoluteOutputPath);
1537
2257
  timer.logPhase('Dev server startup');
1538
2258
  } else {
1539
2259
  // 只有跳过 dev 时才显示 Next steps
@@ -1547,7 +2267,7 @@ const executeInit = async (
1547
2267
  ' git init && git add . && git commit -m "initial commit"',
1548
2268
  );
1549
2269
  }
1550
- logger.info(' npm run dev');
2270
+ logger.info(' coze dev');
1551
2271
  }
1552
2272
 
1553
2273
  // 输出总耗时
@@ -1563,7 +2283,7 @@ const executeInit = async (
1563
2283
  /**
1564
2284
  * 注册 init 命令到 program
1565
2285
  */
1566
- const registerCommand = program => {
2286
+ const registerCommand$1 = program => {
1567
2287
  program
1568
2288
  .command('init')
1569
2289
  .description('Initialize a new project from a template')
@@ -1581,14 +2301,312 @@ const registerCommand = program => {
1581
2301
  });
1582
2302
  };
1583
2303
 
1584
- var version = "0.0.1-alpha.912cd5";
2304
+ // ABOUTME: This file implements the update command for coze CLI
2305
+ // ABOUTME: It wraps pnpm update/install to update package dependencies with logging support
2306
+
2307
+
2308
+
2309
+
2310
+ /**
2311
+ * 日志文件名常量
2312
+ */
2313
+ const LOG_FILE_NAME = 'update.log';
2314
+
2315
+ /**
2316
+ * 获取日志目录
2317
+ * 优先使用环境变量 COZE_LOG_DIR,否则使用 ~/.coze-logs
2318
+ */
2319
+ const getLogDir = () =>
2320
+ process.env.COZE_LOG_DIR || path.join(os.homedir(), '.coze-logs');
2321
+
2322
+ /**
2323
+ * 解析日志文件路径
2324
+ * - 如果是绝对路径,直接使用
2325
+ * - 如果是相对路径,基于 getLogDir() + 相对路径
2326
+ * - 如果为空,使用 getLogDir() + LOG_FILE_NAME
2327
+ */
2328
+ const resolveLogFilePath = (logFile) => {
2329
+ if (!logFile) {
2330
+ return path.join(getLogDir(), LOG_FILE_NAME);
2331
+ }
2332
+
2333
+ if (path.isAbsolute(logFile)) {
2334
+ return logFile;
2335
+ }
2336
+
2337
+ return path.join(getLogDir(), logFile);
2338
+ };
2339
+
2340
+ /**
2341
+ * 创建日志写入流
2342
+ */
2343
+ const createLogStream = (logFilePath) => {
2344
+ const logDir = path.dirname(logFilePath);
2345
+
2346
+ // 确保日志目录存在
2347
+ if (!fs.existsSync(logDir)) {
2348
+ fs.mkdirSync(logDir, { recursive: true });
2349
+ }
2350
+
2351
+ // 使用 'w' 标志覆盖之前的日志
2352
+ return fs.createWriteStream(logFilePath, { flags: 'w' });
2353
+ };
2354
+
2355
+ /**
2356
+ * 格式化时间戳
2357
+ */
2358
+ const formatTimestamp = () => {
2359
+ const now = new Date();
2360
+ return now.toISOString();
2361
+ };
2362
+
2363
+ /**
2364
+ * 写入带时间戳的日志
2365
+ */
2366
+ const writeLogWithTimestamp = (stream, message) => {
2367
+ const timestamp = formatTimestamp();
2368
+ const lines = message.split('\n');
2369
+ lines.forEach(line => {
2370
+ if (line) {
2371
+ stream.write(`[${timestamp}] ${line}\n`);
2372
+ } else {
2373
+ stream.write('\n');
2374
+ }
2375
+ });
2376
+ // 确保数据写入磁盘
2377
+ stream.uncork();
2378
+ };
2379
+
2380
+ /**
2381
+ * 同时输出到控制台和日志文件
2382
+ */
2383
+ const logWithFile = (
2384
+ stream,
2385
+ level,
2386
+ message,
2387
+ ) => {
2388
+ // 输出到控制台
2389
+ switch (level) {
2390
+ case 'info':
2391
+ logger.info(message);
2392
+ break;
2393
+ case 'success':
2394
+ logger.success(message);
2395
+ break;
2396
+ case 'error':
2397
+ logger.error(message);
2398
+ break;
2399
+ default:
2400
+ logger.info(message);
2401
+ break;
2402
+ }
2403
+
2404
+ // 写入日志文件(带时间戳)
2405
+ writeLogWithTimestamp(stream, `[${level.toUpperCase()}] ${message}`);
2406
+ };
2407
+
2408
+ // start_aigc
2409
+ /**
2410
+ * 构建 pnpm add 命令
2411
+ */
2412
+ const buildPnpmCommand = (
2413
+ packageName,
2414
+ options
2415
+
2416
+
2417
+
2418
+
2419
+ ,
2420
+ ) => {
2421
+ const { global, version, registry, extraArgs } = options;
2422
+
2423
+ const parts = ['pnpm', 'add'];
2424
+
2425
+ // 添加全局标记
2426
+ if (global) {
2427
+ parts.push('-g');
2428
+ }
2429
+
2430
+ // 添加包名和版本
2431
+ if (version && version !== 'latest') {
2432
+ parts.push(`${packageName}@${version}`);
2433
+ } else {
2434
+ parts.push(`${packageName}@latest`);
2435
+ }
2436
+
2437
+ // 添加 registry
2438
+ if (registry) {
2439
+ parts.push(`--registry=${registry}`);
2440
+ }
2441
+
2442
+ // 添加额外参数
2443
+ if (extraArgs.length > 0) {
2444
+ parts.push(...extraArgs);
2445
+ }
2446
+
2447
+ return parts.join(' ');
2448
+ };
2449
+ // end_aigc
2450
+
2451
+ // start_aigc
2452
+ /**
2453
+ * 执行 update 命令的内部实现
2454
+ */
2455
+ const executeUpdate = (
2456
+ packageName,
2457
+ options
2458
+
2459
+
2460
+
2461
+
2462
+
2463
+
2464
+ ,
2465
+ ) => {
2466
+ let logStream = null;
2467
+
2468
+ try {
2469
+ const { global, cwd, version, registry, logFile, extraArgs } = options;
2470
+
2471
+ // 准备日志
2472
+ const logFilePath = resolveLogFilePath(logFile);
2473
+
2474
+ // 调试:确认日志路径
2475
+ logger.info(`Log file path resolved to: ${logFilePath}`);
2476
+
2477
+ logStream = createLogStream(logFilePath);
2478
+
2479
+ // 调试:确认流已创建
2480
+ logger.info('Log stream created successfully');
2481
+
2482
+ logWithFile(logStream, 'info', `Updating package: ${packageName}`);
2483
+
2484
+ // 构建命令
2485
+ const command = buildPnpmCommand(packageName, {
2486
+ global,
2487
+ version,
2488
+ registry,
2489
+ extraArgs,
2490
+ });
2491
+
2492
+ // 确定工作目录
2493
+ const workingDir = cwd
2494
+ ? path.isAbsolute(cwd)
2495
+ ? cwd
2496
+ : path.join(process.cwd(), cwd)
2497
+ : process.cwd();
2498
+
2499
+ logWithFile(logStream, 'info', `Executing: ${command}`);
2500
+ logWithFile(logStream, 'info', `Working directory: ${workingDir}`);
2501
+ logWithFile(logStream, 'info', `Log file: ${logFilePath}`);
2502
+
2503
+ // 记录命令开始时间
2504
+ writeLogWithTimestamp(logStream, '--- Command execution started ---');
2505
+
2506
+ // 同步执行命令
2507
+ const result = shelljs.exec(command, {
2508
+ cwd: workingDir,
2509
+ silent: true, // 使用 silent 来捕获输出
2510
+ });
2511
+
2512
+ // 将输出写入控制台和日志文件(带时间戳)
2513
+ if (result.stdout) {
2514
+ process.stdout.write(result.stdout);
2515
+ writeLogWithTimestamp(logStream, result.stdout.trim());
2516
+ }
2517
+
2518
+ if (result.stderr) {
2519
+ process.stderr.write(result.stderr);
2520
+ writeLogWithTimestamp(logStream, result.stderr.trim());
2521
+ }
2522
+
2523
+ // 记录命令结束时间
2524
+ writeLogWithTimestamp(logStream, '--- Command execution ended ---');
2525
+
2526
+ // 检查执行结果并记录到日志
2527
+ if (result.code === 0) {
2528
+ logWithFile(logStream, 'success', 'Package updated successfully');
2529
+ logWithFile(logStream, 'info', `Log file: ${logFilePath}`);
2530
+ } else {
2531
+ logWithFile(
2532
+ logStream,
2533
+ 'error',
2534
+ `Command exited with code ${result.code}`,
2535
+ );
2536
+ logWithFile(
2537
+ logStream,
2538
+ 'error',
2539
+ `Check log file for details: ${logFilePath}`,
2540
+ );
2541
+ }
2542
+
2543
+ // 关闭日志流并等待写入完成
2544
+ logStream.end(() => {
2545
+ // 流关闭后再退出进程
2546
+ if (result.code !== 0) {
2547
+ process.exit(result.code || 1);
2548
+ }
2549
+ });
2550
+ } catch (error) {
2551
+ logger.error('Failed to update package:');
2552
+ logger.error(error instanceof Error ? error.message : String(error));
2553
+
2554
+ // 写入错误到日志文件
2555
+ if (logStream) {
2556
+ writeLogWithTimestamp(
2557
+ logStream,
2558
+ `[ERROR] ${error instanceof Error ? error.message : String(error)}`,
2559
+ );
2560
+ // 等待流关闭后再退出
2561
+ logStream.end(() => {
2562
+ process.exit(1);
2563
+ });
2564
+ } else {
2565
+ process.exit(1);
2566
+ }
2567
+ }
2568
+ };
2569
+ // end_aigc
2570
+
2571
+ /**
2572
+ * 注册 update 命令到 program
2573
+ */
2574
+ const registerCommand = program => {
2575
+ program
2576
+ .command('update <package>')
2577
+ .description('Update a package dependency')
2578
+ .option('-g, --global', 'Update package globally', false)
2579
+ .option('-c, --cwd <path>', 'Working directory for the update')
2580
+ .option(
2581
+ '--to <version>',
2582
+ 'Version to update to (default: latest)',
2583
+ 'latest',
2584
+ )
2585
+ .option('--registry <url>', 'Registry URL to use for the update')
2586
+ .option('--log-file <path>', 'Log file path')
2587
+ .allowUnknownOption() // 允许透传参数给 pnpm
2588
+ .action((packageName, options, command) => {
2589
+ // 收集所有未知选项作为额外参数
2590
+ const extraArgs = command.args.slice(1);
2591
+
2592
+ executeUpdate(packageName, {
2593
+ ...options,
2594
+ version: options.to, // 将 --to 映射到 version
2595
+ extraArgs,
2596
+ });
2597
+ });
2598
+ };
2599
+
2600
+ var version = "0.0.1-alpha.934b8f";
1585
2601
  var packageJson = {
1586
2602
  version: version};
1587
2603
 
1588
2604
  const commands = [
1589
- registerCommand,
1590
2605
  registerCommand$1,
1591
- // registerWarmupCommand,
2606
+ registerCommand$2,
2607
+ registerCommand$4,
2608
+ registerCommand$3,
2609
+ registerCommand,
1592
2610
  ];
1593
2611
 
1594
2612
  const main = () => {