@coze-arch/cli 0.0.1-alpha.e772d9 → 0.0.1-alpha.e89608

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 (124) hide show
  1. package/README.md +1 -0
  2. package/lib/__templates__/expo/.cozeproj/scripts/dev_run.sh +8 -5
  3. package/lib/__templates__/expo/.cozeproj/scripts/server_dev_run.sh +6 -5
  4. package/lib/__templates__/expo/client/components/Screen.tsx +2 -2
  5. package/lib/__templates__/expo/client/eslint.config.mjs +8 -1
  6. package/lib/__templates__/expo/client/metro.config.js +3 -0
  7. package/lib/__templates__/expo/client/package.json +35 -35
  8. package/lib/__templates__/expo/client/screens/demo/index.tsx +3 -3
  9. package/lib/__templates__/expo/client/scripts/install-missing-deps.js +10 -10
  10. package/lib/__templates__/expo/eslint-plugins/forbid-emoji/index.js +9 -0
  11. package/lib/__templates__/expo/eslint-plugins/forbid-emoji/rule.js +112 -0
  12. package/lib/__templates__/expo/eslint-plugins/forbid-emoji/tech.md +94 -0
  13. package/lib/__templates__/expo/eslint-plugins/react-native/index.js +9 -0
  14. package/lib/__templates__/expo/eslint-plugins/react-native/rule.js +64 -0
  15. package/lib/__templates__/expo/package.json +1 -1
  16. package/lib/__templates__/expo/patches/{expo@54.0.32.patch → expo@54.0.33.patch} +3 -2
  17. package/lib/__templates__/expo/pnpm-lock.yaml +340 -1736
  18. package/lib/__templates__/expo/server/package.json +9 -7
  19. package/lib/__templates__/expo/server/src/index.ts +1 -0
  20. package/lib/__templates__/expo/template.config.js +56 -0
  21. package/lib/__templates__/native-static/.coze +11 -0
  22. package/lib/__templates__/native-static/index.html +33 -0
  23. package/lib/__templates__/native-static/styles/main.css +136 -0
  24. package/lib/__templates__/native-static/template.config.js +22 -0
  25. package/lib/__templates__/nextjs/README.md +5 -0
  26. package/lib/__templates__/nextjs/eslint.config.mjs +5 -0
  27. package/lib/__templates__/nextjs/next.config.ts +1 -2
  28. package/lib/__templates__/nextjs/package.json +15 -6
  29. package/lib/__templates__/nextjs/pnpm-lock.yaml +1859 -736
  30. package/lib/__templates__/nextjs/scripts/build.sh +4 -1
  31. package/lib/__templates__/nextjs/scripts/dev.sh +1 -2
  32. package/lib/__templates__/nextjs/scripts/start.sh +1 -1
  33. package/lib/__templates__/nextjs/src/app/layout.tsx +1 -1
  34. package/lib/__templates__/nextjs/src/app/page.tsx +18 -60
  35. package/lib/__templates__/nextjs/src/server.ts +35 -0
  36. package/lib/__templates__/nextjs/template.config.js +49 -14
  37. package/lib/__templates__/nextjs/tsconfig.json +1 -1
  38. package/lib/__templates__/nuxt-vue/.coze +12 -0
  39. package/lib/__templates__/nuxt-vue/README.md +73 -0
  40. package/lib/__templates__/nuxt-vue/_gitignore +24 -0
  41. package/lib/__templates__/nuxt-vue/_npmrc +23 -0
  42. package/lib/__templates__/nuxt-vue/app/app.vue +6 -0
  43. package/lib/__templates__/nuxt-vue/app/pages/index.vue +23 -0
  44. package/lib/__templates__/nuxt-vue/assets/css/main.css +24 -0
  45. package/lib/__templates__/nuxt-vue/nuxt.config.ts +116 -0
  46. package/lib/__templates__/nuxt-vue/package.json +35 -0
  47. package/lib/__templates__/nuxt-vue/pnpm-lock.yaml +8759 -0
  48. package/lib/__templates__/nuxt-vue/postcss.config.mjs +8 -0
  49. package/lib/__templates__/nuxt-vue/public/favicon.ico +0 -0
  50. package/lib/__templates__/nuxt-vue/public/robots.txt +2 -0
  51. package/lib/__templates__/nuxt-vue/scripts/build.sh +14 -0
  52. package/lib/__templates__/nuxt-vue/scripts/dev.sh +31 -0
  53. package/lib/__templates__/nuxt-vue/scripts/prepare.sh +14 -0
  54. package/lib/__templates__/nuxt-vue/scripts/start.sh +15 -0
  55. package/lib/__templates__/nuxt-vue/server/api/hello.ts +10 -0
  56. package/lib/__templates__/nuxt-vue/server/middleware/logger.ts +10 -0
  57. package/lib/__templates__/nuxt-vue/server/routes/health.ts +10 -0
  58. package/lib/__templates__/nuxt-vue/tailwind.config.js +13 -0
  59. package/lib/__templates__/nuxt-vue/template.config.js +87 -0
  60. package/lib/__templates__/nuxt-vue/tsconfig.json +18 -0
  61. package/lib/__templates__/taro/.coze +14 -0
  62. package/lib/__templates__/taro/.cozeproj/scripts/deploy_build.sh +19 -0
  63. package/lib/__templates__/taro/.cozeproj/scripts/deploy_run.sh +14 -0
  64. package/lib/__templates__/taro/.cozeproj/scripts/dev_build.sh +2 -0
  65. package/lib/__templates__/taro/.cozeproj/scripts/dev_run.sh +151 -0
  66. package/lib/__templates__/taro/.cozeproj/scripts/init_env.sh +5 -0
  67. package/lib/__templates__/taro/.cozeproj/scripts/pack.sh +24 -0
  68. package/lib/__templates__/taro/README.md +751 -0
  69. package/lib/__templates__/taro/_gitignore +40 -0
  70. package/lib/__templates__/taro/_npmrc +18 -0
  71. package/lib/__templates__/taro/babel.config.js +12 -0
  72. package/lib/__templates__/taro/config/dev.ts +9 -0
  73. package/lib/__templates__/taro/config/index.ts +223 -0
  74. package/lib/__templates__/taro/config/prod.ts +34 -0
  75. package/lib/__templates__/taro/eslint.config.mjs +80 -0
  76. package/lib/__templates__/taro/key/private.appid.key +0 -0
  77. package/lib/__templates__/taro/package.json +107 -0
  78. package/lib/__templates__/taro/patches/@tarojs__plugin-mini-ci@4.1.9.patch +30 -0
  79. package/lib/__templates__/taro/pnpm-lock.yaml +23100 -0
  80. package/lib/__templates__/taro/pnpm-workspace.yaml +2 -0
  81. package/lib/__templates__/taro/project.config.json +15 -0
  82. package/lib/__templates__/taro/server/nest-cli.json +10 -0
  83. package/lib/__templates__/taro/server/package.json +40 -0
  84. package/lib/__templates__/taro/server/src/app.controller.ts +23 -0
  85. package/lib/__templates__/taro/server/src/app.module.ts +10 -0
  86. package/lib/__templates__/taro/server/src/app.service.ts +8 -0
  87. package/lib/__templates__/taro/server/src/interceptors/http-status.interceptor.ts +23 -0
  88. package/lib/__templates__/taro/server/src/main.ts +49 -0
  89. package/lib/__templates__/taro/server/tsconfig.json +24 -0
  90. package/lib/__templates__/taro/src/app.config.ts +11 -0
  91. package/lib/__templates__/taro/src/app.css +52 -0
  92. package/lib/__templates__/taro/src/app.tsx +9 -0
  93. package/lib/__templates__/taro/src/index.html +39 -0
  94. package/lib/__templates__/taro/src/network.ts +39 -0
  95. package/lib/__templates__/taro/src/pages/index/index.config.ts +3 -0
  96. package/lib/__templates__/taro/src/pages/index/index.css +1 -0
  97. package/lib/__templates__/taro/src/pages/index/index.tsx +33 -0
  98. package/lib/__templates__/taro/src/presets/dev-debug.ts +23 -0
  99. package/lib/__templates__/taro/src/presets/h5-container.tsx +15 -0
  100. package/lib/__templates__/taro/src/presets/h5-navbar.tsx +201 -0
  101. package/lib/__templates__/taro/src/presets/h5-styles.ts +142 -0
  102. package/lib/__templates__/taro/src/presets/index.tsx +18 -0
  103. package/lib/__templates__/taro/stylelint.config.mjs +4 -0
  104. package/lib/__templates__/taro/template.config.js +68 -0
  105. package/lib/__templates__/taro/tsconfig.json +29 -0
  106. package/lib/__templates__/taro/types/global.d.ts +32 -0
  107. package/lib/__templates__/templates.json +75 -0
  108. package/lib/__templates__/vite/README.md +189 -11
  109. package/lib/__templates__/vite/_gitignore +1 -0
  110. package/lib/__templates__/vite/eslint.config.mjs +6 -1
  111. package/lib/__templates__/vite/package.json +20 -3
  112. package/lib/__templates__/vite/pnpm-lock.yaml +944 -1551
  113. package/lib/__templates__/vite/scripts/build.sh +4 -1
  114. package/lib/__templates__/vite/scripts/dev.sh +2 -2
  115. package/lib/__templates__/vite/scripts/start.sh +3 -3
  116. package/lib/__templates__/vite/server/index.ts +57 -0
  117. package/lib/__templates__/vite/server/routes/index.ts +31 -0
  118. package/lib/__templates__/vite/server/vite.ts +79 -0
  119. package/lib/__templates__/vite/src/main.ts +17 -47
  120. package/lib/__templates__/vite/template.config.js +49 -14
  121. package/lib/__templates__/vite/tsconfig.json +4 -3
  122. package/lib/__templates__/vite/vite.config.ts +1 -0
  123. package/lib/cli.js +471 -173
  124. package/package.json +7 -3
package/lib/cli.js CHANGED
@@ -4,6 +4,8 @@
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');
@@ -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$3 = 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
  };
@@ -1033,7 +1037,7 @@ const executeFix = async (options = {}) => {
1033
1037
  /**
1034
1038
  * 注册 fix 命令到 program
1035
1039
  */
1036
- const registerCommand$2 = program => {
1040
+ const registerCommand$3 = program => {
1037
1041
  program
1038
1042
  .command('fix')
1039
1043
  .description(
@@ -1052,13 +1056,13 @@ function _nullishCoalesce$1(lhs, rhsFn) { if (lhs != null) { return lhs; } else
1052
1056
  /**
1053
1057
  * 日志文件名常量
1054
1058
  */
1055
- const LOG_FILE_NAME = 'dev.log';
1059
+ const LOG_FILE_NAME$1 = 'dev.log';
1056
1060
 
1057
1061
  /**
1058
1062
  * 获取日志目录
1059
1063
  * 优先使用环境变量 COZE_LOG_DIR,否则使用 ~/.coze-logs
1060
1064
  */
1061
- const getLogDir = () =>
1065
+ const getLogDir$1 = () =>
1062
1066
  process.env.COZE_LOG_DIR || path.join(os.homedir(), '.coze-logs');
1063
1067
 
1064
1068
  /**
@@ -1067,22 +1071,22 @@ const getLogDir = () =>
1067
1071
  * - 如果是相对路径,基于 getLogDir() + 相对路径
1068
1072
  * - 如果为空,使用 getLogDir() + LOG_FILE_NAME
1069
1073
  */
1070
- const resolveLogFilePath = (logFile) => {
1074
+ const resolveLogFilePath$1 = (logFile) => {
1071
1075
  if (!logFile) {
1072
- return path.join(getLogDir(), LOG_FILE_NAME);
1076
+ return path.join(getLogDir$1(), LOG_FILE_NAME$1);
1073
1077
  }
1074
1078
 
1075
1079
  if (path.isAbsolute(logFile)) {
1076
1080
  return logFile;
1077
1081
  }
1078
1082
 
1079
- return path.join(getLogDir(), logFile);
1083
+ return path.join(getLogDir$1(), logFile);
1080
1084
  };
1081
1085
 
1082
1086
  /**
1083
1087
  * 创建日志写入流
1084
1088
  */
1085
- const createLogStream = (logFilePath) => {
1089
+ const createLogStream$1 = (logFilePath) => {
1086
1090
  const logDir = path.dirname(logFilePath);
1087
1091
 
1088
1092
  // 确保日志目录存在
@@ -1121,8 +1125,8 @@ const executeRun = async (
1121
1125
  const commandArgs = getCommandConfig(config, commandName);
1122
1126
 
1123
1127
  // 3. 准备日志
1124
- const logFilePath = resolveLogFilePath(options.logFile);
1125
- const logStream = createLogStream(logFilePath);
1128
+ const logFilePath = resolveLogFilePath$1(options.logFile);
1129
+ const logStream = createLogStream$1(logFilePath);
1126
1130
 
1127
1131
  // 4. 执行命令
1128
1132
  const commandString = commandArgs.join(' ');
@@ -1185,7 +1189,7 @@ const executeRun = async (
1185
1189
  /**
1186
1190
  * 注册 dev/build/start 命令到 program
1187
1191
  */
1188
- const registerCommand$1 = program => {
1192
+ const registerCommand$2 = program => {
1189
1193
  // dev 命令
1190
1194
  program
1191
1195
  .command('dev')
@@ -1511,46 +1515,6 @@ const convertDotfileName = (filePath) => {
1511
1515
 
1512
1516
  return filePath;
1513
1517
  };
1514
-
1515
- /**
1516
- * 复制 node_modules 目录(如果存在)
1517
- *
1518
- * @param sourceNodeModules - 源 node_modules 路径
1519
- * @param targetNodeModules - 目标 node_modules 路径
1520
- */
1521
- const copyNodeModules = (
1522
- sourceNodeModules,
1523
- targetNodeModules,
1524
- ) => {
1525
- if (!fs.existsSync(sourceNodeModules)) {
1526
- return;
1527
- }
1528
-
1529
- logger.info('\nCopying node_modules from pre-warmed template...');
1530
- logger.verbose(` From: ${sourceNodeModules}`);
1531
- logger.verbose(` To: ${targetNodeModules}`);
1532
-
1533
- const result = shelljs.exec(`cp -R "${sourceNodeModules}" "${targetNodeModules}"`, {
1534
- silent: true,
1535
- });
1536
-
1537
- if (result.stdout) {
1538
- process.stdout.write(result.stdout);
1539
- }
1540
-
1541
- if (result.stderr) {
1542
- process.stderr.write(result.stderr);
1543
- }
1544
-
1545
- if (result.code === 0) {
1546
- logger.success('✓ node_modules copied successfully');
1547
- } else {
1548
- logger.warn(
1549
- `Failed to copy node_modules: ${result.stderr || 'unknown error'}`,
1550
- );
1551
- logger.info('Will need to run pnpm install manually');
1552
- }
1553
- };
1554
1518
  // end_aigc
1555
1519
 
1556
1520
  // ABOUTME: File rendering utilities for template processing
@@ -1758,38 +1722,6 @@ const collectFilesToRender = async (options
1758
1722
 
1759
1723
  return filesToWrite;
1760
1724
  };
1761
-
1762
- /**
1763
- * 检测文件冲突
1764
- *
1765
- * @param outputPath - 输出目录路径
1766
- * @param filesToWrite - 将要写入的文件路径列表
1767
- * @returns 冲突的文件路径列表
1768
- */
1769
- const detectFileConflicts = (
1770
- outputPath,
1771
- filesToWrite,
1772
- ) => {
1773
- logger.verbose('\nChecking for file conflicts...');
1774
-
1775
- const conflicts = [];
1776
-
1777
- for (const file of filesToWrite) {
1778
- const fullPath = path.join(outputPath, file);
1779
- if (fs.existsSync(fullPath)) {
1780
- conflicts.push(file);
1781
- logger.verbose(` ⚠ Conflict detected: ${file}`);
1782
- }
1783
- }
1784
-
1785
- if (conflicts.length === 0) {
1786
- logger.verbose(' ✓ No conflicts detected');
1787
- } else {
1788
- logger.verbose(` ⚠ ${conflicts.length} conflicts detected`);
1789
- }
1790
-
1791
- return conflicts;
1792
- };
1793
1725
  // end_aigc
1794
1726
 
1795
1727
  // ABOUTME: Main file processing orchestration for template rendering
@@ -1845,7 +1777,7 @@ const processSingleFile = async (options
1845
1777
  * 1. 验证模板目录
1846
1778
  * 2. 扫描所有模板文件
1847
1779
  * 3. Dry-run:收集将要写入的文件列表(考虑 hooks 影响)
1848
- * 4. 冲突检测:检查是否有文件会被覆盖
1780
+ * 4. 冲突检测:检查是否有文件会被覆盖(可通过 force 跳过)
1849
1781
  * 5. 实际写入:渲染并写入所有文件
1850
1782
  * 6. 复制 node_modules(如果存在)
1851
1783
  *
@@ -1856,8 +1788,9 @@ const processTemplateFiles = async (options
1856
1788
 
1857
1789
 
1858
1790
 
1791
+
1859
1792
  ) => {
1860
- const { templatePath, outputPath, context, templateConfig } = options;
1793
+ const { templatePath, outputPath, context, templateConfig} = options;
1861
1794
  logger.verbose('Processing template files:');
1862
1795
  logger.verbose(` - Template path: ${templatePath}`);
1863
1796
  logger.verbose(` - Output path: ${outputPath}`);
@@ -1889,23 +1822,17 @@ const processTemplateFiles = async (options
1889
1822
  }
1890
1823
 
1891
1824
  // 阶段 2: Dry-run - 收集所有将要写入的文件
1892
- const filesToWrite = await collectFilesToRender({
1825
+ await collectFilesToRender({
1893
1826
  files,
1894
1827
  templatePath,
1895
1828
  context,
1896
1829
  templateConfig,
1897
1830
  });
1898
1831
 
1899
- // 阶段 3: 冲突检测
1900
- const conflicts = detectFileConflicts(outputPath, filesToWrite);
1901
-
1902
- if (conflicts.length > 0) {
1903
- // 有冲突,抛出详细的错误信息
1904
- const conflictList = conflicts.map(f => ` - ${f}`).join('\n');
1905
- throw new Error(
1906
- `File conflicts detected in output directory: ${outputPath}\n\n` +
1907
- `The following files already exist and would be overwritten:\n${conflictList}\n\n` +
1908
- 'Please remove these files or use a different output directory.',
1832
+ // 阶段 3: 冲突检测(force 为 true 时跳过)
1833
+ {
1834
+ logger.verbose(
1835
+ ' - Force mode enabled, skipping conflict detection. Existing files will be overwritten.',
1909
1836
  );
1910
1837
  }
1911
1838
 
@@ -1925,11 +1852,7 @@ const processTemplateFiles = async (options
1925
1852
 
1926
1853
  logger.verbose('✓ All files processed successfully');
1927
1854
 
1928
- // 阶段 5: 单独处理 node_modules 目录(如果存在)
1929
- const sourceNodeModules = path.join(templatePath, 'node_modules');
1930
- const targetNodeModules = path.join(outputPath, 'node_modules');
1931
-
1932
- copyNodeModules(sourceNodeModules, targetNodeModules);
1855
+ // node_modules 将由 pnpm install 处理(利用缓存和硬链接机制)
1933
1856
  };
1934
1857
  // end_aigc
1935
1858
 
@@ -1942,6 +1865,7 @@ const processTemplateFiles = async (options
1942
1865
 
1943
1866
 
1944
1867
 
1868
+
1945
1869
  /**
1946
1870
  * 加载模板元数据和路径
1947
1871
  */
@@ -2011,6 +1935,19 @@ const executeAfterRenderHook = async (
2011
1935
  }
2012
1936
  };
2013
1937
 
1938
+ /**
1939
+ * 执行完成钩子
1940
+ */
1941
+ const executeCompleteHook = async (
1942
+ templateConfig,
1943
+ context,
1944
+ outputPath,
1945
+ ) => {
1946
+ if (templateConfig.onComplete) {
1947
+ await templateConfig.onComplete(context, outputPath);
1948
+ }
1949
+ };
1950
+
2014
1951
  /**
2015
1952
  * 准备输出目录
2016
1953
  */
@@ -2020,13 +1957,25 @@ const prepareOutputDirectory = (outputPath) => {
2020
1957
  return absolutePath;
2021
1958
  };
2022
1959
 
1960
+ /**
1961
+ * 模板引擎执行结果
1962
+ */
1963
+
1964
+
1965
+
1966
+
1967
+
1968
+
1969
+
1970
+
1971
+
2023
1972
  /**
2024
1973
  * 执行完整的模板渲染流程
2025
1974
  */
2026
1975
  const execute = async (
2027
1976
  options,
2028
1977
  ) => {
2029
- const { templateName, outputPath, command } = options;
1978
+ const { templateName, outputPath, command} = options;
2030
1979
 
2031
1980
  // 1. 加载模板
2032
1981
  const { templatePath } = await loadTemplateMetadata(templateName);
@@ -2050,13 +1999,16 @@ const execute = async (
2050
1999
  templatePath,
2051
2000
  outputPath: absoluteOutputPath,
2052
2001
  context,
2053
- templateConfig,
2054
- });
2002
+ templateConfig});
2055
2003
 
2056
2004
  // 7. 执行 onAfterRender 钩子
2057
2005
  await executeAfterRenderHook(templateConfig, context, absoluteOutputPath);
2058
2006
 
2059
- return absoluteOutputPath;
2007
+ return {
2008
+ outputPath: absoluteOutputPath,
2009
+ templateConfig,
2010
+ context,
2011
+ };
2060
2012
  };
2061
2013
 
2062
2014
  function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
@@ -2098,7 +2050,40 @@ const runPnpmInstall = (projectPath) => {
2098
2050
  };
2099
2051
 
2100
2052
  /**
2101
- * 初始化 git 仓库并创建初始提交
2053
+ * 运行 git 命令的辅助函数
2054
+ */
2055
+ const runGitCommand = (command, projectPath) => {
2056
+ logger.info(`Executing: ${command}`);
2057
+
2058
+ const result = shelljs.exec(command, {
2059
+ cwd: projectPath,
2060
+ silent: true,
2061
+ });
2062
+
2063
+ // 输出命令的结果
2064
+ if (result.stdout) {
2065
+ process.stdout.write(result.stdout);
2066
+ }
2067
+
2068
+ if (result.stderr) {
2069
+ process.stderr.write(result.stderr);
2070
+ }
2071
+
2072
+ if (result.code !== 0) {
2073
+ const errorMessage = [
2074
+ `${command} failed with exit code ${result.code}`,
2075
+ result.stderr ? `\nStderr:\n${result.stderr}` : '',
2076
+ result.stdout ? `\nStdout:\n${result.stdout}` : '',
2077
+ ]
2078
+ .filter(Boolean)
2079
+ .join('');
2080
+
2081
+ throw new Error(errorMessage);
2082
+ }
2083
+ };
2084
+
2085
+ /**
2086
+ * 初始化 git 仓库
2102
2087
  * 如果目录中已存在 .git,则跳过初始化
2103
2088
  */
2104
2089
  const runGitInit = (projectPath) => {
@@ -2111,46 +2096,9 @@ const runGitInit = (projectPath) => {
2111
2096
  return;
2112
2097
  }
2113
2098
 
2114
- const runGitCommand = (command) => {
2115
- logger.info(`Executing: ${command}`);
2116
-
2117
- const result = shelljs.exec(command, {
2118
- cwd: projectPath,
2119
- silent: true,
2120
- });
2121
-
2122
- // 输出命令的结果
2123
- if (result.stdout) {
2124
- process.stdout.write(result.stdout);
2125
- }
2126
-
2127
- if (result.stderr) {
2128
- process.stderr.write(result.stderr);
2129
- }
2130
-
2131
- if (result.code !== 0) {
2132
- const errorMessage = [
2133
- `${command} failed with exit code ${result.code}`,
2134
- result.stderr ? `\nStderr:\n${result.stderr}` : '',
2135
- result.stdout ? `\nStdout:\n${result.stdout}` : '',
2136
- ]
2137
- .filter(Boolean)
2138
- .join('');
2139
-
2140
- throw new Error(errorMessage);
2141
- }
2142
- };
2143
-
2144
2099
  try {
2145
2100
  logger.info('\nInitializing git repository...');
2146
- runGitCommand('git init');
2147
-
2148
- logger.info('Adding files to git...');
2149
- runGitCommand('git add .');
2150
-
2151
- logger.info('Creating initial commit...');
2152
- runGitCommand('git commit -m "chore: initial commit"');
2153
-
2101
+ runGitCommand('git init', projectPath);
2154
2102
  logger.success('Git repository initialized successfully!');
2155
2103
  } catch (error) {
2156
2104
  // Git 初始化失败不应该导致整个流程失败
@@ -2161,6 +2109,35 @@ const runGitInit = (projectPath) => {
2161
2109
  }
2162
2110
  };
2163
2111
 
2112
+ /**
2113
+ * 提交初始化生成的所有文件
2114
+ */
2115
+ const commitChanges = (projectPath) => {
2116
+ // 检查是否存在 .git 目录
2117
+ const gitDir = path.join(projectPath, '.git');
2118
+ if (!fs.existsSync(gitDir)) {
2119
+ logger.warn(
2120
+ '\n⚠️ Git repository does not exist, skipping commit. Run git init first.',
2121
+ );
2122
+ return;
2123
+ }
2124
+
2125
+ try {
2126
+ logger.info('\nCommitting initialized files...');
2127
+ runGitCommand('git add --all', projectPath);
2128
+ runGitCommand('git commit -m "chore: init env"', projectPath);
2129
+ logger.success('Changes committed successfully!');
2130
+ } catch (error) {
2131
+ // Commit 失败不应该导致整个流程失败
2132
+ logger.warn(
2133
+ `Git commit failed: ${error instanceof Error ? error.message : String(error)}`,
2134
+ );
2135
+ logger.info(
2136
+ 'You can manually commit later with: git add --all && git commit -m "chore: init env"',
2137
+ );
2138
+ }
2139
+ };
2140
+
2164
2141
  /**
2165
2142
  * 运行开发服务器(后台模式)
2166
2143
  * 启动后台子进程运行开发服务器,父进程可以直接退出
@@ -2202,6 +2179,8 @@ const executeInit = async (
2202
2179
 
2203
2180
 
2204
2181
 
2182
+
2183
+
2205
2184
  ,
2206
2185
  command,
2207
2186
  ) => {
@@ -2213,44 +2192,58 @@ const executeInit = async (
2213
2192
  output: outputPath,
2214
2193
  skipInstall,
2215
2194
  skipGit,
2195
+ skipCommit,
2216
2196
  skipDev,
2197
+ force,
2217
2198
  } = options;
2218
2199
 
2219
2200
  logger.info(`Initializing project with template: ${templateName}`);
2220
2201
  timer.logPhase('Initialization');
2221
2202
 
2222
- // 执行模板引擎,返回绝对路径
2223
- const absoluteOutputPath = await execute({
2203
+ // 执行模板引擎,返回结果对象
2204
+ const result = await execute({
2224
2205
  templateName,
2225
2206
  outputPath,
2226
2207
  command,
2208
+ force,
2227
2209
  });
2210
+ const { outputPath: absoluteOutputPath, templateConfig, context } = result;
2228
2211
 
2229
2212
  timer.logPhase('Template engine execution');
2230
2213
  logger.success('Project created successfully!');
2231
2214
 
2232
- // 如果没有跳过安装,检查是否需要运行 pnpm install
2233
- if (!skipInstall) {
2234
- const nodeModulesPath = path.join(absoluteOutputPath, 'node_modules');
2235
- const hasNodeModules = fs.existsSync(nodeModulesPath);
2215
+ // 检查是否存在 package.json
2216
+ const packageJsonPath = path.join(absoluteOutputPath, 'package.json');
2217
+ const hasPackageJson = fs.existsSync(packageJsonPath);
2236
2218
 
2237
- if (hasNodeModules) {
2238
- logger.info(
2239
- '\n💡 Using pre-warmed node_modules, skipping pnpm install',
2240
- );
2241
- timer.logPhase('Node modules (pre-warmed)');
2242
- } else {
2219
+ // 安装依赖(始终使用 pnpm install,利用缓存机制)
2220
+ if (!skipInstall) {
2221
+ if (hasPackageJson) {
2243
2222
  runPnpmInstall(absoluteOutputPath);
2244
2223
  timer.logPhase('Dependencies installation');
2224
+ } else {
2225
+ logger.info(
2226
+ '\n💡 No package.json found, skipping dependency installation',
2227
+ );
2245
2228
  }
2246
2229
  }
2247
2230
 
2231
+ // 执行 onComplete 钩子(在 pnpm install 之后)
2232
+ await executeCompleteHook(templateConfig, context, absoluteOutputPath);
2233
+ timer.logPhase('Complete hook execution');
2234
+
2248
2235
  // 如果没有跳过 git,则初始化 git 仓库
2249
2236
  if (!skipGit) {
2250
2237
  runGitInit(absoluteOutputPath);
2251
2238
  timer.logPhase('Git initialization');
2252
2239
  }
2253
2240
 
2241
+ // 如果没有跳过 commit,则提交初始化生成的文件
2242
+ if (!skipCommit) {
2243
+ commitChanges(absoluteOutputPath);
2244
+ timer.logPhase('Git commit');
2245
+ }
2246
+
2254
2247
  // 如果没有跳过 dev,则启动开发服务器
2255
2248
  if (!skipDev) {
2256
2249
  runDev(absoluteOutputPath);
@@ -2259,13 +2252,14 @@ const executeInit = async (
2259
2252
  // 只有跳过 dev 时才显示 Next steps
2260
2253
  logger.info('\nNext steps:');
2261
2254
  logger.info(` cd ${outputPath}`);
2262
- if (skipInstall) {
2255
+ if (skipInstall && hasPackageJson) {
2263
2256
  logger.info(' pnpm install');
2264
2257
  }
2265
2258
  if (skipGit) {
2266
- logger.info(
2267
- ' git init && git add . && git commit -m "initial commit"',
2268
- );
2259
+ logger.info(' git init');
2260
+ }
2261
+ if (skipCommit) {
2262
+ logger.info(' git add --all && git commit -m "chore: init env"');
2269
2263
  }
2270
2264
  logger.info(' coze dev');
2271
2265
  }
@@ -2283,7 +2277,7 @@ const executeInit = async (
2283
2277
  /**
2284
2278
  * 注册 init 命令到 program
2285
2279
  */
2286
- const registerCommand = program => {
2280
+ const registerCommand$1 = program => {
2287
2281
  program
2288
2282
  .command('init')
2289
2283
  .description('Initialize a new project from a template')
@@ -2292,24 +2286,328 @@ const registerCommand = program => {
2292
2286
  .option('-o, --output <path>', 'Output directory', process.cwd())
2293
2287
  .option('--skip-install', 'Skip automatic pnpm install', false)
2294
2288
  .option('--skip-git', 'Skip automatic git initialization', false)
2289
+ .option(
2290
+ '--skip-commit',
2291
+ 'Skip automatic git commit after initialization',
2292
+ false,
2293
+ )
2295
2294
  .option('--skip-dev', 'Skip automatic dev server start', false)
2296
2295
  .allowUnknownOption() // 允许透传参数
2297
2296
  .action(async (directory, options, command) => {
2298
2297
  // 位置参数优先级高于 --output 选项
2299
2298
  const outputPath = _nullishCoalesce(directory, () => ( options.output));
2300
- await executeInit({ ...options, output: outputPath }, command);
2299
+ // Always use force mode - overwrite existing files without conflict check
2300
+ const force = true;
2301
+ await executeInit({ ...options, output: outputPath, force }, command);
2302
+ });
2303
+ };
2304
+
2305
+ // ABOUTME: This file implements the update command for coze CLI
2306
+ // ABOUTME: It wraps pnpm update/install to update package dependencies with logging support
2307
+
2308
+
2309
+
2310
+
2311
+ /**
2312
+ * 日志文件名常量
2313
+ */
2314
+ const LOG_FILE_NAME = 'update.log';
2315
+
2316
+ /**
2317
+ * 获取日志目录
2318
+ * 优先使用环境变量 COZE_LOG_DIR,否则使用 ~/.coze-logs
2319
+ */
2320
+ const getLogDir = () =>
2321
+ process.env.COZE_LOG_DIR || path.join(os.homedir(), '.coze-logs');
2322
+
2323
+ /**
2324
+ * 解析日志文件路径
2325
+ * - 如果是绝对路径,直接使用
2326
+ * - 如果是相对路径,基于 getLogDir() + 相对路径
2327
+ * - 如果为空,使用 getLogDir() + LOG_FILE_NAME
2328
+ */
2329
+ const resolveLogFilePath = (logFile) => {
2330
+ if (!logFile) {
2331
+ return path.join(getLogDir(), LOG_FILE_NAME);
2332
+ }
2333
+
2334
+ if (path.isAbsolute(logFile)) {
2335
+ return logFile;
2336
+ }
2337
+
2338
+ return path.join(getLogDir(), logFile);
2339
+ };
2340
+
2341
+ /**
2342
+ * 创建日志写入流
2343
+ */
2344
+ const createLogStream = (logFilePath) => {
2345
+ const logDir = path.dirname(logFilePath);
2346
+
2347
+ // 确保日志目录存在
2348
+ if (!fs.existsSync(logDir)) {
2349
+ fs.mkdirSync(logDir, { recursive: true });
2350
+ }
2351
+
2352
+ // 使用 'w' 标志覆盖之前的日志
2353
+ return fs.createWriteStream(logFilePath, { flags: 'w' });
2354
+ };
2355
+
2356
+ /**
2357
+ * 格式化时间戳
2358
+ */
2359
+ const formatTimestamp = () => {
2360
+ const now = new Date();
2361
+ return now.toISOString();
2362
+ };
2363
+
2364
+ /**
2365
+ * 写入带时间戳的日志
2366
+ */
2367
+ const writeLogWithTimestamp = (stream, message) => {
2368
+ const timestamp = formatTimestamp();
2369
+ const lines = message.split('\n');
2370
+ lines.forEach(line => {
2371
+ if (line) {
2372
+ stream.write(`[${timestamp}] ${line}\n`);
2373
+ } else {
2374
+ stream.write('\n');
2375
+ }
2376
+ });
2377
+ // 确保数据写入磁盘
2378
+ stream.uncork();
2379
+ };
2380
+
2381
+ /**
2382
+ * 同时输出到控制台和日志文件
2383
+ */
2384
+ const logWithFile = (
2385
+ stream,
2386
+ level,
2387
+ message,
2388
+ ) => {
2389
+ // 输出到控制台
2390
+ switch (level) {
2391
+ case 'info':
2392
+ logger.info(message);
2393
+ break;
2394
+ case 'success':
2395
+ logger.success(message);
2396
+ break;
2397
+ case 'error':
2398
+ logger.error(message);
2399
+ break;
2400
+ default:
2401
+ logger.info(message);
2402
+ break;
2403
+ }
2404
+
2405
+ // 写入日志文件(带时间戳)
2406
+ writeLogWithTimestamp(stream, `[${level.toUpperCase()}] ${message}`);
2407
+ };
2408
+
2409
+ // start_aigc
2410
+ /**
2411
+ * 构建 pnpm add 命令
2412
+ */
2413
+ const buildPnpmCommand = (
2414
+ packageName,
2415
+ options
2416
+
2417
+
2418
+
2419
+
2420
+ ,
2421
+ ) => {
2422
+ const { global, version, registry, extraArgs } = options;
2423
+
2424
+ const parts = ['pnpm', 'add'];
2425
+
2426
+ // 添加全局标记
2427
+ if (global) {
2428
+ parts.push('-g');
2429
+ }
2430
+
2431
+ // 添加包名和版本
2432
+ if (version && version !== 'latest') {
2433
+ parts.push(`${packageName}@${version}`);
2434
+ } else {
2435
+ parts.push(`${packageName}@latest`);
2436
+ }
2437
+
2438
+ // 添加 registry
2439
+ if (registry) {
2440
+ parts.push(`--registry=${registry}`);
2441
+ }
2442
+
2443
+ // 添加额外参数
2444
+ if (extraArgs.length > 0) {
2445
+ parts.push(...extraArgs);
2446
+ }
2447
+
2448
+ return parts.join(' ');
2449
+ };
2450
+ // end_aigc
2451
+
2452
+ // start_aigc
2453
+ /**
2454
+ * 执行 update 命令的内部实现
2455
+ */
2456
+ const executeUpdate = (
2457
+ packageName,
2458
+ options
2459
+
2460
+
2461
+
2462
+
2463
+
2464
+
2465
+ ,
2466
+ ) => {
2467
+ let logStream = null;
2468
+
2469
+ try {
2470
+ const { global, cwd, version, registry, logFile, extraArgs } = options;
2471
+
2472
+ // 准备日志
2473
+ const logFilePath = resolveLogFilePath(logFile);
2474
+
2475
+ // 调试:确认日志路径
2476
+ logger.info(`Log file path resolved to: ${logFilePath}`);
2477
+
2478
+ logStream = createLogStream(logFilePath);
2479
+
2480
+ // 调试:确认流已创建
2481
+ logger.info('Log stream created successfully');
2482
+
2483
+ logWithFile(logStream, 'info', `Updating package: ${packageName}`);
2484
+
2485
+ // 构建命令
2486
+ const command = buildPnpmCommand(packageName, {
2487
+ global,
2488
+ version,
2489
+ registry,
2490
+ extraArgs,
2491
+ });
2492
+
2493
+ // 确定工作目录
2494
+ const workingDir = cwd
2495
+ ? path.isAbsolute(cwd)
2496
+ ? cwd
2497
+ : path.join(process.cwd(), cwd)
2498
+ : process.cwd();
2499
+
2500
+ logWithFile(logStream, 'info', `Executing: ${command}`);
2501
+ logWithFile(logStream, 'info', `Working directory: ${workingDir}`);
2502
+ logWithFile(logStream, 'info', `Log file: ${logFilePath}`);
2503
+
2504
+ // 记录命令开始时间
2505
+ writeLogWithTimestamp(logStream, '--- Command execution started ---');
2506
+
2507
+ // 同步执行命令
2508
+ const result = shelljs.exec(command, {
2509
+ cwd: workingDir,
2510
+ silent: true, // 使用 silent 来捕获输出
2511
+ });
2512
+
2513
+ // 将输出写入控制台和日志文件(带时间戳)
2514
+ if (result.stdout) {
2515
+ process.stdout.write(result.stdout);
2516
+ writeLogWithTimestamp(logStream, result.stdout.trim());
2517
+ }
2518
+
2519
+ if (result.stderr) {
2520
+ process.stderr.write(result.stderr);
2521
+ writeLogWithTimestamp(logStream, result.stderr.trim());
2522
+ }
2523
+
2524
+ // 记录命令结束时间
2525
+ writeLogWithTimestamp(logStream, '--- Command execution ended ---');
2526
+
2527
+ // 检查执行结果并记录到日志
2528
+ if (result.code === 0) {
2529
+ logWithFile(logStream, 'success', 'Package updated successfully');
2530
+ logWithFile(logStream, 'info', `Log file: ${logFilePath}`);
2531
+ } else {
2532
+ logWithFile(
2533
+ logStream,
2534
+ 'error',
2535
+ `Command exited with code ${result.code}`,
2536
+ );
2537
+ logWithFile(
2538
+ logStream,
2539
+ 'error',
2540
+ `Check log file for details: ${logFilePath}`,
2541
+ );
2542
+ }
2543
+
2544
+ // 关闭日志流并等待写入完成
2545
+ logStream.end(() => {
2546
+ // 流关闭后再退出进程
2547
+ if (result.code !== 0) {
2548
+ process.exit(result.code || 1);
2549
+ }
2550
+ });
2551
+ } catch (error) {
2552
+ logger.error('Failed to update package:');
2553
+ logger.error(error instanceof Error ? error.message : String(error));
2554
+
2555
+ // 写入错误到日志文件
2556
+ if (logStream) {
2557
+ writeLogWithTimestamp(
2558
+ logStream,
2559
+ `[ERROR] ${error instanceof Error ? error.message : String(error)}`,
2560
+ );
2561
+ // 等待流关闭后再退出
2562
+ logStream.end(() => {
2563
+ process.exit(1);
2564
+ });
2565
+ } else {
2566
+ process.exit(1);
2567
+ }
2568
+ }
2569
+ };
2570
+ // end_aigc
2571
+
2572
+ /**
2573
+ * 注册 update 命令到 program
2574
+ */
2575
+ const registerCommand = program => {
2576
+ program
2577
+ .command('update <package>')
2578
+ .description('Update a package dependency')
2579
+ .option('-g, --global', 'Update package globally', false)
2580
+ .option('-c, --cwd <path>', 'Working directory for the update')
2581
+ .option(
2582
+ '--to <version>',
2583
+ 'Version to update to (default: latest)',
2584
+ 'latest',
2585
+ )
2586
+ .option('--registry <url>', 'Registry URL to use for the update')
2587
+ .option('--log-file <path>', 'Log file path')
2588
+ .allowUnknownOption() // 允许透传参数给 pnpm
2589
+ .action((packageName, options, command) => {
2590
+ // 收集所有未知选项作为额外参数
2591
+ const extraArgs = command.args.slice(1);
2592
+
2593
+ executeUpdate(packageName, {
2594
+ ...options,
2595
+ version: options.to, // 将 --to 映射到 version
2596
+ extraArgs,
2597
+ });
2301
2598
  });
2302
2599
  };
2303
2600
 
2304
- var version = "0.0.1-alpha.e772d9";
2601
+ var version = "0.0.1-alpha.e89608";
2305
2602
  var packageJson = {
2306
2603
  version: version};
2307
2604
 
2308
2605
  const commands = [
2309
- registerCommand,
2310
2606
  registerCommand$1,
2311
- registerCommand$3,
2312
2607
  registerCommand$2,
2608
+ registerCommand$4,
2609
+ registerCommand$3,
2610
+ registerCommand,
2313
2611
  ];
2314
2612
 
2315
2613
  const main = () => {