@coze-arch/cli 0.0.1-alpha.a1ca15 → 0.0.1-alpha.a2a210

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 (139) 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 +2 -4
  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 +124 -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 -103
  40. package/lib/__templates__/expo/patches/expo@54.0.33.patch +45 -0
  41. package/lib/__templates__/expo/pnpm-lock.yaml +1437 -3171
  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 +34 -0
  45. package/lib/__templates__/expo/server/src/index.ts +20 -0
  46. package/lib/__templates__/expo/server/tsconfig.json +24 -0
  47. package/lib/__templates__/expo/template.config.js +58 -1
  48. package/lib/__templates__/expo/tsconfig.json +1 -24
  49. package/lib/__templates__/native-static/.coze +11 -0
  50. package/lib/__templates__/native-static/index.html +33 -0
  51. package/lib/__templates__/native-static/styles/main.css +136 -0
  52. package/lib/__templates__/native-static/template.config.js +22 -0
  53. package/lib/__templates__/nextjs/.babelrc +15 -0
  54. package/lib/__templates__/nextjs/.coze +1 -0
  55. package/lib/__templates__/nextjs/_npmrc +1 -0
  56. package/lib/__templates__/nextjs/next.config.ts +12 -0
  57. package/lib/__templates__/nextjs/package.json +13 -2
  58. package/lib/__templates__/nextjs/pnpm-lock.yaml +2682 -1786
  59. package/lib/__templates__/nextjs/scripts/dev.sh +7 -26
  60. package/lib/__templates__/nextjs/scripts/prepare.sh +9 -0
  61. package/lib/__templates__/nextjs/src/app/globals.css +109 -89
  62. package/lib/__templates__/nextjs/src/app/layout.tsx +23 -32
  63. package/lib/__templates__/nextjs/src/app/page.tsx +18 -48
  64. package/lib/__templates__/nextjs/src/components/ui/resizable.tsx +29 -22
  65. package/lib/__templates__/nextjs/src/components/ui/sidebar.tsx +228 -230
  66. package/lib/__templates__/nextjs/template.config.js +67 -2
  67. package/lib/__templates__/taro/.coze +14 -0
  68. package/lib/__templates__/taro/.cozeproj/scripts/deploy_build.sh +19 -0
  69. package/lib/__templates__/taro/.cozeproj/scripts/deploy_run.sh +14 -0
  70. package/lib/__templates__/taro/.cozeproj/scripts/dev_build.sh +2 -0
  71. package/lib/__templates__/taro/.cozeproj/scripts/dev_run.sh +151 -0
  72. package/lib/__templates__/taro/.cozeproj/scripts/init_env.sh +5 -0
  73. package/lib/__templates__/taro/.cozeproj/scripts/pack.sh +24 -0
  74. package/lib/__templates__/taro/README.md +749 -0
  75. package/lib/__templates__/taro/_gitignore +40 -0
  76. package/lib/__templates__/taro/_npmrc +18 -0
  77. package/lib/__templates__/taro/babel.config.js +12 -0
  78. package/lib/__templates__/taro/config/dev.ts +9 -0
  79. package/lib/__templates__/taro/config/index.ts +173 -0
  80. package/lib/__templates__/taro/config/prod.ts +35 -0
  81. package/lib/__templates__/taro/eslint.config.mjs +79 -0
  82. package/lib/__templates__/taro/key/private.appid.key +0 -0
  83. package/lib/__templates__/taro/package.json +97 -0
  84. package/lib/__templates__/taro/pnpm-lock.yaml +22694 -0
  85. package/lib/__templates__/taro/pnpm-workspace.yaml +2 -0
  86. package/lib/__templates__/taro/project.config.json +15 -0
  87. package/lib/__templates__/taro/server/nest-cli.json +10 -0
  88. package/lib/__templates__/taro/server/package.json +40 -0
  89. package/lib/__templates__/taro/server/src/app.controller.ts +23 -0
  90. package/lib/__templates__/taro/server/src/app.module.ts +10 -0
  91. package/lib/__templates__/taro/server/src/app.service.ts +8 -0
  92. package/lib/__templates__/taro/server/src/interceptors/http-status.interceptor.ts +23 -0
  93. package/lib/__templates__/taro/server/src/main.ts +49 -0
  94. package/lib/__templates__/taro/server/tsconfig.json +24 -0
  95. package/lib/__templates__/taro/src/app.config.ts +11 -0
  96. package/lib/__templates__/taro/src/app.css +52 -0
  97. package/lib/__templates__/taro/src/app.tsx +9 -0
  98. package/lib/__templates__/taro/src/index.html +39 -0
  99. package/lib/__templates__/taro/src/network.ts +39 -0
  100. package/lib/__templates__/taro/src/pages/index/index.config.ts +3 -0
  101. package/lib/__templates__/taro/src/pages/index/index.css +1 -0
  102. package/lib/__templates__/taro/src/pages/index/index.tsx +33 -0
  103. package/lib/__templates__/taro/src/presets/h5-navbar.tsx +171 -0
  104. package/lib/__templates__/taro/src/presets/h5-styles.ts +33 -0
  105. package/lib/__templates__/taro/src/presets/index.tsx +18 -0
  106. package/lib/__templates__/taro/src/presets/wx-debug.ts +23 -0
  107. package/lib/__templates__/taro/stylelint.config.mjs +4 -0
  108. package/lib/__templates__/taro/template.config.js +68 -0
  109. package/lib/__templates__/taro/tsconfig.json +29 -0
  110. package/lib/__templates__/taro/types/global.d.ts +32 -0
  111. package/lib/__templates__/templates.json +104 -36
  112. package/lib/__templates__/vite/.coze +1 -0
  113. package/lib/__templates__/vite/_npmrc +1 -0
  114. package/lib/__templates__/vite/eslint.config.mjs +9 -0
  115. package/lib/__templates__/vite/package.json +15 -2
  116. package/lib/__templates__/vite/pnpm-lock.yaml +1697 -221
  117. package/lib/__templates__/vite/scripts/dev.sh +7 -26
  118. package/lib/__templates__/vite/scripts/prepare.sh +9 -0
  119. package/lib/__templates__/vite/src/main.ts +17 -48
  120. package/lib/__templates__/vite/template.config.js +78 -8
  121. package/lib/__templates__/vite/vite.config.ts +4 -3
  122. package/lib/cli.js +1061 -191
  123. package/package.json +9 -4
  124. package/lib/__templates__/expo/.cozeproj/scripts/deploy_build.sh +0 -115
  125. package/lib/__templates__/expo/.cozeproj/scripts/deploy_run.sh +0 -271
  126. package/lib/__templates__/expo/app.json +0 -63
  127. package/lib/__templates__/expo/babel.config.js +0 -9
  128. package/lib/__templates__/expo/client/app/(tabs)/_layout.tsx +0 -43
  129. package/lib/__templates__/expo/client/app/(tabs)/home.tsx +0 -1
  130. package/lib/__templates__/expo/client/app/(tabs)/index.tsx +0 -7
  131. package/lib/__templates__/expo/client/hooks/useColorScheme.ts +0 -1
  132. package/lib/__templates__/expo/client/index.js +0 -12
  133. package/lib/__templates__/expo/client/screens/home/index.tsx +0 -54
  134. package/lib/__templates__/expo/client/screens/home/styles.ts +0 -332
  135. package/lib/__templates__/expo/metro.config.js +0 -53
  136. package/lib/__templates__/expo/src/index.ts +0 -12
  137. package/lib/__templates__/nextjs/.vscode/settings.json +0 -121
  138. package/lib/__templates__/vite/.vscode/settings.json +0 -7
  139. /package/lib/__templates__/expo/{eslint-formatter-simple.mjs → client/eslint-formatter-simple.mjs} +0 -0
package/lib/cli.js CHANGED
@@ -7,8 +7,10 @@ var fs = require('fs');
7
7
  var shelljs = require('shelljs');
8
8
  var perf_hooks = require('perf_hooks');
9
9
  var fs$1 = require('fs/promises');
10
- var toml = require('@iarna/toml');
10
+ var os = require('os');
11
11
  var jsYaml = require('js-yaml');
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');
14
16
  var minimist = require('minimist');
@@ -125,7 +127,7 @@ const generateTemplatesHelpText = () => {
125
127
  return lines.join('\n');
126
128
  };
127
129
 
128
- 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) {
129
131
  const ERROR = 0; LogLevel[LogLevel["ERROR"] = ERROR] = "ERROR";
130
132
  const WARN = 1; LogLevel[LogLevel["WARN"] = WARN] = "WARN";
131
133
  const SUCCESS = 2; LogLevel[LogLevel["SUCCESS"] = SUCCESS] = "SUCCESS";
@@ -172,7 +174,7 @@ class Logger {
172
174
  return level;
173
175
  }
174
176
 
175
- 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()]);
176
178
  if (envLevel && envLevel in LOG_LEVEL_MAP) {
177
179
  return LOG_LEVEL_MAP[envLevel];
178
180
  }
@@ -184,7 +186,7 @@ class Logger {
184
186
  // 简单检测:Node.js 环境且支持 TTY
185
187
  return (
186
188
  typeof process !== 'undefined' &&
187
- _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 &&
188
190
  process.env.NO_COLOR === undefined
189
191
  );
190
192
  }
@@ -583,7 +585,7 @@ const executeWarmup = async (
583
585
  /**
584
586
  * 注册 warmup 命令到 program
585
587
  */
586
- const registerCommand$2 = program => {
588
+ const registerCommand$4 = program => {
587
589
  program
588
590
  .command('warmup')
589
591
  .description('Pre-install dependencies for templates to speed up init')
@@ -593,7 +595,7 @@ const registerCommand$2 = program => {
593
595
  });
594
596
  };
595
597
 
596
- 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 */
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 */
597
599
  // Safe JSON parsing utilities with type safety and error handling
598
600
  // Provides fallback values, validation, and error monitoring capabilities
599
601
 
@@ -684,12 +686,12 @@ function safeJsonParse(
684
686
  const parsed = JSON.parse(String(input));
685
687
 
686
688
  // Optional validation
687
- if (_optionalChain$3([options, 'optionalAccess', _ => _.validate])) {
689
+ if (_optionalChain$2([options, 'optionalAccess', _ => _.validate])) {
688
690
  if (options.validate(parsed)) {
689
691
  return parsed;
690
692
  } else {
691
693
  const validationError = new Error('JSON validation failed');
692
- _optionalChain$3([options, 'access', _2 => _2.onError, 'optionalCall', _3 => _3(validationError, input)]);
694
+ _optionalChain$2([options, 'access', _2 => _2.onError, 'optionalCall', _3 => _3(validationError, input)]);
693
695
 
694
696
  if (options.throwOnValidationError) {
695
697
  throw validationError;
@@ -701,15 +703,15 @@ function safeJsonParse(
701
703
  return parsed;
702
704
  } catch (error) {
703
705
  // Re-throw validation errors when throwOnValidationError is true
704
- if (error instanceof Error && error.message === 'JSON validation failed' && _optionalChain$3([options, 'optionalAccess', _4 => _4.throwOnValidationError])) {
706
+ if (error instanceof Error && error.message === 'JSON validation failed' && _optionalChain$2([options, 'optionalAccess', _4 => _4.throwOnValidationError])) {
705
707
  throw error;
706
708
  }
707
- _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)]);
708
710
  return defaultValue;
709
711
  }
710
712
  }
711
713
 
712
- 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; }
713
715
 
714
716
 
715
717
  /**
@@ -725,7 +727,7 @@ const parseConfigContent = (content) => {
725
727
  return config ;
726
728
  } catch (error) {
727
729
  // TOML 解析失败,继续尝试其他格式
728
- // eslint-disable-next-line no-console
730
+
729
731
  console.debug('TOML parse failed:', error);
730
732
  }
731
733
 
@@ -737,7 +739,7 @@ const parseConfigContent = (content) => {
737
739
  }
738
740
  } catch (error) {
739
741
  // YAML 解析失败,继续尝试其他格式
740
- // eslint-disable-next-line no-console
742
+
741
743
  console.debug('YAML parse failed:', error);
742
744
  }
743
745
 
@@ -799,13 +801,13 @@ const getCommandConfig = (
799
801
  // 根据命令名称映射到配置路径
800
802
  switch (commandName) {
801
803
  case 'dev':
802
- commandConfig = _optionalChain$2([config, 'access', _ => _.dev, 'optionalAccess', _2 => _2.run]);
804
+ commandConfig = _optionalChain$1([config, 'access', _ => _.dev, 'optionalAccess', _2 => _2.run]);
803
805
  break;
804
806
  case 'build':
805
- 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]);
806
808
  break;
807
809
  case 'start':
808
- 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]);
809
811
  break;
810
812
  default:
811
813
  throw new Error(`Unknown command: ${commandName}`);
@@ -821,27 +823,276 @@ const getCommandConfig = (
821
823
  return commandConfig;
822
824
  };
823
825
 
824
- 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
+ };
825
855
 
826
856
  /**
827
- * 创建日志管理器
857
+ * 查找 Next.js 配置文件
828
858
  */
829
- const createLogManager = (logDir = '.coze-logs') => {
830
- const ensureLogDir = () => {
831
- if (!fs.existsSync(logDir)) {
832
- 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;
833
870
  }
834
- };
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);
835
893
 
836
- const getLogPath = (logFile) => path.join(logDir, logFile);
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
+ }
934
+
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
+ }
837
950
 
838
951
  return {
839
- createWriteStream: (logFile) => {
840
- ensureLogDir();
841
- return fs.createWriteStream(getLogPath(logFile), { flags: 'a' });
842
- },
952
+ ruleName,
953
+ applied: false,
954
+ message: 'No outputFileTracingRoot config found, skipping',
843
955
  };
844
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
+ };
845
1096
 
846
1097
  /**
847
1098
  * 执行命令的内部实现
@@ -853,21 +1104,32 @@ const executeRun = async (
853
1104
  try {
854
1105
  logger.info(`Running ${commandName} command...`);
855
1106
 
856
- // 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 配置
857
1120
  const config = await loadCozeConfig();
858
1121
  const commandArgs = getCommandConfig(config, commandName);
859
1122
 
860
- // 2. 准备日志
861
- const logManager = createLogManager();
862
- const logFile = options.logFile || `${commandName}.log`;
863
- const logStream = logManager.createWriteStream(logFile);
1123
+ // 3. 准备日志
1124
+ const logFilePath = resolveLogFilePath$1(options.logFile);
1125
+ const logStream = createLogStream$1(logFilePath);
864
1126
 
865
- // 3. 执行命令
1127
+ // 4. 执行命令
866
1128
  const commandString = commandArgs.join(' ');
867
1129
 
868
1130
  logger.info(`Executing: ${commandString}`);
869
1131
  logger.info(`Working directory: ${process.cwd()}`);
870
- logger.info(`Log file: ${logFile}`);
1132
+ logger.info(`Log file: ${logFilePath}`);
871
1133
 
872
1134
  const childProcess = shelljs.exec(commandString, {
873
1135
  async: true,
@@ -879,12 +1141,12 @@ const executeRun = async (
879
1141
  }
880
1142
 
881
1143
  // 将输出同时写入控制台和日志文件
882
- _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) => {
883
1145
  process.stdout.write(data);
884
1146
  logStream.write(data);
885
1147
  })]);
886
1148
 
887
- _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) => {
888
1150
  process.stderr.write(data);
889
1151
  logStream.write(data);
890
1152
  })]);
@@ -896,11 +1158,11 @@ const executeRun = async (
896
1158
  logger.error(
897
1159
  `Command exited with code ${_nullishCoalesce$1(code, () => ( 'unknown'))}${signal ? ` and signal ${signal}` : ''}`,
898
1160
  );
899
- logger.error(`Check log file for details: ${logFile}`);
1161
+ logger.error(`Check log file for details: ${logFilePath}`);
900
1162
  process.exit(code || 1);
901
1163
  } else {
902
1164
  logger.success('Command completed successfully');
903
- logger.info(`Log file: ${logFile}`);
1165
+ logger.info(`Log file: ${logFilePath}`);
904
1166
  }
905
1167
  });
906
1168
 
@@ -923,7 +1185,7 @@ const executeRun = async (
923
1185
  /**
924
1186
  * 注册 dev/build/start 命令到 program
925
1187
  */
926
- const registerCommand$1 = program => {
1188
+ const registerCommand$2 = program => {
927
1189
  // dev 命令
928
1190
  program
929
1191
  .command('dev')
@@ -952,6 +1214,45 @@ const registerCommand$1 = program => {
952
1214
  });
953
1215
  };
954
1216
 
1217
+ /**
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
1225
+ */
1226
+ function spawnDetached(
1227
+ command,
1228
+ args,
1229
+ options,
1230
+ ) {
1231
+ const { cwd, verbose = true } = options;
1232
+ const isWindows = os.platform() === 'win32';
1233
+
1234
+ if (verbose) {
1235
+ console.log(`Spawning detached process: ${command} ${args.join(' ')}`);
1236
+ console.log(`Working directory: ${cwd}`);
1237
+ }
1238
+
1239
+ // 使用 spawn 创建后台子进程
1240
+ const child = child_process.spawn(command, args, {
1241
+ cwd,
1242
+ detached: !isWindows, // Windows 不完全支持 detached,但仍可以使用
1243
+ stdio: 'ignore', // 忽略所有输入输出,让进程完全独立运行
1244
+ });
1245
+
1246
+ // 分离父子进程引用,允许父进程退出而不等待子进程
1247
+ child.unref();
1248
+
1249
+ if (verbose && child.pid) {
1250
+ console.log(`Process started with PID: ${child.pid}`);
1251
+ }
1252
+
1253
+ return child.pid;
1254
+ }
1255
+
955
1256
  /**
956
1257
  * 创建 AJV 验证器实例
957
1258
  */
@@ -1136,6 +1437,11 @@ const shouldIgnoreFile = (filePath) => {
1136
1437
  return directoryPatterns.some(dir => pathParts.includes(dir));
1137
1438
  };
1138
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
1139
1445
  /**
1140
1446
  * 递归获取目录中的所有文件
1141
1447
  *
@@ -1205,144 +1511,383 @@ const convertDotfileName = (filePath) => {
1205
1511
 
1206
1512
  return filePath;
1207
1513
  };
1514
+ // end_aigc
1515
+
1516
+ // ABOUTME: File rendering utilities for template processing
1517
+ // ABOUTME: Handles file content rendering, hook execution, and file writing
1518
+
1519
+
1520
+
1521
+
1208
1522
 
1523
+
1524
+
1525
+
1526
+ // start_aigc
1209
1527
  /**
1210
- * 复制并处理模板文件到目标目录
1528
+ * 执行文件渲染钩子
1211
1529
  *
1212
- * @param templatePath - 模板目录路径
1213
- * @param outputPath - 输出目录路径
1530
+ * @param templateConfig - 模板配置
1531
+ * @param fileInfo - 文件渲染信息
1214
1532
  * @param context - 模板上下文
1533
+ * @returns 处理后的文件信息,或 null 表示跳过该文件
1215
1534
  */
1216
- const processTemplateFiles = async (
1217
- templatePath,
1218
- outputPath,
1535
+ const executeFileRenderHook = async (
1536
+ templateConfig,
1537
+ fileInfo,
1219
1538
  context,
1220
1539
  ) => {
1221
- logger.verbose('Processing template files:');
1222
- logger.verbose(` - Template path: ${templatePath}`);
1223
- logger.verbose(` - Output path: ${outputPath}`);
1224
-
1225
- // 验证模板目录是否存在
1226
- try {
1227
- const stat = await fs$1.stat(templatePath);
1228
- logger.verbose(
1229
- ` - Template path exists: ${stat.isDirectory() ? 'directory' : 'file'}`,
1230
- );
1231
- if (!stat.isDirectory()) {
1232
- throw new Error(`Template path is not a directory: ${templatePath}`);
1233
- }
1234
- } catch (error) {
1235
- logger.error(
1236
- ` - Failed to access template path: ${error instanceof Error ? error.message : String(error)}`,
1237
- );
1238
- throw error;
1540
+ if (!templateConfig.onFileRender) {
1541
+ return fileInfo;
1239
1542
  }
1240
1543
 
1241
- const files = await getAllFiles(templatePath);
1544
+ const result = await templateConfig.onFileRender(
1545
+ fileInfo,
1546
+ context,
1547
+ );
1242
1548
 
1243
- logger.verbose(` - Found ${files.length} files to process`);
1549
+ // false: 跳过文件
1550
+ if (result === false) {
1551
+ return null;
1552
+ }
1244
1553
 
1245
- if (files.length === 0) {
1246
- logger.warn(' - No files found in template directory!');
1247
- return;
1554
+ // undefined/void: 使用默认内容
1555
+ if (result === undefined || result === null) {
1556
+ return fileInfo;
1248
1557
  }
1249
1558
 
1250
- await Promise.all(
1251
- files.map(async file => {
1252
- const srcPath = path.join(templatePath, file);
1253
- const destFile = convertDotfileName(file);
1254
- const destPath = path.join(outputPath, destFile);
1559
+ // string: 作为 content,其他不变
1560
+ if (typeof result === 'string') {
1561
+ return {
1562
+ ...fileInfo,
1563
+ content: result,
1564
+ };
1565
+ }
1255
1566
 
1256
- logger.verbose(
1257
- ` - Processing: ${file}${destFile !== file ? ` -> ${destFile}` : ''}`,
1258
- );
1567
+ // FileRenderInfo: 使用新对象的信息
1568
+ return result;
1569
+ };
1259
1570
 
1260
- // 确保目标目录存在
1261
- await ensureDir(path.dirname(destPath));
1571
+ /**
1572
+ * 准备单个文件的渲染信息(不实际写入)
1573
+ * 用于 dry-run 阶段,收集所有将要写入的文件信息
1574
+ *
1575
+ * @param options - 准备选项
1576
+ * @returns 文件渲染信息,或 null 表示该文件被跳过
1577
+ */
1578
+ const prepareFileInfo = async (options
1262
1579
 
1263
- if (shouldRenderFile(srcPath)) {
1264
- // 渲染文本文件
1265
- const rendered = await renderTemplate(srcPath, context);
1266
- await fs$1.writeFile(destPath, rendered, 'utf-8');
1267
- logger.verbose(' ✓ Rendered and written');
1268
- } else {
1269
- // 直接复制二进制文件
1270
- await fs$1.copyFile(srcPath, destPath);
1271
- logger.verbose(' ✓ Copied');
1272
- }
1273
- }),
1580
+
1581
+
1582
+
1583
+ ) => {
1584
+ const { file, templatePath, context, templateConfig } = options;
1585
+
1586
+ const srcPath = path.join(templatePath, file);
1587
+ const destFile = convertDotfileName(file);
1588
+
1589
+ logger.verbose(
1590
+ ` - Preparing: ${file}${destFile !== file ? ` -> ${destFile}` : ''}`,
1274
1591
  );
1275
1592
 
1276
- logger.verbose('✓ All files processed successfully');
1593
+ // 判断是否为二进制文件
1594
+ const isBinary = !shouldRenderFile(srcPath);
1595
+ let content;
1596
+ let wasRendered = false;
1597
+
1598
+ if (isBinary) {
1599
+ // 二进制文件,读取为 buffer 然后转为 base64
1600
+ const buffer = await fs$1.readFile(srcPath);
1601
+ content = buffer.toString('base64');
1602
+ } else {
1603
+ // 文本文件,渲染后的内容
1604
+ content = await renderTemplate(srcPath, context);
1605
+ wasRendered = true;
1606
+ }
1277
1607
 
1278
- // 单独处理 node_modules 目录(如果存在)
1279
- const sourceNodeModules = path.join(templatePath, 'node_modules');
1280
- const targetNodeModules = path.join(outputPath, 'node_modules');
1608
+ // 构造文件信息对象
1609
+ const fileInfo = {
1610
+ path: file,
1611
+ destPath: destFile,
1612
+ content,
1613
+ isBinary,
1614
+ wasRendered,
1615
+ };
1281
1616
 
1282
- if (fs.existsSync(sourceNodeModules)) {
1283
- logger.info('\nCopying node_modules from pre-warmed template...');
1284
- logger.verbose(` From: ${sourceNodeModules}`);
1285
- logger.verbose(` To: ${targetNodeModules}`);
1617
+ // 执行文件渲染钩子
1618
+ const processedFileInfo = await executeFileRenderHook(
1619
+ templateConfig,
1620
+ fileInfo,
1621
+ context,
1622
+ );
1286
1623
 
1287
- const result = shelljs.exec(`cp -R "${sourceNodeModules}" "${targetNodeModules}"`, {
1288
- silent: true,
1289
- });
1624
+ // 如果返回 null,表示该文件被 hook 跳过
1625
+ if (processedFileInfo === null) {
1626
+ logger.verbose(' ⊘ Skipped by onFileRender hook');
1627
+ return null;
1628
+ }
1290
1629
 
1291
- if (result.stdout) {
1292
- process.stdout.write(result.stdout);
1293
- }
1630
+ return processedFileInfo;
1631
+ };
1294
1632
 
1295
- if (result.stderr) {
1296
- process.stderr.write(result.stderr);
1297
- }
1633
+ /**
1634
+ * 写入渲染后的文件到目标路径
1635
+ *
1636
+ * @param options - 写入选项
1637
+ */
1638
+ const writeRenderedFile = async (options
1298
1639
 
1299
- if (result.code === 0) {
1300
- logger.success('✓ node_modules copied successfully');
1640
+
1641
+
1642
+ ) => {
1643
+ const { fileInfo, srcPath, destPath } = options;
1644
+
1645
+ logger.verbose(` - Writing: ${fileInfo.destPath}`);
1646
+
1647
+ // 确保目标目录存在
1648
+ await ensureDir(path.dirname(destPath));
1649
+
1650
+ // 写入文件
1651
+ if (fileInfo.isBinary) {
1652
+ // 二进制文件:如果内容是原始 base64(未被 hook 修改),直接复制;否则从 base64 解码写入
1653
+ const buffer = await fs$1.readFile(srcPath);
1654
+ const originalContent = buffer.toString('base64');
1655
+
1656
+ if (fileInfo.content === originalContent) {
1657
+ await fs$1.copyFile(srcPath, destPath);
1658
+ logger.verbose(' ✓ Copied (binary)');
1301
1659
  } else {
1302
- logger.warn(
1303
- `Failed to copy node_modules: ${result.stderr || 'unknown error'}`,
1304
- );
1305
- logger.info('Will need to run pnpm install manually');
1660
+ const modifiedBuffer = Buffer.from(fileInfo.content, 'base64');
1661
+ await fs$1.writeFile(destPath, modifiedBuffer);
1662
+ logger.verbose(' ✓ Written (binary, modified by hook)');
1306
1663
  }
1664
+ } else {
1665
+ // 文本文件
1666
+ await fs$1.writeFile(destPath, fileInfo.content, 'utf-8');
1667
+ logger.verbose(' ✓ Rendered and written');
1307
1668
  }
1308
1669
  };
1670
+ // end_aigc
1671
+
1672
+ // ABOUTME: File conflict detection utilities for template processing
1673
+ // ABOUTME: Provides dry-run file collection and conflict checking
1674
+
1675
+
1676
+
1677
+
1678
+
1679
+
1309
1680
 
1681
+ // start_aigc
1310
1682
  /**
1311
- * 检查输出目录是否为空
1312
- * 注意:.git 目录会被忽略,允许在已初始化 git 的目录中创建项目
1683
+ * 收集所有将要写入的文件路径(dry-run 阶段)
1684
+ *
1685
+ * @param options - 收集选项
1686
+ * @returns 将要写入的文件路径列表
1687
+ */
1688
+ const collectFilesToRender = async (options
1689
+
1690
+
1691
+
1692
+
1693
+ ) => {
1694
+ const { files, templatePath, context, templateConfig } = options;
1695
+
1696
+ logger.verbose('\nDry-run: Collecting files to render...');
1697
+
1698
+ const fileInfos = await Promise.all(
1699
+ files.map(file =>
1700
+ prepareFileInfo({
1701
+ file,
1702
+ templatePath,
1703
+ context,
1704
+ templateConfig,
1705
+ }),
1706
+ ),
1707
+ );
1708
+
1709
+ // 过滤掉被 hook 跳过的文件,收集 destPath
1710
+ const filesToWrite = fileInfos
1711
+ .filter((info) => info !== null)
1712
+ .map(info => info.destPath);
1713
+
1714
+ logger.verbose(` - ${filesToWrite.length} files will be written`);
1715
+ logger.verbose(
1716
+ ` - ${fileInfos.length - filesToWrite.length} files skipped by hooks`,
1717
+ );
1718
+
1719
+ return filesToWrite;
1720
+ };
1721
+
1722
+ /**
1723
+ * 检测文件冲突
1313
1724
  *
1314
1725
  * @param outputPath - 输出目录路径
1315
- * @returns 是否为空
1726
+ * @param filesToWrite - 将要写入的文件路径列表
1727
+ * @returns 冲突的文件路径列表
1316
1728
  */
1317
- const isOutputDirEmpty = async (
1729
+ const detectFileConflicts = (
1318
1730
  outputPath,
1731
+ filesToWrite,
1319
1732
  ) => {
1320
- try {
1321
- const entries = await fs$1.readdir(outputPath);
1322
- // 过滤掉 .git 目录,允许在已初始化 git 的目录中创建项目
1323
- const filteredEntries = entries.filter(entry => entry !== '.git');
1324
- return filteredEntries.length === 0;
1325
- } catch (e) {
1326
- // 目录不存在,视为空
1327
- return true;
1733
+ logger.verbose('\nChecking for file conflicts...');
1734
+
1735
+ const conflicts = [];
1736
+
1737
+ for (const file of filesToWrite) {
1738
+ const fullPath = path.join(outputPath, file);
1739
+ if (fs.existsSync(fullPath)) {
1740
+ conflicts.push(file);
1741
+ logger.verbose(` ⚠ Conflict detected: ${file}`);
1742
+ }
1328
1743
  }
1744
+
1745
+ if (conflicts.length === 0) {
1746
+ logger.verbose(' ✓ No conflicts detected');
1747
+ } else {
1748
+ logger.verbose(` ⚠ ${conflicts.length} conflicts detected`);
1749
+ }
1750
+
1751
+ return conflicts;
1329
1752
  };
1753
+ // end_aigc
1754
+
1755
+ // ABOUTME: Main file processing orchestration for template rendering
1756
+ // ABOUTME: Coordinates file system, rendering, and conflict detection layers
1330
1757
 
1758
+
1759
+
1760
+ // start_aigc
1331
1761
  /**
1332
- * 验证输出目录
1762
+ * 处理单个文件(准备 + 写入)
1333
1763
  *
1334
- * @param outputPath - 输出目录路径
1335
- * @throws 如果目录不为空则抛出错误
1764
+ * @param options - 处理选项
1336
1765
  */
1337
- const validateOutputDir = async (outputPath) => {
1338
- const isEmpty = await isOutputDirEmpty(outputPath);
1339
- if (!isEmpty) {
1766
+ const processSingleFile = async (options
1767
+
1768
+
1769
+
1770
+
1771
+
1772
+ ) => {
1773
+ const { file, templatePath, outputPath, context, templateConfig } = options;
1774
+
1775
+ const srcPath = path.join(templatePath, file);
1776
+
1777
+ // 准备文件信息
1778
+ const processedFileInfo = await prepareFileInfo({
1779
+ file,
1780
+ templatePath,
1781
+ context,
1782
+ templateConfig,
1783
+ });
1784
+
1785
+ // 如果返回 null,跳过该文件
1786
+ if (processedFileInfo === null) {
1787
+ return;
1788
+ }
1789
+
1790
+ // 使用处理后的目标路径
1791
+ const finalDestPath = path.join(outputPath, processedFileInfo.destPath);
1792
+
1793
+ // 写入文件
1794
+ await writeRenderedFile({
1795
+ fileInfo: processedFileInfo,
1796
+ srcPath,
1797
+ destPath: finalDestPath,
1798
+ });
1799
+ };
1800
+
1801
+ /**
1802
+ * 复制并处理模板文件到目标目录
1803
+ *
1804
+ * 流程:
1805
+ * 1. 验证模板目录
1806
+ * 2. 扫描所有模板文件
1807
+ * 3. Dry-run:收集将要写入的文件列表(考虑 hooks 影响)
1808
+ * 4. 冲突检测:检查是否有文件会被覆盖
1809
+ * 5. 实际写入:渲染并写入所有文件
1810
+ * 6. 复制 node_modules(如果存在)
1811
+ *
1812
+ * @param options - 处理选项
1813
+ */
1814
+ const processTemplateFiles = async (options
1815
+
1816
+
1817
+
1818
+
1819
+ ) => {
1820
+ const { templatePath, outputPath, context, templateConfig } = options;
1821
+ logger.verbose('Processing template files:');
1822
+ logger.verbose(` - Template path: ${templatePath}`);
1823
+ logger.verbose(` - Output path: ${outputPath}`);
1824
+
1825
+ // 阶段 0: 验证模板目录是否存在
1826
+ try {
1827
+ const stat = await fs$1.stat(templatePath);
1828
+ logger.verbose(
1829
+ ` - Template path exists: ${stat.isDirectory() ? 'directory' : 'file'}`,
1830
+ );
1831
+ if (!stat.isDirectory()) {
1832
+ throw new Error(`Template path is not a directory: ${templatePath}`);
1833
+ }
1834
+ } catch (error) {
1835
+ logger.error(
1836
+ ` - Failed to access template path: ${error instanceof Error ? error.message : String(error)}`,
1837
+ );
1838
+ throw error;
1839
+ }
1840
+
1841
+ // 阶段 1: 扫描所有模板文件
1842
+ const files = await getAllFiles(templatePath);
1843
+
1844
+ logger.verbose(` - Found ${files.length} files to process`);
1845
+
1846
+ if (files.length === 0) {
1847
+ logger.warn(' - No files found in template directory!');
1848
+ return;
1849
+ }
1850
+
1851
+ // 阶段 2: Dry-run - 收集所有将要写入的文件
1852
+ const filesToWrite = await collectFilesToRender({
1853
+ files,
1854
+ templatePath,
1855
+ context,
1856
+ templateConfig,
1857
+ });
1858
+
1859
+ // 阶段 3: 冲突检测
1860
+ const conflicts = detectFileConflicts(outputPath, filesToWrite);
1861
+
1862
+ if (conflicts.length > 0) {
1863
+ // 有冲突,抛出详细的错误信息
1864
+ const conflictList = conflicts.map(f => ` - ${f}`).join('\n');
1340
1865
  throw new Error(
1341
- `Output directory is not empty: ${outputPath}\n` +
1342
- 'Please use an empty directory or remove existing files.',
1866
+ `File conflicts detected in output directory: ${outputPath}\n\n` +
1867
+ `The following files already exist and would be overwritten:\n${conflictList}\n\n` +
1868
+ 'Please remove these files or use a different output directory.',
1343
1869
  );
1344
1870
  }
1871
+
1872
+ // 阶段 4: 实际写入文件
1873
+ logger.verbose('\nWriting files...');
1874
+ await Promise.all(
1875
+ files.map(file =>
1876
+ processSingleFile({
1877
+ file,
1878
+ templatePath,
1879
+ outputPath,
1880
+ context,
1881
+ templateConfig,
1882
+ }),
1883
+ ),
1884
+ );
1885
+
1886
+ logger.verbose('✓ All files processed successfully');
1887
+
1888
+ // node_modules 将由 pnpm install 处理(利用缓存和硬链接机制)
1345
1889
  };
1890
+ // end_aigc
1346
1891
 
1347
1892
  /**
1348
1893
  * 模板引擎执行选项
@@ -1422,15 +1967,40 @@ const executeAfterRenderHook = async (
1422
1967
  }
1423
1968
  };
1424
1969
 
1970
+ /**
1971
+ * 执行完成钩子
1972
+ */
1973
+ const executeCompleteHook = async (
1974
+ templateConfig,
1975
+ context,
1976
+ outputPath,
1977
+ ) => {
1978
+ if (templateConfig.onComplete) {
1979
+ await templateConfig.onComplete(context, outputPath);
1980
+ }
1981
+ };
1982
+
1425
1983
  /**
1426
1984
  * 准备输出目录
1427
1985
  */
1428
- const prepareOutputDirectory = async (outputPath) => {
1986
+ const prepareOutputDirectory = (outputPath) => {
1429
1987
  const absolutePath = path.resolve(process.cwd(), outputPath);
1430
- await validateOutputDir(absolutePath);
1988
+ // 不再在这里验证目录是否为空,冲突检测已移至 processTemplateFiles 中
1431
1989
  return absolutePath;
1432
1990
  };
1433
1991
 
1992
+ /**
1993
+ * 模板引擎执行结果
1994
+ */
1995
+
1996
+
1997
+
1998
+
1999
+
2000
+
2001
+
2002
+
2003
+
1434
2004
  /**
1435
2005
  * 执行完整的模板渲染流程
1436
2006
  */
@@ -1454,18 +2024,27 @@ const execute = async (
1454
2024
  });
1455
2025
 
1456
2026
  // 5. 准备输出目录
1457
- const absoluteOutputPath = await prepareOutputDirectory(outputPath);
2027
+ const absoluteOutputPath = prepareOutputDirectory(outputPath);
1458
2028
 
1459
2029
  // 6. 处理模板文件
1460
- await processTemplateFiles(templatePath, absoluteOutputPath, context);
2030
+ await processTemplateFiles({
2031
+ templatePath,
2032
+ outputPath: absoluteOutputPath,
2033
+ context,
2034
+ templateConfig,
2035
+ });
1461
2036
 
1462
2037
  // 7. 执行 onAfterRender 钩子
1463
2038
  await executeAfterRenderHook(templateConfig, context, absoluteOutputPath);
1464
2039
 
1465
- return absoluteOutputPath;
2040
+ return {
2041
+ outputPath: absoluteOutputPath,
2042
+ templateConfig,
2043
+ context,
2044
+ };
1466
2045
  };
1467
2046
 
1468
- 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; }
2047
+ function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
1469
2048
  /**
1470
2049
  * 运行 pnpm install
1471
2050
  */
@@ -1568,45 +2147,33 @@ const runGitInit = (projectPath) => {
1568
2147
  };
1569
2148
 
1570
2149
  /**
1571
- * 运行开发服务器
2150
+ * 运行开发服务器(后台模式)
2151
+ * 启动后台子进程运行开发服务器,父进程可以直接退出
2152
+ * 使用 CLI 自己的 dev 命令(定义在 run.ts)而不是直接运行 npm run dev
1572
2153
  */
1573
- const runNpmDev = (projectPath) => {
1574
- logger.info('\nStarting development server...');
1575
- logger.info(`Executing: npm run dev in ${projectPath}`);
1576
- logger.info('Press Ctrl+C to stop the server\n');
1577
-
1578
- // 使用 async: true 异步执行,不阻塞进程
1579
- const child = shelljs.exec('npm run dev', {
1580
- cwd: projectPath,
1581
- async: true,
1582
- silent: true, // 手动处理输出以便显示详细信息
1583
- });
2154
+ const runDev = (projectPath) => {
2155
+ logger.info('\nStarting development server in background...');
1584
2156
 
1585
- if (child) {
1586
- // 输出 stdout
1587
- _optionalChain([child, 'access', _ => _.stdout, 'optionalAccess', _2 => _2.on, 'call', _3 => _3('data', (data) => {
1588
- process.stdout.write(data);
1589
- })]);
2157
+ // 获取当前 CLI 的可执行文件路径
2158
+ // process.argv[0] 是 node,process.argv[1] 是 CLI 入口文件
2159
+ const cliPath = process.argv[1];
1590
2160
 
1591
- // 输出 stderr
1592
- _optionalChain([child, 'access', _4 => _4.stderr, 'optionalAccess', _5 => _5.on, 'call', _6 => _6('data', (data) => {
1593
- process.stderr.write(data);
1594
- })]);
2161
+ logger.info(`Executing: ${cliPath} dev in ${projectPath}`);
1595
2162
 
1596
- // 监听错误
1597
- child.on('error', (error) => {
1598
- logger.error(`Failed to start dev server: ${error.message}`);
1599
- logger.error(`Error stack: ${error.stack}`);
1600
- });
2163
+ // 使用通用的后台执行函数启动开发服务器
2164
+ // 调用 CLI 自己的 dev 命令
2165
+ const pid = spawnDetached(process.argv[0], [cliPath, 'dev'], {
2166
+ cwd: projectPath,
2167
+ verbose: false, // 不输出额外的进程信息,由 logger 统一处理
2168
+ });
1601
2169
 
1602
- // 监听退出
1603
- child.on('exit', (code, signal) => {
1604
- if (code !== 0 && code !== null) {
1605
- logger.error(
1606
- `Dev server exited with code ${code}${signal ? ` and signal ${signal}` : ''}`,
1607
- );
1608
- }
1609
- });
2170
+ logger.success('Development server started in background!');
2171
+ if (pid) {
2172
+ logger.info(`Process ID: ${pid}`);
2173
+ logger.info(
2174
+ '\nThe dev server is running independently. You can close this terminal.',
2175
+ );
2176
+ logger.info(`To stop the server later, use: kill ${pid}`);
1610
2177
  }
1611
2178
  };
1612
2179
 
@@ -1637,32 +2204,37 @@ const executeInit = async (
1637
2204
  logger.info(`Initializing project with template: ${templateName}`);
1638
2205
  timer.logPhase('Initialization');
1639
2206
 
1640
- // 执行模板引擎,返回绝对路径
1641
- const absoluteOutputPath = await execute({
2207
+ // 执行模板引擎,返回结果对象
2208
+ const result = await execute({
1642
2209
  templateName,
1643
2210
  outputPath,
1644
2211
  command,
1645
2212
  });
2213
+ const { outputPath: absoluteOutputPath, templateConfig, context } = result;
1646
2214
 
1647
2215
  timer.logPhase('Template engine execution');
1648
2216
  logger.success('Project created successfully!');
1649
2217
 
1650
- // 如果没有跳过安装,检查是否需要运行 pnpm install
1651
- if (!skipInstall) {
1652
- const nodeModulesPath = path.join(absoluteOutputPath, 'node_modules');
1653
- const hasNodeModules = fs.existsSync(nodeModulesPath);
2218
+ // 检查是否存在 package.json
2219
+ const packageJsonPath = path.join(absoluteOutputPath, 'package.json');
2220
+ const hasPackageJson = fs.existsSync(packageJsonPath);
1654
2221
 
1655
- if (hasNodeModules) {
1656
- logger.info(
1657
- '\n💡 Using pre-warmed node_modules, skipping pnpm install',
1658
- );
1659
- timer.logPhase('Node modules (pre-warmed)');
1660
- } else {
2222
+ // 安装依赖(始终使用 pnpm install,利用缓存机制)
2223
+ if (!skipInstall) {
2224
+ if (hasPackageJson) {
1661
2225
  runPnpmInstall(absoluteOutputPath);
1662
2226
  timer.logPhase('Dependencies installation');
2227
+ } else {
2228
+ logger.info(
2229
+ '\n💡 No package.json found, skipping dependency installation',
2230
+ );
1663
2231
  }
1664
2232
  }
1665
2233
 
2234
+ // 执行 onComplete 钩子(在 pnpm install 之后)
2235
+ await executeCompleteHook(templateConfig, context, absoluteOutputPath);
2236
+ timer.logPhase('Complete hook execution');
2237
+
1666
2238
  // 如果没有跳过 git,则初始化 git 仓库
1667
2239
  if (!skipGit) {
1668
2240
  runGitInit(absoluteOutputPath);
@@ -1671,13 +2243,13 @@ const executeInit = async (
1671
2243
 
1672
2244
  // 如果没有跳过 dev,则启动开发服务器
1673
2245
  if (!skipDev) {
1674
- runNpmDev(absoluteOutputPath);
2246
+ runDev(absoluteOutputPath);
1675
2247
  timer.logPhase('Dev server startup');
1676
2248
  } else {
1677
2249
  // 只有跳过 dev 时才显示 Next steps
1678
2250
  logger.info('\nNext steps:');
1679
2251
  logger.info(` cd ${outputPath}`);
1680
- if (skipInstall) {
2252
+ if (skipInstall && hasPackageJson) {
1681
2253
  logger.info(' pnpm install');
1682
2254
  }
1683
2255
  if (skipGit) {
@@ -1685,7 +2257,7 @@ const executeInit = async (
1685
2257
  ' git init && git add . && git commit -m "initial commit"',
1686
2258
  );
1687
2259
  }
1688
- logger.info(' npm run dev');
2260
+ logger.info(' coze dev');
1689
2261
  }
1690
2262
 
1691
2263
  // 输出总耗时
@@ -1701,7 +2273,7 @@ const executeInit = async (
1701
2273
  /**
1702
2274
  * 注册 init 命令到 program
1703
2275
  */
1704
- const registerCommand = program => {
2276
+ const registerCommand$1 = program => {
1705
2277
  program
1706
2278
  .command('init')
1707
2279
  .description('Initialize a new project from a template')
@@ -1719,14 +2291,312 @@ const registerCommand = program => {
1719
2291
  });
1720
2292
  };
1721
2293
 
1722
- var version = "0.0.1-alpha.a1ca15";
2294
+ // ABOUTME: This file implements the update command for coze CLI
2295
+ // ABOUTME: It wraps pnpm update/install to update package dependencies with logging support
2296
+
2297
+
2298
+
2299
+
2300
+ /**
2301
+ * 日志文件名常量
2302
+ */
2303
+ const LOG_FILE_NAME = 'update.log';
2304
+
2305
+ /**
2306
+ * 获取日志目录
2307
+ * 优先使用环境变量 COZE_LOG_DIR,否则使用 ~/.coze-logs
2308
+ */
2309
+ const getLogDir = () =>
2310
+ process.env.COZE_LOG_DIR || path.join(os.homedir(), '.coze-logs');
2311
+
2312
+ /**
2313
+ * 解析日志文件路径
2314
+ * - 如果是绝对路径,直接使用
2315
+ * - 如果是相对路径,基于 getLogDir() + 相对路径
2316
+ * - 如果为空,使用 getLogDir() + LOG_FILE_NAME
2317
+ */
2318
+ const resolveLogFilePath = (logFile) => {
2319
+ if (!logFile) {
2320
+ return path.join(getLogDir(), LOG_FILE_NAME);
2321
+ }
2322
+
2323
+ if (path.isAbsolute(logFile)) {
2324
+ return logFile;
2325
+ }
2326
+
2327
+ return path.join(getLogDir(), logFile);
2328
+ };
2329
+
2330
+ /**
2331
+ * 创建日志写入流
2332
+ */
2333
+ const createLogStream = (logFilePath) => {
2334
+ const logDir = path.dirname(logFilePath);
2335
+
2336
+ // 确保日志目录存在
2337
+ if (!fs.existsSync(logDir)) {
2338
+ fs.mkdirSync(logDir, { recursive: true });
2339
+ }
2340
+
2341
+ // 使用 'w' 标志覆盖之前的日志
2342
+ return fs.createWriteStream(logFilePath, { flags: 'w' });
2343
+ };
2344
+
2345
+ /**
2346
+ * 格式化时间戳
2347
+ */
2348
+ const formatTimestamp = () => {
2349
+ const now = new Date();
2350
+ return now.toISOString();
2351
+ };
2352
+
2353
+ /**
2354
+ * 写入带时间戳的日志
2355
+ */
2356
+ const writeLogWithTimestamp = (stream, message) => {
2357
+ const timestamp = formatTimestamp();
2358
+ const lines = message.split('\n');
2359
+ lines.forEach(line => {
2360
+ if (line) {
2361
+ stream.write(`[${timestamp}] ${line}\n`);
2362
+ } else {
2363
+ stream.write('\n');
2364
+ }
2365
+ });
2366
+ // 确保数据写入磁盘
2367
+ stream.uncork();
2368
+ };
2369
+
2370
+ /**
2371
+ * 同时输出到控制台和日志文件
2372
+ */
2373
+ const logWithFile = (
2374
+ stream,
2375
+ level,
2376
+ message,
2377
+ ) => {
2378
+ // 输出到控制台
2379
+ switch (level) {
2380
+ case 'info':
2381
+ logger.info(message);
2382
+ break;
2383
+ case 'success':
2384
+ logger.success(message);
2385
+ break;
2386
+ case 'error':
2387
+ logger.error(message);
2388
+ break;
2389
+ default:
2390
+ logger.info(message);
2391
+ break;
2392
+ }
2393
+
2394
+ // 写入日志文件(带时间戳)
2395
+ writeLogWithTimestamp(stream, `[${level.toUpperCase()}] ${message}`);
2396
+ };
2397
+
2398
+ // start_aigc
2399
+ /**
2400
+ * 构建 pnpm add 命令
2401
+ */
2402
+ const buildPnpmCommand = (
2403
+ packageName,
2404
+ options
2405
+
2406
+
2407
+
2408
+
2409
+ ,
2410
+ ) => {
2411
+ const { global, version, registry, extraArgs } = options;
2412
+
2413
+ const parts = ['pnpm', 'add'];
2414
+
2415
+ // 添加全局标记
2416
+ if (global) {
2417
+ parts.push('-g');
2418
+ }
2419
+
2420
+ // 添加包名和版本
2421
+ if (version && version !== 'latest') {
2422
+ parts.push(`${packageName}@${version}`);
2423
+ } else {
2424
+ parts.push(`${packageName}@latest`);
2425
+ }
2426
+
2427
+ // 添加 registry
2428
+ if (registry) {
2429
+ parts.push(`--registry=${registry}`);
2430
+ }
2431
+
2432
+ // 添加额外参数
2433
+ if (extraArgs.length > 0) {
2434
+ parts.push(...extraArgs);
2435
+ }
2436
+
2437
+ return parts.join(' ');
2438
+ };
2439
+ // end_aigc
2440
+
2441
+ // start_aigc
2442
+ /**
2443
+ * 执行 update 命令的内部实现
2444
+ */
2445
+ const executeUpdate = (
2446
+ packageName,
2447
+ options
2448
+
2449
+
2450
+
2451
+
2452
+
2453
+
2454
+ ,
2455
+ ) => {
2456
+ let logStream = null;
2457
+
2458
+ try {
2459
+ const { global, cwd, version, registry, logFile, extraArgs } = options;
2460
+
2461
+ // 准备日志
2462
+ const logFilePath = resolveLogFilePath(logFile);
2463
+
2464
+ // 调试:确认日志路径
2465
+ logger.info(`Log file path resolved to: ${logFilePath}`);
2466
+
2467
+ logStream = createLogStream(logFilePath);
2468
+
2469
+ // 调试:确认流已创建
2470
+ logger.info('Log stream created successfully');
2471
+
2472
+ logWithFile(logStream, 'info', `Updating package: ${packageName}`);
2473
+
2474
+ // 构建命令
2475
+ const command = buildPnpmCommand(packageName, {
2476
+ global,
2477
+ version,
2478
+ registry,
2479
+ extraArgs,
2480
+ });
2481
+
2482
+ // 确定工作目录
2483
+ const workingDir = cwd
2484
+ ? path.isAbsolute(cwd)
2485
+ ? cwd
2486
+ : path.join(process.cwd(), cwd)
2487
+ : process.cwd();
2488
+
2489
+ logWithFile(logStream, 'info', `Executing: ${command}`);
2490
+ logWithFile(logStream, 'info', `Working directory: ${workingDir}`);
2491
+ logWithFile(logStream, 'info', `Log file: ${logFilePath}`);
2492
+
2493
+ // 记录命令开始时间
2494
+ writeLogWithTimestamp(logStream, '--- Command execution started ---');
2495
+
2496
+ // 同步执行命令
2497
+ const result = shelljs.exec(command, {
2498
+ cwd: workingDir,
2499
+ silent: true, // 使用 silent 来捕获输出
2500
+ });
2501
+
2502
+ // 将输出写入控制台和日志文件(带时间戳)
2503
+ if (result.stdout) {
2504
+ process.stdout.write(result.stdout);
2505
+ writeLogWithTimestamp(logStream, result.stdout.trim());
2506
+ }
2507
+
2508
+ if (result.stderr) {
2509
+ process.stderr.write(result.stderr);
2510
+ writeLogWithTimestamp(logStream, result.stderr.trim());
2511
+ }
2512
+
2513
+ // 记录命令结束时间
2514
+ writeLogWithTimestamp(logStream, '--- Command execution ended ---');
2515
+
2516
+ // 检查执行结果并记录到日志
2517
+ if (result.code === 0) {
2518
+ logWithFile(logStream, 'success', 'Package updated successfully');
2519
+ logWithFile(logStream, 'info', `Log file: ${logFilePath}`);
2520
+ } else {
2521
+ logWithFile(
2522
+ logStream,
2523
+ 'error',
2524
+ `Command exited with code ${result.code}`,
2525
+ );
2526
+ logWithFile(
2527
+ logStream,
2528
+ 'error',
2529
+ `Check log file for details: ${logFilePath}`,
2530
+ );
2531
+ }
2532
+
2533
+ // 关闭日志流并等待写入完成
2534
+ logStream.end(() => {
2535
+ // 流关闭后再退出进程
2536
+ if (result.code !== 0) {
2537
+ process.exit(result.code || 1);
2538
+ }
2539
+ });
2540
+ } catch (error) {
2541
+ logger.error('Failed to update package:');
2542
+ logger.error(error instanceof Error ? error.message : String(error));
2543
+
2544
+ // 写入错误到日志文件
2545
+ if (logStream) {
2546
+ writeLogWithTimestamp(
2547
+ logStream,
2548
+ `[ERROR] ${error instanceof Error ? error.message : String(error)}`,
2549
+ );
2550
+ // 等待流关闭后再退出
2551
+ logStream.end(() => {
2552
+ process.exit(1);
2553
+ });
2554
+ } else {
2555
+ process.exit(1);
2556
+ }
2557
+ }
2558
+ };
2559
+ // end_aigc
2560
+
2561
+ /**
2562
+ * 注册 update 命令到 program
2563
+ */
2564
+ const registerCommand = program => {
2565
+ program
2566
+ .command('update <package>')
2567
+ .description('Update a package dependency')
2568
+ .option('-g, --global', 'Update package globally', false)
2569
+ .option('-c, --cwd <path>', 'Working directory for the update')
2570
+ .option(
2571
+ '--to <version>',
2572
+ 'Version to update to (default: latest)',
2573
+ 'latest',
2574
+ )
2575
+ .option('--registry <url>', 'Registry URL to use for the update')
2576
+ .option('--log-file <path>', 'Log file path')
2577
+ .allowUnknownOption() // 允许透传参数给 pnpm
2578
+ .action((packageName, options, command) => {
2579
+ // 收集所有未知选项作为额外参数
2580
+ const extraArgs = command.args.slice(1);
2581
+
2582
+ executeUpdate(packageName, {
2583
+ ...options,
2584
+ version: options.to, // 将 --to 映射到 version
2585
+ extraArgs,
2586
+ });
2587
+ });
2588
+ };
2589
+
2590
+ var version = "0.0.1-alpha.a2a210";
1723
2591
  var packageJson = {
1724
2592
  version: version};
1725
2593
 
1726
2594
  const commands = [
1727
- registerCommand,
1728
2595
  registerCommand$1,
1729
2596
  registerCommand$2,
2597
+ registerCommand$4,
2598
+ registerCommand$3,
2599
+ registerCommand,
1730
2600
  ];
1731
2601
 
1732
2602
  const main = () => {