@coze-arch/cli 0.0.1-alpha.d260be → 0.0.1-alpha.d4acfb

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 (122) hide show
  1. package/lib/__templates__/expo/.cozeproj/scripts/dev_run.sh +25 -16
  2. package/lib/__templates__/expo/.cozeproj/scripts/server_dev_run.sh +9 -8
  3. package/lib/__templates__/expo/README.md +4 -23
  4. package/lib/__templates__/expo/client/app/+not-found.tsx +30 -0
  5. package/lib/__templates__/expo/client/app/_layout.tsx +11 -8
  6. package/lib/__templates__/expo/client/app.config.ts +2 -2
  7. package/lib/__templates__/expo/client/components/Screen.tsx +1 -17
  8. package/lib/__templates__/expo/client/components/ThemedView.tsx +1 -2
  9. package/lib/__templates__/expo/client/constants/theme.ts +21 -698
  10. package/lib/__templates__/expo/client/eslint.config.mjs +33 -10
  11. package/lib/__templates__/expo/client/hooks/{useColorScheme.ts → useColorScheme.tsx} +20 -6
  12. package/lib/__templates__/expo/client/hooks/useSafeRouter.ts +152 -0
  13. package/lib/__templates__/expo/client/hooks/useTheme.ts +26 -6
  14. package/lib/__templates__/expo/client/metro.config.js +3 -0
  15. package/lib/__templates__/expo/client/package.json +36 -34
  16. package/lib/__templates__/expo/client/screens/demo/index.tsx +3 -3
  17. package/lib/__templates__/expo/client/scripts/install-missing-deps.js +1 -0
  18. package/lib/__templates__/expo/client/utils/index.ts +22 -0
  19. package/lib/__templates__/expo/eslint-plugins/fontawesome6/index.js +9 -0
  20. package/lib/__templates__/expo/eslint-plugins/fontawesome6/names.js +1889 -0
  21. package/lib/__templates__/expo/eslint-plugins/fontawesome6/rule.js +174 -0
  22. package/lib/__templates__/expo/eslint-plugins/fontawesome6/v5-only-names.js +388 -0
  23. package/lib/__templates__/expo/eslint-plugins/react-native/index.js +9 -0
  24. package/lib/__templates__/expo/eslint-plugins/react-native/rule.js +64 -0
  25. package/lib/__templates__/expo/eslint-plugins/reanimated/index.js +9 -0
  26. package/lib/__templates__/expo/eslint-plugins/reanimated/rule.js +88 -0
  27. package/lib/__templates__/expo/package.json +3 -0
  28. package/lib/__templates__/expo/patches/expo@54.0.33.patch +45 -0
  29. package/lib/__templates__/expo/pnpm-lock.yaml +1318 -2636
  30. package/lib/__templates__/expo/server/build.js +21 -0
  31. package/lib/__templates__/expo/server/package.json +9 -7
  32. package/lib/__templates__/expo/server/src/index.ts +3 -1
  33. package/lib/__templates__/expo/server/tsconfig.json +2 -2
  34. package/lib/__templates__/expo/template.config.js +56 -0
  35. package/lib/__templates__/native-static/.coze +11 -0
  36. package/lib/__templates__/native-static/index.html +33 -0
  37. package/lib/__templates__/native-static/styles/main.css +136 -0
  38. package/lib/__templates__/native-static/template.config.js +22 -0
  39. package/lib/__templates__/nextjs/.babelrc +15 -0
  40. package/lib/__templates__/nextjs/next.config.ts +1 -1
  41. package/lib/__templates__/nextjs/package.json +11 -1
  42. package/lib/__templates__/nextjs/pnpm-lock.yaml +2701 -1813
  43. package/lib/__templates__/nextjs/src/app/layout.tsx +5 -3
  44. package/lib/__templates__/nextjs/src/app/page.tsx +18 -60
  45. package/lib/__templates__/nextjs/template.config.js +47 -12
  46. package/lib/__templates__/taro/.coze +14 -0
  47. package/lib/__templates__/taro/.cozeproj/scripts/deploy_build.sh +19 -0
  48. package/lib/__templates__/taro/.cozeproj/scripts/deploy_run.sh +14 -0
  49. package/lib/__templates__/taro/.cozeproj/scripts/dev_build.sh +2 -0
  50. package/lib/__templates__/taro/.cozeproj/scripts/dev_run.sh +151 -0
  51. package/lib/__templates__/taro/.cozeproj/scripts/init_env.sh +5 -0
  52. package/lib/__templates__/taro/.cozeproj/scripts/pack.sh +24 -0
  53. package/lib/__templates__/taro/README.md +751 -0
  54. package/lib/__templates__/taro/_gitignore +40 -0
  55. package/lib/__templates__/taro/_npmrc +18 -0
  56. package/lib/__templates__/taro/babel.config.js +12 -0
  57. package/lib/__templates__/taro/config/dev.ts +9 -0
  58. package/lib/__templates__/taro/config/index.ts +223 -0
  59. package/lib/__templates__/taro/config/prod.ts +34 -0
  60. package/lib/__templates__/taro/eslint.config.mjs +80 -0
  61. package/lib/__templates__/taro/key/private.appid.key +0 -0
  62. package/lib/__templates__/taro/package.json +107 -0
  63. package/lib/__templates__/taro/patches/@tarojs__plugin-mini-ci@4.1.9.patch +30 -0
  64. package/lib/__templates__/taro/pnpm-lock.yaml +23100 -0
  65. package/lib/__templates__/taro/pnpm-workspace.yaml +2 -0
  66. package/lib/__templates__/taro/project.config.json +15 -0
  67. package/lib/__templates__/taro/server/nest-cli.json +10 -0
  68. package/lib/__templates__/taro/server/package.json +40 -0
  69. package/lib/__templates__/taro/server/src/app.controller.ts +23 -0
  70. package/lib/__templates__/taro/server/src/app.module.ts +10 -0
  71. package/lib/__templates__/taro/server/src/app.service.ts +8 -0
  72. package/lib/__templates__/taro/server/src/interceptors/http-status.interceptor.ts +23 -0
  73. package/lib/__templates__/taro/server/src/main.ts +49 -0
  74. package/lib/__templates__/taro/server/tsconfig.json +24 -0
  75. package/lib/__templates__/taro/src/app.config.ts +11 -0
  76. package/lib/__templates__/taro/src/app.css +52 -0
  77. package/lib/__templates__/taro/src/app.tsx +9 -0
  78. package/lib/__templates__/taro/src/index.html +39 -0
  79. package/lib/__templates__/taro/src/network.ts +39 -0
  80. package/lib/__templates__/taro/src/pages/index/index.config.ts +3 -0
  81. package/lib/__templates__/taro/src/pages/index/index.css +1 -0
  82. package/lib/__templates__/taro/src/pages/index/index.tsx +33 -0
  83. package/lib/__templates__/taro/src/presets/dev-debug.ts +23 -0
  84. package/lib/__templates__/taro/src/presets/h5-container.tsx +15 -0
  85. package/lib/__templates__/taro/src/presets/h5-navbar.tsx +201 -0
  86. package/lib/__templates__/taro/src/presets/h5-styles.ts +142 -0
  87. package/lib/__templates__/taro/src/presets/index.tsx +18 -0
  88. package/lib/__templates__/taro/stylelint.config.mjs +4 -0
  89. package/lib/__templates__/taro/template.config.js +68 -0
  90. package/lib/__templates__/taro/tsconfig.json +29 -0
  91. package/lib/__templates__/taro/types/global.d.ts +32 -0
  92. package/lib/__templates__/templates.json +75 -0
  93. package/lib/__templates__/vite/package.json +10 -1
  94. package/lib/__templates__/vite/pnpm-lock.yaml +350 -2341
  95. package/lib/__templates__/vite/src/main.ts +17 -47
  96. package/lib/__templates__/vite/template.config.js +47 -10
  97. package/lib/__templates__/vite/vite.config.ts +1 -0
  98. package/lib/__templates__/vite-vue/.coze +12 -0
  99. package/lib/__templates__/vite-vue/README.md +451 -0
  100. package/lib/__templates__/vite-vue/_gitignore +66 -0
  101. package/lib/__templates__/vite-vue/_npmrc +23 -0
  102. package/lib/__templates__/vite-vue/eslint.config.mjs +9 -0
  103. package/lib/__templates__/vite-vue/index.html +13 -0
  104. package/lib/__templates__/vite-vue/package.json +37 -0
  105. package/lib/__templates__/vite-vue/pnpm-lock.yaml +3151 -0
  106. package/lib/__templates__/vite-vue/postcss.config.mjs +6 -0
  107. package/lib/__templates__/vite-vue/scripts/build.sh +14 -0
  108. package/lib/__templates__/vite-vue/scripts/dev.sh +32 -0
  109. package/lib/__templates__/vite-vue/scripts/prepare.sh +9 -0
  110. package/lib/__templates__/vite-vue/scripts/start.sh +15 -0
  111. package/lib/__templates__/vite-vue/src/App.vue +6 -0
  112. package/lib/__templates__/vite-vue/src/index.css +29 -0
  113. package/lib/__templates__/vite-vue/src/main.ts +8 -0
  114. package/lib/__templates__/vite-vue/src/router/index.ts +17 -0
  115. package/lib/__templates__/vite-vue/src/views/Home.vue +37 -0
  116. package/lib/__templates__/vite-vue/src/vite-env.d.ts +8 -0
  117. package/lib/__templates__/vite-vue/tailwind.config.js +9 -0
  118. package/lib/__templates__/vite-vue/template.config.js +128 -0
  119. package/lib/__templates__/vite-vue/tsconfig.json +17 -0
  120. package/lib/__templates__/vite-vue/vite.config.ts +28 -0
  121. package/lib/cli.js +838 -130
  122. package/package.json +2 -1
package/lib/cli.js CHANGED
@@ -4,12 +4,14 @@
4
4
  var commander = require('commander');
5
5
  var path = require('path');
6
6
  var fs = require('fs');
7
+ var node_path = require('node:path');
8
+ var node_fs = require('node:fs');
7
9
  var shelljs = require('shelljs');
8
10
  var perf_hooks = require('perf_hooks');
9
11
  var fs$1 = require('fs/promises');
10
12
  var os = require('os');
11
- var toml = require('@iarna/toml');
12
13
  var jsYaml = require('js-yaml');
14
+ var toml = require('@iarna/toml');
13
15
  var child_process = require('child_process');
14
16
  var addFormats = require('ajv-formats');
15
17
  var Ajv = require('ajv');
@@ -481,6 +483,14 @@ const warmupTemplate = (templatePath, templateName) => {
481
483
  logger.info(`\nWarming up template: ${templateName}`);
482
484
  logger.info(` Path: ${templatePath}`);
483
485
 
486
+ // 检查是否存在 package.json
487
+ const packageJsonPath = node_path.join(templatePath, 'package.json');
488
+ // eslint-disable-next-line security/detect-non-literal-fs-filename
489
+ if (!node_fs.existsSync(packageJsonPath)) {
490
+ logger.info(` ⊘ Skipping ${templateName} (no package.json found)`);
491
+ return;
492
+ }
493
+
484
494
  const result = shelljs.exec('pnpm install', {
485
495
  cwd: templatePath,
486
496
  silent: true,
@@ -514,13 +524,7 @@ const warmupTemplate = (templatePath, templateName) => {
514
524
  /**
515
525
  * 执行 warmup 命令的内部实现
516
526
  */
517
- const executeWarmup = async (
518
- options
519
-
520
- ,
521
-
522
- command,
523
- ) => {
527
+ const executeWarmup = async (options) => {
524
528
  const timer = new TimeTracker();
525
529
 
526
530
  try {
@@ -585,12 +589,12 @@ const executeWarmup = async (
585
589
  /**
586
590
  * 注册 warmup 命令到 program
587
591
  */
588
- const registerCommand$2 = program => {
592
+ const registerCommand$4 = program => {
589
593
  program
590
594
  .command('warmup')
591
595
  .description('Pre-install dependencies for templates to speed up init')
592
596
  .option('-t, --template <name>', 'Warmup a specific template only')
593
- .action(async (options, command) => {
597
+ .action(async options => {
594
598
  await executeWarmup(options);
595
599
  });
596
600
  };
@@ -727,7 +731,7 @@ const parseConfigContent = (content) => {
727
731
  return config ;
728
732
  } catch (error) {
729
733
  // TOML 解析失败,继续尝试其他格式
730
- // eslint-disable-next-line no-console
734
+
731
735
  console.debug('TOML parse failed:', error);
732
736
  }
733
737
 
@@ -739,7 +743,7 @@ const parseConfigContent = (content) => {
739
743
  }
740
744
  } catch (error) {
741
745
  // YAML 解析失败,继续尝试其他格式
742
- // eslint-disable-next-line no-console
746
+
743
747
  console.debug('YAML parse failed:', error);
744
748
  }
745
749
 
@@ -823,18 +827,242 @@ const getCommandConfig = (
823
827
  return commandConfig;
824
828
  };
825
829
 
826
- 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; }
830
+ // ABOUTME: Fix rule to comment out problematic outputFileTracingRoot config in Next.js projects
831
+ // ABOUTME: This config can cause issues in monorepo environments and should be removed
832
+
833
+
834
+
835
+
836
+ /**
837
+ * 检查是否为 Next.js 项目
838
+ */
839
+ const isNextProject = (projectFolder) => {
840
+ const packageJsonPath = path.join(projectFolder, 'package.json');
841
+
842
+ if (!fs.existsSync(packageJsonPath)) {
843
+ return false;
844
+ }
845
+
846
+ try {
847
+ // eslint-disable-next-line no-restricted-syntax
848
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
849
+ const deps = {
850
+ ...packageJson.dependencies,
851
+ ...packageJson.devDependencies,
852
+ };
853
+
854
+ return 'next' in deps;
855
+ } catch (e) {
856
+ return false;
857
+ }
858
+ };
859
+
860
+ /**
861
+ * 查找 Next.js 配置文件
862
+ */
863
+ const findNextConfigFile = (projectFolder) => {
864
+ const possibleConfigs = [
865
+ 'next.config.ts',
866
+ 'next.config.js',
867
+ 'next.config.mjs',
868
+ ];
869
+
870
+ for (const configFile of possibleConfigs) {
871
+ const configPath = path.join(projectFolder, configFile);
872
+ if (fs.existsSync(configPath)) {
873
+ return configPath;
874
+ }
875
+ }
876
+
877
+ return null;
878
+ };
879
+
880
+ /**
881
+ * 注释掉 outputFileTracingRoot 配置
882
+ */
883
+ const commentOutOutputTracingRoot = (
884
+ configPath,
885
+ ) => {
886
+ let content = fs.readFileSync(configPath, 'utf-8');
887
+ let modified = false;
888
+ let originalLine = null;
889
+
890
+ // 匹配包含 outputFileTracingRoot 的行(尚未被注释的)
891
+ // 支持 path.resolve(...) 后面有空格,然后可选逗号
892
+ // 只匹配单行配置,不匹配跨多行的配置
893
+ const pattern =
894
+ /^(\s*)(outputFileTracingRoot:\s*path\.resolve\([^\n\r)]+\)\s*,?)\s*$/gm;
895
+
896
+ const matches = content.match(pattern);
897
+
898
+ if (matches && matches.length > 0) {
899
+ originalLine = matches[0].trim();
900
+
901
+ // 在匹配的行前添加 // 注释
902
+ content = content.replace(pattern, '$1// $2');
903
+ modified = true;
904
+
905
+ fs.writeFileSync(configPath, content, 'utf-8');
906
+ }
907
+
908
+ return { modified, originalLine };
909
+ };
910
+
911
+ // start_aigc
912
+ /**
913
+ * Fix 规则:注释掉 Next.js 项目中的 outputFileTracingRoot 配置
914
+ * 这个配置在 monorepo 环境中可能会导致问题
915
+ */
916
+ const fixNextOutputTracingRoot = context => {
917
+ const ruleName = 'next-output-tracing-root';
918
+
919
+ // 1. 检查是否为 Next.js 项目
920
+ if (!isNextProject(context.projectFolder)) {
921
+ return {
922
+ ruleName,
923
+ applied: false,
924
+ message: 'Not a Next.js project, skipping',
925
+ };
926
+ }
927
+
928
+ // 2. 查找 Next.js 配置文件
929
+ const configPath = findNextConfigFile(context.projectFolder);
930
+
931
+ if (!configPath) {
932
+ return {
933
+ ruleName,
934
+ applied: false,
935
+ message: 'Next.js config file not found, skipping',
936
+ };
937
+ }
938
+
939
+ // 3. 注释掉 outputFileTracingRoot 配置
940
+ const { modified, originalLine } = commentOutOutputTracingRoot(configPath);
941
+
942
+ if (modified && originalLine) {
943
+ logger.success(
944
+ `Commented out outputFileTracingRoot in ${configPath.split('/').pop()}`,
945
+ );
946
+ logger.info(` Original: ${originalLine}`);
947
+
948
+ return {
949
+ ruleName,
950
+ applied: true,
951
+ message: `Successfully commented out: ${originalLine}`,
952
+ };
953
+ }
954
+
955
+ return {
956
+ ruleName,
957
+ applied: false,
958
+ message: 'No outputFileTracingRoot config found, skipping',
959
+ };
960
+ };
961
+ // end_aigc
827
962
 
963
+ /**
964
+ * 所有修复规则的数组
965
+ * 按顺序执行,新增规则直接添加到数组中
966
+ */
967
+ const rules = [
968
+ // Next.js related fixes
969
+ fixNextOutputTracingRoot,
970
+
971
+ // Add more rules here
972
+ ] ;
973
+
974
+ // ABOUTME: Fix command for resolving legacy issues from previous project versions
975
+ // ABOUTME: Applies a series of fix rules defined in the fix-rules directory
976
+
977
+
978
+ // start_aigc
979
+ /**
980
+ * 执行 fix 命令的内部实现
981
+ */
982
+ const executeFix = async (options = {}) => {
983
+ try {
984
+ const cwd = process.cwd();
985
+ const projectFolder = options.directory
986
+ ? path.resolve(cwd, options.directory)
987
+ : cwd;
988
+
989
+ logger.info(`Running fix command on: ${projectFolder}`);
990
+ logger.info(`Found ${rules.length} fix rule(s) to apply\n`);
991
+
992
+ const context = {
993
+ cwd,
994
+ projectFolder,
995
+ };
996
+
997
+ let appliedCount = 0;
998
+ let skippedCount = 0;
999
+
1000
+ // 依次执行所有修复规则
1001
+ for (const rule of rules) {
1002
+ try {
1003
+ const result = await Promise.resolve(rule(context));
1004
+
1005
+ if (result.applied) {
1006
+ appliedCount++;
1007
+ logger.success(`✓ ${result.ruleName}: ${result.message}`);
1008
+ } else {
1009
+ skippedCount++;
1010
+ logger.info(`○ ${result.ruleName}: ${result.message}`);
1011
+ }
1012
+ } catch (error) {
1013
+ logger.error(
1014
+ `✗ Rule execution failed: ${error instanceof Error ? error.message : String(error)}`,
1015
+ );
1016
+ }
1017
+ }
1018
+
1019
+ // 输出汇总信息
1020
+ logger.info(
1021
+ `\nSummary: ${appliedCount} fixed, ${skippedCount} skipped, ${rules.length} total`,
1022
+ );
1023
+
1024
+ if (appliedCount > 0) {
1025
+ logger.success('\nFixes applied successfully!');
1026
+ } else {
1027
+ logger.info('\nNo fixes needed');
1028
+ }
1029
+ } catch (error) {
1030
+ logger.error('Failed to run fix command:');
1031
+ logger.error(error instanceof Error ? error.message : String(error));
1032
+ process.exit(1);
1033
+ }
1034
+ };
1035
+ // end_aigc
1036
+
1037
+ /**
1038
+ * 注册 fix 命令到 program
1039
+ */
1040
+ const registerCommand$3 = program => {
1041
+ program
1042
+ .command('fix')
1043
+ .description(
1044
+ 'Fix legacy issues from previous versions (e.g., problematic configs)',
1045
+ )
1046
+ .argument(
1047
+ '[directory]',
1048
+ 'Target directory to fix (defaults to current directory)',
1049
+ )
1050
+ .action(async (directory) => {
1051
+ await executeFix({ directory });
1052
+ });
1053
+ };
1054
+
1055
+ 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; }
828
1056
  /**
829
1057
  * 日志文件名常量
830
1058
  */
831
- const LOG_FILE_NAME = 'dev.log';
1059
+ const LOG_FILE_NAME$1 = 'dev.log';
832
1060
 
833
1061
  /**
834
1062
  * 获取日志目录
835
1063
  * 优先使用环境变量 COZE_LOG_DIR,否则使用 ~/.coze-logs
836
1064
  */
837
- const getLogDir = () =>
1065
+ const getLogDir$1 = () =>
838
1066
  process.env.COZE_LOG_DIR || path.join(os.homedir(), '.coze-logs');
839
1067
 
840
1068
  /**
@@ -843,22 +1071,22 @@ const getLogDir = () =>
843
1071
  * - 如果是相对路径,基于 getLogDir() + 相对路径
844
1072
  * - 如果为空,使用 getLogDir() + LOG_FILE_NAME
845
1073
  */
846
- const resolveLogFilePath = (logFile) => {
1074
+ const resolveLogFilePath$1 = (logFile) => {
847
1075
  if (!logFile) {
848
- return path.join(getLogDir(), LOG_FILE_NAME);
1076
+ return path.join(getLogDir$1(), LOG_FILE_NAME$1);
849
1077
  }
850
1078
 
851
1079
  if (path.isAbsolute(logFile)) {
852
1080
  return logFile;
853
1081
  }
854
1082
 
855
- return path.join(getLogDir(), logFile);
1083
+ return path.join(getLogDir$1(), logFile);
856
1084
  };
857
1085
 
858
1086
  /**
859
1087
  * 创建日志写入流
860
1088
  */
861
- const createLogStream = (logFilePath) => {
1089
+ const createLogStream$1 = (logFilePath) => {
862
1090
  const logDir = path.dirname(logFilePath);
863
1091
 
864
1092
  // 确保日志目录存在
@@ -880,15 +1108,27 @@ const executeRun = async (
880
1108
  try {
881
1109
  logger.info(`Running ${commandName} command...`);
882
1110
 
883
- // 1. 加载 .coze 配置
1111
+ // 1. 对于 build 命令,先执行 fix 以确保项目配置正确
1112
+ if (['dev', 'build'].includes(commandName)) {
1113
+ logger.info('\n🔧 Running fix command before build...\n');
1114
+ try {
1115
+ await executeFix();
1116
+ // eslint-disable-next-line @coze-arch/no-empty-catch
1117
+ } catch (e) {
1118
+ // just ignore
1119
+ }
1120
+ logger.info('');
1121
+ }
1122
+
1123
+ // 2. 加载 .coze 配置
884
1124
  const config = await loadCozeConfig();
885
1125
  const commandArgs = getCommandConfig(config, commandName);
886
1126
 
887
- // 2. 准备日志
888
- const logFilePath = resolveLogFilePath(options.logFile);
889
- const logStream = createLogStream(logFilePath);
1127
+ // 3. 准备日志
1128
+ const logFilePath = resolveLogFilePath$1(options.logFile);
1129
+ const logStream = createLogStream$1(logFilePath);
890
1130
 
891
- // 3. 执行命令
1131
+ // 4. 执行命令
892
1132
  const commandString = commandArgs.join(' ');
893
1133
 
894
1134
  logger.info(`Executing: ${commandString}`);
@@ -949,7 +1189,7 @@ const executeRun = async (
949
1189
  /**
950
1190
  * 注册 dev/build/start 命令到 program
951
1191
  */
952
- const registerCommand$1 = program => {
1192
+ const registerCommand$2 = program => {
953
1193
  // dev 命令
954
1194
  program
955
1195
  .command('dev')
@@ -1201,6 +1441,11 @@ const shouldIgnoreFile = (filePath) => {
1201
1441
  return directoryPatterns.some(dir => pathParts.includes(dir));
1202
1442
  };
1203
1443
 
1444
+ // ABOUTME: File system utilities for template file processing
1445
+ // ABOUTME: Provides directory scanning, file path conversion, and node_modules copying
1446
+
1447
+
1448
+ // start_aigc
1204
1449
  /**
1205
1450
  * 递归获取目录中的所有文件
1206
1451
  *
@@ -1270,7 +1515,19 @@ const convertDotfileName = (filePath) => {
1270
1515
 
1271
1516
  return filePath;
1272
1517
  };
1518
+ // end_aigc
1519
+
1520
+ // ABOUTME: File rendering utilities for template processing
1521
+ // ABOUTME: Handles file content rendering, hook execution, and file writing
1522
+
1523
+
1524
+
1525
+
1526
+
1527
+
1528
+
1273
1529
 
1530
+ // start_aigc
1274
1531
  /**
1275
1532
  * 执行文件渲染钩子
1276
1533
  *
@@ -1316,22 +1573,25 @@ const executeFileRenderHook = async (
1316
1573
  };
1317
1574
 
1318
1575
  /**
1319
- * 处理单个文件
1576
+ * 准备单个文件的渲染信息(不实际写入)
1577
+ * 用于 dry-run 阶段,收集所有将要写入的文件信息
1578
+ *
1579
+ * @param options - 准备选项
1580
+ * @returns 文件渲染信息,或 null 表示该文件被跳过
1320
1581
  */
1321
- const processSingleFile = async (options
1322
-
1582
+ const prepareFileInfo = async (options
1323
1583
 
1324
1584
 
1325
1585
 
1326
1586
 
1327
1587
  ) => {
1328
- const { file, templatePath, outputPath, context, templateConfig } = options;
1588
+ const { file, templatePath, context, templateConfig } = options;
1329
1589
 
1330
1590
  const srcPath = path.join(templatePath, file);
1331
1591
  const destFile = convertDotfileName(file);
1332
1592
 
1333
1593
  logger.verbose(
1334
- ` - Processing: ${file}${destFile !== file ? ` -> ${destFile}` : ''}`,
1594
+ ` - Preparing: ${file}${destFile !== file ? ` -> ${destFile}` : ''}`,
1335
1595
  );
1336
1596
 
1337
1597
  // 判断是否为二进制文件
@@ -1365,38 +1625,195 @@ const processSingleFile = async (options
1365
1625
  context,
1366
1626
  );
1367
1627
 
1368
- // 如果返回 null,跳过该文件
1628
+ // 如果返回 null,表示该文件被 hook 跳过
1369
1629
  if (processedFileInfo === null) {
1370
1630
  logger.verbose(' ⊘ Skipped by onFileRender hook');
1371
- return;
1631
+ return null;
1372
1632
  }
1373
1633
 
1374
- // 使用处理后的目标路径
1375
- const finalDestPath = path.join(outputPath, processedFileInfo.destPath);
1634
+ return processedFileInfo;
1635
+ };
1636
+
1637
+ /**
1638
+ * 写入渲染后的文件到目标路径
1639
+ *
1640
+ * @param options - 写入选项
1641
+ */
1642
+ const writeRenderedFile = async (options
1643
+
1644
+
1645
+
1646
+ ) => {
1647
+ const { fileInfo, srcPath, destPath } = options;
1648
+
1649
+ logger.verbose(` - Writing: ${fileInfo.destPath}`);
1376
1650
 
1377
1651
  // 确保目标目录存在
1378
- await ensureDir(path.dirname(finalDestPath));
1652
+ await ensureDir(path.dirname(destPath));
1379
1653
 
1380
1654
  // 写入文件
1381
- if (processedFileInfo.isBinary) {
1382
- // 二进制文件:如果内容没变,直接复制;否则从 base64 解码写入
1383
- if (processedFileInfo.content === content) {
1384
- await fs$1.copyFile(srcPath, finalDestPath);
1655
+ if (fileInfo.isBinary) {
1656
+ // 二进制文件:如果内容是原始 base64(未被 hook 修改),直接复制;否则从 base64 解码写入
1657
+ const buffer = await fs$1.readFile(srcPath);
1658
+ const originalContent = buffer.toString('base64');
1659
+
1660
+ if (fileInfo.content === originalContent) {
1661
+ await fs$1.copyFile(srcPath, destPath);
1385
1662
  logger.verbose(' ✓ Copied (binary)');
1386
1663
  } else {
1387
- const buffer = Buffer.from(processedFileInfo.content, 'base64');
1388
- await fs$1.writeFile(finalDestPath, buffer);
1664
+ const modifiedBuffer = Buffer.from(fileInfo.content, 'base64');
1665
+ await fs$1.writeFile(destPath, modifiedBuffer);
1389
1666
  logger.verbose(' ✓ Written (binary, modified by hook)');
1390
1667
  }
1391
1668
  } else {
1392
1669
  // 文本文件
1393
- await fs$1.writeFile(finalDestPath, processedFileInfo.content, 'utf-8');
1670
+ await fs$1.writeFile(destPath, fileInfo.content, 'utf-8');
1394
1671
  logger.verbose(' ✓ Rendered and written');
1395
1672
  }
1396
1673
  };
1674
+ // end_aigc
1675
+
1676
+ // ABOUTME: File conflict detection utilities for template processing
1677
+ // ABOUTME: Provides dry-run file collection and conflict checking
1678
+
1679
+
1680
+
1681
+
1682
+
1683
+
1684
+
1685
+ // start_aigc
1686
+ /**
1687
+ * 收集所有将要写入的文件路径(dry-run 阶段)
1688
+ *
1689
+ * @param options - 收集选项
1690
+ * @returns 将要写入的文件路径列表
1691
+ */
1692
+ const collectFilesToRender = async (options
1693
+
1694
+
1695
+
1696
+
1697
+ ) => {
1698
+ const { files, templatePath, context, templateConfig } = options;
1699
+
1700
+ logger.verbose('\nDry-run: Collecting files to render...');
1701
+
1702
+ const fileInfos = await Promise.all(
1703
+ files.map(file =>
1704
+ prepareFileInfo({
1705
+ file,
1706
+ templatePath,
1707
+ context,
1708
+ templateConfig,
1709
+ }),
1710
+ ),
1711
+ );
1712
+
1713
+ // 过滤掉被 hook 跳过的文件,收集 destPath
1714
+ const filesToWrite = fileInfos
1715
+ .filter((info) => info !== null)
1716
+ .map(info => info.destPath);
1717
+
1718
+ logger.verbose(` - ${filesToWrite.length} files will be written`);
1719
+ logger.verbose(
1720
+ ` - ${fileInfos.length - filesToWrite.length} files skipped by hooks`,
1721
+ );
1722
+
1723
+ return filesToWrite;
1724
+ };
1725
+
1726
+ /**
1727
+ * 检测文件冲突
1728
+ *
1729
+ * @param outputPath - 输出目录路径
1730
+ * @param filesToWrite - 将要写入的文件路径列表
1731
+ * @returns 冲突的文件路径列表
1732
+ */
1733
+ const detectFileConflicts = (
1734
+ outputPath,
1735
+ filesToWrite,
1736
+ ) => {
1737
+ logger.verbose('\nChecking for file conflicts...');
1738
+
1739
+ const conflicts = [];
1740
+
1741
+ for (const file of filesToWrite) {
1742
+ const fullPath = path.join(outputPath, file);
1743
+ if (fs.existsSync(fullPath)) {
1744
+ conflicts.push(file);
1745
+ logger.verbose(` ⚠ Conflict detected: ${file}`);
1746
+ }
1747
+ }
1748
+
1749
+ if (conflicts.length === 0) {
1750
+ logger.verbose(' ✓ No conflicts detected');
1751
+ } else {
1752
+ logger.verbose(` ⚠ ${conflicts.length} conflicts detected`);
1753
+ }
1754
+
1755
+ return conflicts;
1756
+ };
1757
+ // end_aigc
1758
+
1759
+ // ABOUTME: Main file processing orchestration for template rendering
1760
+ // ABOUTME: Coordinates file system, rendering, and conflict detection layers
1761
+
1762
+
1763
+
1764
+ // start_aigc
1765
+ /**
1766
+ * 处理单个文件(准备 + 写入)
1767
+ *
1768
+ * @param options - 处理选项
1769
+ */
1770
+ const processSingleFile = async (options
1771
+
1772
+
1773
+
1774
+
1775
+
1776
+ ) => {
1777
+ const { file, templatePath, outputPath, context, templateConfig } = options;
1778
+
1779
+ const srcPath = path.join(templatePath, file);
1780
+
1781
+ // 准备文件信息
1782
+ const processedFileInfo = await prepareFileInfo({
1783
+ file,
1784
+ templatePath,
1785
+ context,
1786
+ templateConfig,
1787
+ });
1788
+
1789
+ // 如果返回 null,跳过该文件
1790
+ if (processedFileInfo === null) {
1791
+ return;
1792
+ }
1793
+
1794
+ // 使用处理后的目标路径
1795
+ const finalDestPath = path.join(outputPath, processedFileInfo.destPath);
1796
+
1797
+ // 写入文件
1798
+ await writeRenderedFile({
1799
+ fileInfo: processedFileInfo,
1800
+ srcPath,
1801
+ destPath: finalDestPath,
1802
+ });
1803
+ };
1397
1804
 
1398
1805
  /**
1399
1806
  * 复制并处理模板文件到目标目录
1807
+ *
1808
+ * 流程:
1809
+ * 1. 验证模板目录
1810
+ * 2. 扫描所有模板文件
1811
+ * 3. Dry-run:收集将要写入的文件列表(考虑 hooks 影响)
1812
+ * 4. 冲突检测:检查是否有文件会被覆盖
1813
+ * 5. 实际写入:渲染并写入所有文件
1814
+ * 6. 复制 node_modules(如果存在)
1815
+ *
1816
+ * @param options - 处理选项
1400
1817
  */
1401
1818
  const processTemplateFiles = async (options
1402
1819
 
@@ -1409,7 +1826,7 @@ const processTemplateFiles = async (options
1409
1826
  logger.verbose(` - Template path: ${templatePath}`);
1410
1827
  logger.verbose(` - Output path: ${outputPath}`);
1411
1828
 
1412
- // 验证模板目录是否存在
1829
+ // 阶段 0: 验证模板目录是否存在
1413
1830
  try {
1414
1831
  const stat = await fs$1.stat(templatePath);
1415
1832
  logger.verbose(
@@ -1425,6 +1842,7 @@ const processTemplateFiles = async (options
1425
1842
  throw error;
1426
1843
  }
1427
1844
 
1845
+ // 阶段 1: 扫描所有模板文件
1428
1846
  const files = await getAllFiles(templatePath);
1429
1847
 
1430
1848
  logger.verbose(` - Found ${files.length} files to process`);
@@ -1434,6 +1852,29 @@ const processTemplateFiles = async (options
1434
1852
  return;
1435
1853
  }
1436
1854
 
1855
+ // 阶段 2: Dry-run - 收集所有将要写入的文件
1856
+ const filesToWrite = await collectFilesToRender({
1857
+ files,
1858
+ templatePath,
1859
+ context,
1860
+ templateConfig,
1861
+ });
1862
+
1863
+ // 阶段 3: 冲突检测
1864
+ const conflicts = detectFileConflicts(outputPath, filesToWrite);
1865
+
1866
+ if (conflicts.length > 0) {
1867
+ // 有冲突,抛出详细的错误信息
1868
+ const conflictList = conflicts.map(f => ` - ${f}`).join('\n');
1869
+ throw new Error(
1870
+ `File conflicts detected in output directory: ${outputPath}\n\n` +
1871
+ `The following files already exist and would be overwritten:\n${conflictList}\n\n` +
1872
+ 'Please remove these files or use a different output directory.',
1873
+ );
1874
+ }
1875
+
1876
+ // 阶段 4: 实际写入文件
1877
+ logger.verbose('\nWriting files...');
1437
1878
  await Promise.all(
1438
1879
  files.map(file =>
1439
1880
  processSingleFile({
@@ -1448,74 +1889,9 @@ const processTemplateFiles = async (options
1448
1889
 
1449
1890
  logger.verbose('✓ All files processed successfully');
1450
1891
 
1451
- // 单独处理 node_modules 目录(如果存在)
1452
- const sourceNodeModules = path.join(templatePath, 'node_modules');
1453
- const targetNodeModules = path.join(outputPath, 'node_modules');
1454
-
1455
- if (fs.existsSync(sourceNodeModules)) {
1456
- logger.info('\nCopying node_modules from pre-warmed template...');
1457
- logger.verbose(` From: ${sourceNodeModules}`);
1458
- logger.verbose(` To: ${targetNodeModules}`);
1459
-
1460
- const result = shelljs.exec(`cp -R "${sourceNodeModules}" "${targetNodeModules}"`, {
1461
- silent: true,
1462
- });
1463
-
1464
- if (result.stdout) {
1465
- process.stdout.write(result.stdout);
1466
- }
1467
-
1468
- if (result.stderr) {
1469
- process.stderr.write(result.stderr);
1470
- }
1471
-
1472
- if (result.code === 0) {
1473
- logger.success('✓ node_modules copied successfully');
1474
- } else {
1475
- logger.warn(
1476
- `Failed to copy node_modules: ${result.stderr || 'unknown error'}`,
1477
- );
1478
- logger.info('Will need to run pnpm install manually');
1479
- }
1480
- }
1481
- };
1482
-
1483
- /**
1484
- * 检查输出目录是否为空
1485
- * 注意:.git 目录会被忽略,允许在已初始化 git 的目录中创建项目
1486
- *
1487
- * @param outputPath - 输出目录路径
1488
- * @returns 是否为空
1489
- */
1490
- const isOutputDirEmpty = async (
1491
- outputPath,
1492
- ) => {
1493
- try {
1494
- const entries = await fs$1.readdir(outputPath);
1495
- // 过滤掉 .git 目录,允许在已初始化 git 的目录中创建项目
1496
- const filteredEntries = entries.filter(entry => entry !== '.git');
1497
- return filteredEntries.length === 0;
1498
- } catch (e) {
1499
- // 目录不存在,视为空
1500
- return true;
1501
- }
1502
- };
1503
-
1504
- /**
1505
- * 验证输出目录
1506
- *
1507
- * @param outputPath - 输出目录路径
1508
- * @throws 如果目录不为空则抛出错误
1509
- */
1510
- const validateOutputDir = async (outputPath) => {
1511
- const isEmpty = await isOutputDirEmpty(outputPath);
1512
- if (!isEmpty) {
1513
- throw new Error(
1514
- `Output directory is not empty: ${outputPath}\n` +
1515
- 'Please use an empty directory or remove existing files.',
1516
- );
1517
- }
1892
+ // node_modules 将由 pnpm install 处理(利用缓存和硬链接机制)
1518
1893
  };
1894
+ // end_aigc
1519
1895
 
1520
1896
  /**
1521
1897
  * 模板引擎执行选项
@@ -1595,15 +1971,40 @@ const executeAfterRenderHook = async (
1595
1971
  }
1596
1972
  };
1597
1973
 
1974
+ /**
1975
+ * 执行完成钩子
1976
+ */
1977
+ const executeCompleteHook = async (
1978
+ templateConfig,
1979
+ context,
1980
+ outputPath,
1981
+ ) => {
1982
+ if (templateConfig.onComplete) {
1983
+ await templateConfig.onComplete(context, outputPath);
1984
+ }
1985
+ };
1986
+
1598
1987
  /**
1599
1988
  * 准备输出目录
1600
1989
  */
1601
- const prepareOutputDirectory = async (outputPath) => {
1990
+ const prepareOutputDirectory = (outputPath) => {
1602
1991
  const absolutePath = path.resolve(process.cwd(), outputPath);
1603
- await validateOutputDir(absolutePath);
1992
+ // 不再在这里验证目录是否为空,冲突检测已移至 processTemplateFiles 中
1604
1993
  return absolutePath;
1605
1994
  };
1606
1995
 
1996
+ /**
1997
+ * 模板引擎执行结果
1998
+ */
1999
+
2000
+
2001
+
2002
+
2003
+
2004
+
2005
+
2006
+
2007
+
1607
2008
  /**
1608
2009
  * 执行完整的模板渲染流程
1609
2010
  */
@@ -1627,7 +2028,7 @@ const execute = async (
1627
2028
  });
1628
2029
 
1629
2030
  // 5. 准备输出目录
1630
- const absoluteOutputPath = await prepareOutputDirectory(outputPath);
2031
+ const absoluteOutputPath = prepareOutputDirectory(outputPath);
1631
2032
 
1632
2033
  // 6. 处理模板文件
1633
2034
  await processTemplateFiles({
@@ -1640,7 +2041,11 @@ const execute = async (
1640
2041
  // 7. 执行 onAfterRender 钩子
1641
2042
  await executeAfterRenderHook(templateConfig, context, absoluteOutputPath);
1642
2043
 
1643
- return absoluteOutputPath;
2044
+ return {
2045
+ outputPath: absoluteOutputPath,
2046
+ templateConfig,
2047
+ context,
2048
+ };
1644
2049
  };
1645
2050
 
1646
2051
  function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
@@ -1803,32 +2208,37 @@ const executeInit = async (
1803
2208
  logger.info(`Initializing project with template: ${templateName}`);
1804
2209
  timer.logPhase('Initialization');
1805
2210
 
1806
- // 执行模板引擎,返回绝对路径
1807
- const absoluteOutputPath = await execute({
2211
+ // 执行模板引擎,返回结果对象
2212
+ const result = await execute({
1808
2213
  templateName,
1809
2214
  outputPath,
1810
2215
  command,
1811
2216
  });
2217
+ const { outputPath: absoluteOutputPath, templateConfig, context } = result;
1812
2218
 
1813
2219
  timer.logPhase('Template engine execution');
1814
2220
  logger.success('Project created successfully!');
1815
2221
 
1816
- // 如果没有跳过安装,检查是否需要运行 pnpm install
1817
- if (!skipInstall) {
1818
- const nodeModulesPath = path.join(absoluteOutputPath, 'node_modules');
1819
- const hasNodeModules = fs.existsSync(nodeModulesPath);
2222
+ // 检查是否存在 package.json
2223
+ const packageJsonPath = path.join(absoluteOutputPath, 'package.json');
2224
+ const hasPackageJson = fs.existsSync(packageJsonPath);
1820
2225
 
1821
- if (hasNodeModules) {
1822
- logger.info(
1823
- '\n💡 Using pre-warmed node_modules, skipping pnpm install',
1824
- );
1825
- timer.logPhase('Node modules (pre-warmed)');
1826
- } else {
2226
+ // 安装依赖(始终使用 pnpm install,利用缓存机制)
2227
+ if (!skipInstall) {
2228
+ if (hasPackageJson) {
1827
2229
  runPnpmInstall(absoluteOutputPath);
1828
2230
  timer.logPhase('Dependencies installation');
2231
+ } else {
2232
+ logger.info(
2233
+ '\n💡 No package.json found, skipping dependency installation',
2234
+ );
1829
2235
  }
1830
2236
  }
1831
2237
 
2238
+ // 执行 onComplete 钩子(在 pnpm install 之后)
2239
+ await executeCompleteHook(templateConfig, context, absoluteOutputPath);
2240
+ timer.logPhase('Complete hook execution');
2241
+
1832
2242
  // 如果没有跳过 git,则初始化 git 仓库
1833
2243
  if (!skipGit) {
1834
2244
  runGitInit(absoluteOutputPath);
@@ -1843,7 +2253,7 @@ const executeInit = async (
1843
2253
  // 只有跳过 dev 时才显示 Next steps
1844
2254
  logger.info('\nNext steps:');
1845
2255
  logger.info(` cd ${outputPath}`);
1846
- if (skipInstall) {
2256
+ if (skipInstall && hasPackageJson) {
1847
2257
  logger.info(' pnpm install');
1848
2258
  }
1849
2259
  if (skipGit) {
@@ -1867,7 +2277,7 @@ const executeInit = async (
1867
2277
  /**
1868
2278
  * 注册 init 命令到 program
1869
2279
  */
1870
- const registerCommand = program => {
2280
+ const registerCommand$1 = program => {
1871
2281
  program
1872
2282
  .command('init')
1873
2283
  .description('Initialize a new project from a template')
@@ -1885,14 +2295,312 @@ const registerCommand = program => {
1885
2295
  });
1886
2296
  };
1887
2297
 
1888
- var version = "0.0.1-alpha.d260be";
2298
+ // ABOUTME: This file implements the update command for coze CLI
2299
+ // ABOUTME: It wraps pnpm update/install to update package dependencies with logging support
2300
+
2301
+
2302
+
2303
+
2304
+ /**
2305
+ * 日志文件名常量
2306
+ */
2307
+ const LOG_FILE_NAME = 'update.log';
2308
+
2309
+ /**
2310
+ * 获取日志目录
2311
+ * 优先使用环境变量 COZE_LOG_DIR,否则使用 ~/.coze-logs
2312
+ */
2313
+ const getLogDir = () =>
2314
+ process.env.COZE_LOG_DIR || path.join(os.homedir(), '.coze-logs');
2315
+
2316
+ /**
2317
+ * 解析日志文件路径
2318
+ * - 如果是绝对路径,直接使用
2319
+ * - 如果是相对路径,基于 getLogDir() + 相对路径
2320
+ * - 如果为空,使用 getLogDir() + LOG_FILE_NAME
2321
+ */
2322
+ const resolveLogFilePath = (logFile) => {
2323
+ if (!logFile) {
2324
+ return path.join(getLogDir(), LOG_FILE_NAME);
2325
+ }
2326
+
2327
+ if (path.isAbsolute(logFile)) {
2328
+ return logFile;
2329
+ }
2330
+
2331
+ return path.join(getLogDir(), logFile);
2332
+ };
2333
+
2334
+ /**
2335
+ * 创建日志写入流
2336
+ */
2337
+ const createLogStream = (logFilePath) => {
2338
+ const logDir = path.dirname(logFilePath);
2339
+
2340
+ // 确保日志目录存在
2341
+ if (!fs.existsSync(logDir)) {
2342
+ fs.mkdirSync(logDir, { recursive: true });
2343
+ }
2344
+
2345
+ // 使用 'w' 标志覆盖之前的日志
2346
+ return fs.createWriteStream(logFilePath, { flags: 'w' });
2347
+ };
2348
+
2349
+ /**
2350
+ * 格式化时间戳
2351
+ */
2352
+ const formatTimestamp = () => {
2353
+ const now = new Date();
2354
+ return now.toISOString();
2355
+ };
2356
+
2357
+ /**
2358
+ * 写入带时间戳的日志
2359
+ */
2360
+ const writeLogWithTimestamp = (stream, message) => {
2361
+ const timestamp = formatTimestamp();
2362
+ const lines = message.split('\n');
2363
+ lines.forEach(line => {
2364
+ if (line) {
2365
+ stream.write(`[${timestamp}] ${line}\n`);
2366
+ } else {
2367
+ stream.write('\n');
2368
+ }
2369
+ });
2370
+ // 确保数据写入磁盘
2371
+ stream.uncork();
2372
+ };
2373
+
2374
+ /**
2375
+ * 同时输出到控制台和日志文件
2376
+ */
2377
+ const logWithFile = (
2378
+ stream,
2379
+ level,
2380
+ message,
2381
+ ) => {
2382
+ // 输出到控制台
2383
+ switch (level) {
2384
+ case 'info':
2385
+ logger.info(message);
2386
+ break;
2387
+ case 'success':
2388
+ logger.success(message);
2389
+ break;
2390
+ case 'error':
2391
+ logger.error(message);
2392
+ break;
2393
+ default:
2394
+ logger.info(message);
2395
+ break;
2396
+ }
2397
+
2398
+ // 写入日志文件(带时间戳)
2399
+ writeLogWithTimestamp(stream, `[${level.toUpperCase()}] ${message}`);
2400
+ };
2401
+
2402
+ // start_aigc
2403
+ /**
2404
+ * 构建 pnpm add 命令
2405
+ */
2406
+ const buildPnpmCommand = (
2407
+ packageName,
2408
+ options
2409
+
2410
+
2411
+
2412
+
2413
+ ,
2414
+ ) => {
2415
+ const { global, version, registry, extraArgs } = options;
2416
+
2417
+ const parts = ['pnpm', 'add'];
2418
+
2419
+ // 添加全局标记
2420
+ if (global) {
2421
+ parts.push('-g');
2422
+ }
2423
+
2424
+ // 添加包名和版本
2425
+ if (version && version !== 'latest') {
2426
+ parts.push(`${packageName}@${version}`);
2427
+ } else {
2428
+ parts.push(`${packageName}@latest`);
2429
+ }
2430
+
2431
+ // 添加 registry
2432
+ if (registry) {
2433
+ parts.push(`--registry=${registry}`);
2434
+ }
2435
+
2436
+ // 添加额外参数
2437
+ if (extraArgs.length > 0) {
2438
+ parts.push(...extraArgs);
2439
+ }
2440
+
2441
+ return parts.join(' ');
2442
+ };
2443
+ // end_aigc
2444
+
2445
+ // start_aigc
2446
+ /**
2447
+ * 执行 update 命令的内部实现
2448
+ */
2449
+ const executeUpdate = (
2450
+ packageName,
2451
+ options
2452
+
2453
+
2454
+
2455
+
2456
+
2457
+
2458
+ ,
2459
+ ) => {
2460
+ let logStream = null;
2461
+
2462
+ try {
2463
+ const { global, cwd, version, registry, logFile, extraArgs } = options;
2464
+
2465
+ // 准备日志
2466
+ const logFilePath = resolveLogFilePath(logFile);
2467
+
2468
+ // 调试:确认日志路径
2469
+ logger.info(`Log file path resolved to: ${logFilePath}`);
2470
+
2471
+ logStream = createLogStream(logFilePath);
2472
+
2473
+ // 调试:确认流已创建
2474
+ logger.info('Log stream created successfully');
2475
+
2476
+ logWithFile(logStream, 'info', `Updating package: ${packageName}`);
2477
+
2478
+ // 构建命令
2479
+ const command = buildPnpmCommand(packageName, {
2480
+ global,
2481
+ version,
2482
+ registry,
2483
+ extraArgs,
2484
+ });
2485
+
2486
+ // 确定工作目录
2487
+ const workingDir = cwd
2488
+ ? path.isAbsolute(cwd)
2489
+ ? cwd
2490
+ : path.join(process.cwd(), cwd)
2491
+ : process.cwd();
2492
+
2493
+ logWithFile(logStream, 'info', `Executing: ${command}`);
2494
+ logWithFile(logStream, 'info', `Working directory: ${workingDir}`);
2495
+ logWithFile(logStream, 'info', `Log file: ${logFilePath}`);
2496
+
2497
+ // 记录命令开始时间
2498
+ writeLogWithTimestamp(logStream, '--- Command execution started ---');
2499
+
2500
+ // 同步执行命令
2501
+ const result = shelljs.exec(command, {
2502
+ cwd: workingDir,
2503
+ silent: true, // 使用 silent 来捕获输出
2504
+ });
2505
+
2506
+ // 将输出写入控制台和日志文件(带时间戳)
2507
+ if (result.stdout) {
2508
+ process.stdout.write(result.stdout);
2509
+ writeLogWithTimestamp(logStream, result.stdout.trim());
2510
+ }
2511
+
2512
+ if (result.stderr) {
2513
+ process.stderr.write(result.stderr);
2514
+ writeLogWithTimestamp(logStream, result.stderr.trim());
2515
+ }
2516
+
2517
+ // 记录命令结束时间
2518
+ writeLogWithTimestamp(logStream, '--- Command execution ended ---');
2519
+
2520
+ // 检查执行结果并记录到日志
2521
+ if (result.code === 0) {
2522
+ logWithFile(logStream, 'success', 'Package updated successfully');
2523
+ logWithFile(logStream, 'info', `Log file: ${logFilePath}`);
2524
+ } else {
2525
+ logWithFile(
2526
+ logStream,
2527
+ 'error',
2528
+ `Command exited with code ${result.code}`,
2529
+ );
2530
+ logWithFile(
2531
+ logStream,
2532
+ 'error',
2533
+ `Check log file for details: ${logFilePath}`,
2534
+ );
2535
+ }
2536
+
2537
+ // 关闭日志流并等待写入完成
2538
+ logStream.end(() => {
2539
+ // 流关闭后再退出进程
2540
+ if (result.code !== 0) {
2541
+ process.exit(result.code || 1);
2542
+ }
2543
+ });
2544
+ } catch (error) {
2545
+ logger.error('Failed to update package:');
2546
+ logger.error(error instanceof Error ? error.message : String(error));
2547
+
2548
+ // 写入错误到日志文件
2549
+ if (logStream) {
2550
+ writeLogWithTimestamp(
2551
+ logStream,
2552
+ `[ERROR] ${error instanceof Error ? error.message : String(error)}`,
2553
+ );
2554
+ // 等待流关闭后再退出
2555
+ logStream.end(() => {
2556
+ process.exit(1);
2557
+ });
2558
+ } else {
2559
+ process.exit(1);
2560
+ }
2561
+ }
2562
+ };
2563
+ // end_aigc
2564
+
2565
+ /**
2566
+ * 注册 update 命令到 program
2567
+ */
2568
+ const registerCommand = program => {
2569
+ program
2570
+ .command('update <package>')
2571
+ .description('Update a package dependency')
2572
+ .option('-g, --global', 'Update package globally', false)
2573
+ .option('-c, --cwd <path>', 'Working directory for the update')
2574
+ .option(
2575
+ '--to <version>',
2576
+ 'Version to update to (default: latest)',
2577
+ 'latest',
2578
+ )
2579
+ .option('--registry <url>', 'Registry URL to use for the update')
2580
+ .option('--log-file <path>', 'Log file path')
2581
+ .allowUnknownOption() // 允许透传参数给 pnpm
2582
+ .action((packageName, options, command) => {
2583
+ // 收集所有未知选项作为额外参数
2584
+ const extraArgs = command.args.slice(1);
2585
+
2586
+ executeUpdate(packageName, {
2587
+ ...options,
2588
+ version: options.to, // 将 --to 映射到 version
2589
+ extraArgs,
2590
+ });
2591
+ });
2592
+ };
2593
+
2594
+ var version = "0.0.1-alpha.d4acfb";
1889
2595
  var packageJson = {
1890
2596
  version: version};
1891
2597
 
1892
2598
  const commands = [
1893
- registerCommand,
1894
2599
  registerCommand$1,
1895
2600
  registerCommand$2,
2601
+ registerCommand$4,
2602
+ registerCommand$3,
2603
+ registerCommand,
1896
2604
  ];
1897
2605
 
1898
2606
  const main = () => {