@coze-arch/cli 0.0.1-alpha.800d04 → 0.0.1-alpha.8058fa

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 (80) 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 +2 -2
  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 +20 -0
  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/package.json +3 -1
  14. package/lib/__templates__/expo/client/screens/demo/index.tsx +3 -3
  15. package/lib/__templates__/expo/eslint-plugins/fontawesome6/names.js +1886 -2483
  16. package/lib/__templates__/expo/eslint-plugins/fontawesome6/rule.js +20 -1
  17. package/lib/__templates__/expo/eslint-plugins/fontawesome6/v5-only-names.js +388 -0
  18. package/lib/__templates__/expo/eslint-plugins/react-native/index.js +9 -0
  19. package/lib/__templates__/expo/eslint-plugins/react-native/rule.js +64 -0
  20. package/lib/__templates__/expo/eslint-plugins/reanimated/index.js +9 -0
  21. package/lib/__templates__/expo/eslint-plugins/reanimated/rule.js +88 -0
  22. package/lib/__templates__/expo/package.json +3 -0
  23. package/lib/__templates__/expo/patches/expo@54.0.32.patch +45 -0
  24. package/lib/__templates__/expo/pnpm-lock.yaml +1211 -2702
  25. package/lib/__templates__/expo/server/package.json +1 -1
  26. package/lib/__templates__/nextjs/.babelrc +15 -0
  27. package/lib/__templates__/nextjs/next.config.ts +1 -1
  28. package/lib/__templates__/nextjs/package.json +9 -1
  29. package/lib/__templates__/nextjs/pnpm-lock.yaml +2603 -1728
  30. package/lib/__templates__/nextjs/src/app/layout.tsx +5 -3
  31. package/lib/__templates__/nextjs/src/app/page.tsx +17 -61
  32. package/lib/__templates__/taro/.coze +14 -0
  33. package/lib/__templates__/taro/.cozeproj/scripts/deploy_build.sh +19 -0
  34. package/lib/__templates__/taro/.cozeproj/scripts/deploy_run.sh +13 -0
  35. package/lib/__templates__/taro/.cozeproj/scripts/dev_build.sh +16 -0
  36. package/lib/__templates__/taro/.cozeproj/scripts/dev_run.sh +74 -0
  37. package/lib/__templates__/taro/.cozeproj/scripts/init_env.sh +5 -0
  38. package/lib/__templates__/taro/.cozeproj/scripts/pack.sh +1 -0
  39. package/lib/__templates__/taro/README.md +687 -0
  40. package/lib/__templates__/taro/_gitignore +40 -0
  41. package/lib/__templates__/taro/_npmrc +18 -0
  42. package/lib/__templates__/taro/babel.config.js +12 -0
  43. package/lib/__templates__/taro/config/dev.ts +9 -0
  44. package/lib/__templates__/taro/config/index.ts +173 -0
  45. package/lib/__templates__/taro/config/prod.ts +35 -0
  46. package/lib/__templates__/taro/eslint.config.mjs +57 -0
  47. package/lib/__templates__/taro/key/private.appid.key +0 -0
  48. package/lib/__templates__/taro/package.json +95 -0
  49. package/lib/__templates__/taro/pnpm-lock.yaml +22430 -0
  50. package/lib/__templates__/taro/pnpm-workspace.yaml +2 -0
  51. package/lib/__templates__/taro/project.config.json +15 -0
  52. package/lib/__templates__/taro/server/nest-cli.json +10 -0
  53. package/lib/__templates__/taro/server/package.json +38 -0
  54. package/lib/__templates__/taro/server/src/app.controller.ts +23 -0
  55. package/lib/__templates__/taro/server/src/app.module.ts +10 -0
  56. package/lib/__templates__/taro/server/src/app.service.ts +8 -0
  57. package/lib/__templates__/taro/server/src/interceptors/http-status.interceptor.ts +23 -0
  58. package/lib/__templates__/taro/server/src/main.ts +37 -0
  59. package/lib/__templates__/taro/server/tsconfig.json +24 -0
  60. package/lib/__templates__/taro/src/app.config.ts +11 -0
  61. package/lib/__templates__/taro/src/app.css +52 -0
  62. package/lib/__templates__/taro/src/app.ts +14 -0
  63. package/lib/__templates__/taro/src/index.html +50 -0
  64. package/lib/__templates__/taro/src/network.ts +39 -0
  65. package/lib/__templates__/taro/src/pages/index/index.config.ts +3 -0
  66. package/lib/__templates__/taro/src/pages/index/index.css +1 -0
  67. package/lib/__templates__/taro/src/pages/index/index.tsx +33 -0
  68. package/lib/__templates__/taro/src/utils/h5-styles.ts +22 -0
  69. package/lib/__templates__/taro/src/utils/wx-debug.ts +23 -0
  70. package/lib/__templates__/taro/stylelint.config.mjs +4 -0
  71. package/lib/__templates__/taro/template.config.js +68 -0
  72. package/lib/__templates__/taro/tsconfig.json +29 -0
  73. package/lib/__templates__/taro/types/global.d.ts +32 -0
  74. package/lib/__templates__/templates.json +32 -0
  75. package/lib/__templates__/vite/package.json +6 -1
  76. package/lib/__templates__/vite/pnpm-lock.yaml +504 -982
  77. package/lib/__templates__/vite/src/main.ts +17 -47
  78. package/lib/__templates__/vite/template.config.js +6 -4
  79. package/lib/cli.js +585 -102
  80. package/package.json +1 -1
package/lib/cli.js CHANGED
@@ -585,7 +585,7 @@ const executeWarmup = async (
585
585
  /**
586
586
  * 注册 warmup 命令到 program
587
587
  */
588
- const registerCommand$3 = program => {
588
+ const registerCommand$4 = program => {
589
589
  program
590
590
  .command('warmup')
591
591
  .description('Pre-install dependencies for templates to speed up init')
@@ -840,6 +840,7 @@ const isNextProject = (projectFolder) => {
840
840
  }
841
841
 
842
842
  try {
843
+ // eslint-disable-next-line no-restricted-syntax
843
844
  const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
844
845
  const deps = {
845
846
  ...packageJson.dependencies,
@@ -1032,7 +1033,7 @@ const executeFix = async (options = {}) => {
1032
1033
  /**
1033
1034
  * 注册 fix 命令到 program
1034
1035
  */
1035
- const registerCommand$2 = program => {
1036
+ const registerCommand$3 = program => {
1036
1037
  program
1037
1038
  .command('fix')
1038
1039
  .description(
@@ -1051,13 +1052,13 @@ function _nullishCoalesce$1(lhs, rhsFn) { if (lhs != null) { return lhs; } else
1051
1052
  /**
1052
1053
  * 日志文件名常量
1053
1054
  */
1054
- const LOG_FILE_NAME = 'dev.log';
1055
+ const LOG_FILE_NAME$1 = 'dev.log';
1055
1056
 
1056
1057
  /**
1057
1058
  * 获取日志目录
1058
1059
  * 优先使用环境变量 COZE_LOG_DIR,否则使用 ~/.coze-logs
1059
1060
  */
1060
- const getLogDir = () =>
1061
+ const getLogDir$1 = () =>
1061
1062
  process.env.COZE_LOG_DIR || path.join(os.homedir(), '.coze-logs');
1062
1063
 
1063
1064
  /**
@@ -1066,22 +1067,22 @@ const getLogDir = () =>
1066
1067
  * - 如果是相对路径,基于 getLogDir() + 相对路径
1067
1068
  * - 如果为空,使用 getLogDir() + LOG_FILE_NAME
1068
1069
  */
1069
- const resolveLogFilePath = (logFile) => {
1070
+ const resolveLogFilePath$1 = (logFile) => {
1070
1071
  if (!logFile) {
1071
- return path.join(getLogDir(), LOG_FILE_NAME);
1072
+ return path.join(getLogDir$1(), LOG_FILE_NAME$1);
1072
1073
  }
1073
1074
 
1074
1075
  if (path.isAbsolute(logFile)) {
1075
1076
  return logFile;
1076
1077
  }
1077
1078
 
1078
- return path.join(getLogDir(), logFile);
1079
+ return path.join(getLogDir$1(), logFile);
1079
1080
  };
1080
1081
 
1081
1082
  /**
1082
1083
  * 创建日志写入流
1083
1084
  */
1084
- const createLogStream = (logFilePath) => {
1085
+ const createLogStream$1 = (logFilePath) => {
1085
1086
  const logDir = path.dirname(logFilePath);
1086
1087
 
1087
1088
  // 确保日志目录存在
@@ -1104,9 +1105,14 @@ const executeRun = async (
1104
1105
  logger.info(`Running ${commandName} command...`);
1105
1106
 
1106
1107
  // 1. 对于 build 命令,先执行 fix 以确保项目配置正确
1107
- if (commandName === 'build') {
1108
+ if (['dev', 'build'].includes(commandName)) {
1108
1109
  logger.info('\n🔧 Running fix command before build...\n');
1109
- await executeFix();
1110
+ try {
1111
+ await executeFix();
1112
+ // eslint-disable-next-line @coze-arch/no-empty-catch
1113
+ } catch (e) {
1114
+ // just ignore
1115
+ }
1110
1116
  logger.info('');
1111
1117
  }
1112
1118
 
@@ -1115,8 +1121,8 @@ const executeRun = async (
1115
1121
  const commandArgs = getCommandConfig(config, commandName);
1116
1122
 
1117
1123
  // 3. 准备日志
1118
- const logFilePath = resolveLogFilePath(options.logFile);
1119
- const logStream = createLogStream(logFilePath);
1124
+ const logFilePath = resolveLogFilePath$1(options.logFile);
1125
+ const logStream = createLogStream$1(logFilePath);
1120
1126
 
1121
1127
  // 4. 执行命令
1122
1128
  const commandString = commandArgs.join(' ');
@@ -1179,7 +1185,7 @@ const executeRun = async (
1179
1185
  /**
1180
1186
  * 注册 dev/build/start 命令到 program
1181
1187
  */
1182
- const registerCommand$1 = program => {
1188
+ const registerCommand$2 = program => {
1183
1189
  // dev 命令
1184
1190
  program
1185
1191
  .command('dev')
@@ -1431,6 +1437,11 @@ const shouldIgnoreFile = (filePath) => {
1431
1437
  return directoryPatterns.some(dir => pathParts.includes(dir));
1432
1438
  };
1433
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
1434
1445
  /**
1435
1446
  * 递归获取目录中的所有文件
1436
1447
  *
@@ -1501,6 +1512,58 @@ const convertDotfileName = (filePath) => {
1501
1512
  return filePath;
1502
1513
  };
1503
1514
 
1515
+ /**
1516
+ * 复制 node_modules 目录(如果存在)
1517
+ *
1518
+ * @param sourceNodeModules - 源 node_modules 路径
1519
+ * @param targetNodeModules - 目标 node_modules 路径
1520
+ */
1521
+ const copyNodeModules = (
1522
+ sourceNodeModules,
1523
+ targetNodeModules,
1524
+ ) => {
1525
+ if (!fs.existsSync(sourceNodeModules)) {
1526
+ return;
1527
+ }
1528
+
1529
+ logger.info('\nCopying node_modules from pre-warmed template...');
1530
+ logger.verbose(` From: ${sourceNodeModules}`);
1531
+ logger.verbose(` To: ${targetNodeModules}`);
1532
+
1533
+ const result = shelljs.exec(`cp -R "${sourceNodeModules}" "${targetNodeModules}"`, {
1534
+ silent: true,
1535
+ });
1536
+
1537
+ if (result.stdout) {
1538
+ process.stdout.write(result.stdout);
1539
+ }
1540
+
1541
+ if (result.stderr) {
1542
+ process.stderr.write(result.stderr);
1543
+ }
1544
+
1545
+ if (result.code === 0) {
1546
+ logger.success('✓ node_modules copied successfully');
1547
+ } else {
1548
+ logger.warn(
1549
+ `Failed to copy node_modules: ${result.stderr || 'unknown error'}`,
1550
+ );
1551
+ logger.info('Will need to run pnpm install manually');
1552
+ }
1553
+ };
1554
+ // end_aigc
1555
+
1556
+ // ABOUTME: File rendering utilities for template processing
1557
+ // ABOUTME: Handles file content rendering, hook execution, and file writing
1558
+
1559
+
1560
+
1561
+
1562
+
1563
+
1564
+
1565
+
1566
+ // start_aigc
1504
1567
  /**
1505
1568
  * 执行文件渲染钩子
1506
1569
  *
@@ -1546,22 +1609,25 @@ const executeFileRenderHook = async (
1546
1609
  };
1547
1610
 
1548
1611
  /**
1549
- * 处理单个文件
1612
+ * 准备单个文件的渲染信息(不实际写入)
1613
+ * 用于 dry-run 阶段,收集所有将要写入的文件信息
1614
+ *
1615
+ * @param options - 准备选项
1616
+ * @returns 文件渲染信息,或 null 表示该文件被跳过
1550
1617
  */
1551
- const processSingleFile = async (options
1552
-
1618
+ const prepareFileInfo = async (options
1553
1619
 
1554
1620
 
1555
1621
 
1556
1622
 
1557
1623
  ) => {
1558
- const { file, templatePath, outputPath, context, templateConfig } = options;
1624
+ const { file, templatePath, context, templateConfig } = options;
1559
1625
 
1560
1626
  const srcPath = path.join(templatePath, file);
1561
1627
  const destFile = convertDotfileName(file);
1562
1628
 
1563
1629
  logger.verbose(
1564
- ` - Processing: ${file}${destFile !== file ? ` -> ${destFile}` : ''}`,
1630
+ ` - Preparing: ${file}${destFile !== file ? ` -> ${destFile}` : ''}`,
1565
1631
  );
1566
1632
 
1567
1633
  // 判断是否为二进制文件
@@ -1595,38 +1661,195 @@ const processSingleFile = async (options
1595
1661
  context,
1596
1662
  );
1597
1663
 
1598
- // 如果返回 null,跳过该文件
1664
+ // 如果返回 null,表示该文件被 hook 跳过
1599
1665
  if (processedFileInfo === null) {
1600
1666
  logger.verbose(' ⊘ Skipped by onFileRender hook');
1601
- return;
1667
+ return null;
1602
1668
  }
1603
1669
 
1604
- // 使用处理后的目标路径
1605
- const finalDestPath = path.join(outputPath, processedFileInfo.destPath);
1670
+ return processedFileInfo;
1671
+ };
1672
+
1673
+ /**
1674
+ * 写入渲染后的文件到目标路径
1675
+ *
1676
+ * @param options - 写入选项
1677
+ */
1678
+ const writeRenderedFile = async (options
1679
+
1680
+
1681
+
1682
+ ) => {
1683
+ const { fileInfo, srcPath, destPath } = options;
1684
+
1685
+ logger.verbose(` - Writing: ${fileInfo.destPath}`);
1606
1686
 
1607
1687
  // 确保目标目录存在
1608
- await ensureDir(path.dirname(finalDestPath));
1688
+ await ensureDir(path.dirname(destPath));
1609
1689
 
1610
1690
  // 写入文件
1611
- if (processedFileInfo.isBinary) {
1612
- // 二进制文件:如果内容没变,直接复制;否则从 base64 解码写入
1613
- if (processedFileInfo.content === content) {
1614
- await fs$1.copyFile(srcPath, finalDestPath);
1691
+ if (fileInfo.isBinary) {
1692
+ // 二进制文件:如果内容是原始 base64(未被 hook 修改),直接复制;否则从 base64 解码写入
1693
+ const buffer = await fs$1.readFile(srcPath);
1694
+ const originalContent = buffer.toString('base64');
1695
+
1696
+ if (fileInfo.content === originalContent) {
1697
+ await fs$1.copyFile(srcPath, destPath);
1615
1698
  logger.verbose(' ✓ Copied (binary)');
1616
1699
  } else {
1617
- const buffer = Buffer.from(processedFileInfo.content, 'base64');
1618
- await fs$1.writeFile(finalDestPath, buffer);
1700
+ const modifiedBuffer = Buffer.from(fileInfo.content, 'base64');
1701
+ await fs$1.writeFile(destPath, modifiedBuffer);
1619
1702
  logger.verbose(' ✓ Written (binary, modified by hook)');
1620
1703
  }
1621
1704
  } else {
1622
1705
  // 文本文件
1623
- await fs$1.writeFile(finalDestPath, processedFileInfo.content, 'utf-8');
1706
+ await fs$1.writeFile(destPath, fileInfo.content, 'utf-8');
1624
1707
  logger.verbose(' ✓ Rendered and written');
1625
1708
  }
1626
1709
  };
1710
+ // end_aigc
1711
+
1712
+ // ABOUTME: File conflict detection utilities for template processing
1713
+ // ABOUTME: Provides dry-run file collection and conflict checking
1714
+
1715
+
1716
+
1717
+
1718
+
1719
+
1720
+
1721
+ // start_aigc
1722
+ /**
1723
+ * 收集所有将要写入的文件路径(dry-run 阶段)
1724
+ *
1725
+ * @param options - 收集选项
1726
+ * @returns 将要写入的文件路径列表
1727
+ */
1728
+ const collectFilesToRender = async (options
1729
+
1730
+
1731
+
1732
+
1733
+ ) => {
1734
+ const { files, templatePath, context, templateConfig } = options;
1735
+
1736
+ logger.verbose('\nDry-run: Collecting files to render...');
1737
+
1738
+ const fileInfos = await Promise.all(
1739
+ files.map(file =>
1740
+ prepareFileInfo({
1741
+ file,
1742
+ templatePath,
1743
+ context,
1744
+ templateConfig,
1745
+ }),
1746
+ ),
1747
+ );
1748
+
1749
+ // 过滤掉被 hook 跳过的文件,收集 destPath
1750
+ const filesToWrite = fileInfos
1751
+ .filter((info) => info !== null)
1752
+ .map(info => info.destPath);
1753
+
1754
+ logger.verbose(` - ${filesToWrite.length} files will be written`);
1755
+ logger.verbose(
1756
+ ` - ${fileInfos.length - filesToWrite.length} files skipped by hooks`,
1757
+ );
1758
+
1759
+ return filesToWrite;
1760
+ };
1761
+
1762
+ /**
1763
+ * 检测文件冲突
1764
+ *
1765
+ * @param outputPath - 输出目录路径
1766
+ * @param filesToWrite - 将要写入的文件路径列表
1767
+ * @returns 冲突的文件路径列表
1768
+ */
1769
+ const detectFileConflicts = (
1770
+ outputPath,
1771
+ filesToWrite,
1772
+ ) => {
1773
+ logger.verbose('\nChecking for file conflicts...');
1774
+
1775
+ const conflicts = [];
1776
+
1777
+ for (const file of filesToWrite) {
1778
+ const fullPath = path.join(outputPath, file);
1779
+ if (fs.existsSync(fullPath)) {
1780
+ conflicts.push(file);
1781
+ logger.verbose(` ⚠ Conflict detected: ${file}`);
1782
+ }
1783
+ }
1784
+
1785
+ if (conflicts.length === 0) {
1786
+ logger.verbose(' ✓ No conflicts detected');
1787
+ } else {
1788
+ logger.verbose(` ⚠ ${conflicts.length} conflicts detected`);
1789
+ }
1790
+
1791
+ return conflicts;
1792
+ };
1793
+ // end_aigc
1794
+
1795
+ // ABOUTME: Main file processing orchestration for template rendering
1796
+ // ABOUTME: Coordinates file system, rendering, and conflict detection layers
1797
+
1798
+
1799
+
1800
+ // start_aigc
1801
+ /**
1802
+ * 处理单个文件(准备 + 写入)
1803
+ *
1804
+ * @param options - 处理选项
1805
+ */
1806
+ const processSingleFile = async (options
1807
+
1808
+
1809
+
1810
+
1811
+
1812
+ ) => {
1813
+ const { file, templatePath, outputPath, context, templateConfig } = options;
1814
+
1815
+ const srcPath = path.join(templatePath, file);
1816
+
1817
+ // 准备文件信息
1818
+ const processedFileInfo = await prepareFileInfo({
1819
+ file,
1820
+ templatePath,
1821
+ context,
1822
+ templateConfig,
1823
+ });
1824
+
1825
+ // 如果返回 null,跳过该文件
1826
+ if (processedFileInfo === null) {
1827
+ return;
1828
+ }
1829
+
1830
+ // 使用处理后的目标路径
1831
+ const finalDestPath = path.join(outputPath, processedFileInfo.destPath);
1832
+
1833
+ // 写入文件
1834
+ await writeRenderedFile({
1835
+ fileInfo: processedFileInfo,
1836
+ srcPath,
1837
+ destPath: finalDestPath,
1838
+ });
1839
+ };
1627
1840
 
1628
1841
  /**
1629
1842
  * 复制并处理模板文件到目标目录
1843
+ *
1844
+ * 流程:
1845
+ * 1. 验证模板目录
1846
+ * 2. 扫描所有模板文件
1847
+ * 3. Dry-run:收集将要写入的文件列表(考虑 hooks 影响)
1848
+ * 4. 冲突检测:检查是否有文件会被覆盖
1849
+ * 5. 实际写入:渲染并写入所有文件
1850
+ * 6. 复制 node_modules(如果存在)
1851
+ *
1852
+ * @param options - 处理选项
1630
1853
  */
1631
1854
  const processTemplateFiles = async (options
1632
1855
 
@@ -1639,7 +1862,7 @@ const processTemplateFiles = async (options
1639
1862
  logger.verbose(` - Template path: ${templatePath}`);
1640
1863
  logger.verbose(` - Output path: ${outputPath}`);
1641
1864
 
1642
- // 验证模板目录是否存在
1865
+ // 阶段 0: 验证模板目录是否存在
1643
1866
  try {
1644
1867
  const stat = await fs$1.stat(templatePath);
1645
1868
  logger.verbose(
@@ -1655,6 +1878,7 @@ const processTemplateFiles = async (options
1655
1878
  throw error;
1656
1879
  }
1657
1880
 
1881
+ // 阶段 1: 扫描所有模板文件
1658
1882
  const files = await getAllFiles(templatePath);
1659
1883
 
1660
1884
  logger.verbose(` - Found ${files.length} files to process`);
@@ -1664,6 +1888,29 @@ const processTemplateFiles = async (options
1664
1888
  return;
1665
1889
  }
1666
1890
 
1891
+ // 阶段 2: Dry-run - 收集所有将要写入的文件
1892
+ const filesToWrite = await collectFilesToRender({
1893
+ files,
1894
+ templatePath,
1895
+ context,
1896
+ templateConfig,
1897
+ });
1898
+
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.',
1909
+ );
1910
+ }
1911
+
1912
+ // 阶段 4: 实际写入文件
1913
+ logger.verbose('\nWriting files...');
1667
1914
  await Promise.all(
1668
1915
  files.map(file =>
1669
1916
  processSingleFile({
@@ -1678,74 +1925,13 @@ const processTemplateFiles = async (options
1678
1925
 
1679
1926
  logger.verbose('✓ All files processed successfully');
1680
1927
 
1681
- // 单独处理 node_modules 目录(如果存在)
1928
+ // 阶段 5: 单独处理 node_modules 目录(如果存在)
1682
1929
  const sourceNodeModules = path.join(templatePath, 'node_modules');
1683
1930
  const targetNodeModules = path.join(outputPath, 'node_modules');
1684
1931
 
1685
- if (fs.existsSync(sourceNodeModules)) {
1686
- logger.info('\nCopying node_modules from pre-warmed template...');
1687
- logger.verbose(` From: ${sourceNodeModules}`);
1688
- logger.verbose(` To: ${targetNodeModules}`);
1689
-
1690
- const result = shelljs.exec(`cp -R "${sourceNodeModules}" "${targetNodeModules}"`, {
1691
- silent: true,
1692
- });
1693
-
1694
- if (result.stdout) {
1695
- process.stdout.write(result.stdout);
1696
- }
1697
-
1698
- if (result.stderr) {
1699
- process.stderr.write(result.stderr);
1700
- }
1701
-
1702
- if (result.code === 0) {
1703
- logger.success('✓ node_modules copied successfully');
1704
- } else {
1705
- logger.warn(
1706
- `Failed to copy node_modules: ${result.stderr || 'unknown error'}`,
1707
- );
1708
- logger.info('Will need to run pnpm install manually');
1709
- }
1710
- }
1711
- };
1712
-
1713
- /**
1714
- * 检查输出目录是否为空
1715
- * 注意:.git 目录会被忽略,允许在已初始化 git 的目录中创建项目
1716
- *
1717
- * @param outputPath - 输出目录路径
1718
- * @returns 是否为空
1719
- */
1720
- const isOutputDirEmpty = async (
1721
- outputPath,
1722
- ) => {
1723
- try {
1724
- const entries = await fs$1.readdir(outputPath);
1725
- // 过滤掉 .git 目录,允许在已初始化 git 的目录中创建项目
1726
- const filteredEntries = entries.filter(entry => entry !== '.git');
1727
- return filteredEntries.length === 0;
1728
- } catch (e) {
1729
- // 目录不存在,视为空
1730
- return true;
1731
- }
1732
- };
1733
-
1734
- /**
1735
- * 验证输出目录
1736
- *
1737
- * @param outputPath - 输出目录路径
1738
- * @throws 如果目录不为空则抛出错误
1739
- */
1740
- const validateOutputDir = async (outputPath) => {
1741
- const isEmpty = await isOutputDirEmpty(outputPath);
1742
- if (!isEmpty) {
1743
- throw new Error(
1744
- `Output directory is not empty: ${outputPath}\n` +
1745
- 'Please use an empty directory or remove existing files.',
1746
- );
1747
- }
1932
+ copyNodeModules(sourceNodeModules, targetNodeModules);
1748
1933
  };
1934
+ // end_aigc
1749
1935
 
1750
1936
  /**
1751
1937
  * 模板引擎执行选项
@@ -1828,9 +2014,9 @@ const executeAfterRenderHook = async (
1828
2014
  /**
1829
2015
  * 准备输出目录
1830
2016
  */
1831
- const prepareOutputDirectory = async (outputPath) => {
2017
+ const prepareOutputDirectory = (outputPath) => {
1832
2018
  const absolutePath = path.resolve(process.cwd(), outputPath);
1833
- await validateOutputDir(absolutePath);
2019
+ // 不再在这里验证目录是否为空,冲突检测已移至 processTemplateFiles 中
1834
2020
  return absolutePath;
1835
2021
  };
1836
2022
 
@@ -1857,7 +2043,7 @@ const execute = async (
1857
2043
  });
1858
2044
 
1859
2045
  // 5. 准备输出目录
1860
- const absoluteOutputPath = await prepareOutputDirectory(outputPath);
2046
+ const absoluteOutputPath = prepareOutputDirectory(outputPath);
1861
2047
 
1862
2048
  // 6. 处理模板文件
1863
2049
  await processTemplateFiles({
@@ -2097,7 +2283,7 @@ const executeInit = async (
2097
2283
  /**
2098
2284
  * 注册 init 命令到 program
2099
2285
  */
2100
- const registerCommand = program => {
2286
+ const registerCommand$1 = program => {
2101
2287
  program
2102
2288
  .command('init')
2103
2289
  .description('Initialize a new project from a template')
@@ -2115,15 +2301,312 @@ const registerCommand = program => {
2115
2301
  });
2116
2302
  };
2117
2303
 
2118
- var version = "0.0.1-alpha.800d04";
2304
+ // ABOUTME: This file implements the update command for coze CLI
2305
+ // ABOUTME: It wraps pnpm update/install to update package dependencies with logging support
2306
+
2307
+
2308
+
2309
+
2310
+ /**
2311
+ * 日志文件名常量
2312
+ */
2313
+ const LOG_FILE_NAME = 'update.log';
2314
+
2315
+ /**
2316
+ * 获取日志目录
2317
+ * 优先使用环境变量 COZE_LOG_DIR,否则使用 ~/.coze-logs
2318
+ */
2319
+ const getLogDir = () =>
2320
+ process.env.COZE_LOG_DIR || path.join(os.homedir(), '.coze-logs');
2321
+
2322
+ /**
2323
+ * 解析日志文件路径
2324
+ * - 如果是绝对路径,直接使用
2325
+ * - 如果是相对路径,基于 getLogDir() + 相对路径
2326
+ * - 如果为空,使用 getLogDir() + LOG_FILE_NAME
2327
+ */
2328
+ const resolveLogFilePath = (logFile) => {
2329
+ if (!logFile) {
2330
+ return path.join(getLogDir(), LOG_FILE_NAME);
2331
+ }
2332
+
2333
+ if (path.isAbsolute(logFile)) {
2334
+ return logFile;
2335
+ }
2336
+
2337
+ return path.join(getLogDir(), logFile);
2338
+ };
2339
+
2340
+ /**
2341
+ * 创建日志写入流
2342
+ */
2343
+ const createLogStream = (logFilePath) => {
2344
+ const logDir = path.dirname(logFilePath);
2345
+
2346
+ // 确保日志目录存在
2347
+ if (!fs.existsSync(logDir)) {
2348
+ fs.mkdirSync(logDir, { recursive: true });
2349
+ }
2350
+
2351
+ // 使用 'w' 标志覆盖之前的日志
2352
+ return fs.createWriteStream(logFilePath, { flags: 'w' });
2353
+ };
2354
+
2355
+ /**
2356
+ * 格式化时间戳
2357
+ */
2358
+ const formatTimestamp = () => {
2359
+ const now = new Date();
2360
+ return now.toISOString();
2361
+ };
2362
+
2363
+ /**
2364
+ * 写入带时间戳的日志
2365
+ */
2366
+ const writeLogWithTimestamp = (stream, message) => {
2367
+ const timestamp = formatTimestamp();
2368
+ const lines = message.split('\n');
2369
+ lines.forEach(line => {
2370
+ if (line) {
2371
+ stream.write(`[${timestamp}] ${line}\n`);
2372
+ } else {
2373
+ stream.write('\n');
2374
+ }
2375
+ });
2376
+ // 确保数据写入磁盘
2377
+ stream.uncork();
2378
+ };
2379
+
2380
+ /**
2381
+ * 同时输出到控制台和日志文件
2382
+ */
2383
+ const logWithFile = (
2384
+ stream,
2385
+ level,
2386
+ message,
2387
+ ) => {
2388
+ // 输出到控制台
2389
+ switch (level) {
2390
+ case 'info':
2391
+ logger.info(message);
2392
+ break;
2393
+ case 'success':
2394
+ logger.success(message);
2395
+ break;
2396
+ case 'error':
2397
+ logger.error(message);
2398
+ break;
2399
+ default:
2400
+ logger.info(message);
2401
+ break;
2402
+ }
2403
+
2404
+ // 写入日志文件(带时间戳)
2405
+ writeLogWithTimestamp(stream, `[${level.toUpperCase()}] ${message}`);
2406
+ };
2407
+
2408
+ // start_aigc
2409
+ /**
2410
+ * 构建 pnpm add 命令
2411
+ */
2412
+ const buildPnpmCommand = (
2413
+ packageName,
2414
+ options
2415
+
2416
+
2417
+
2418
+
2419
+ ,
2420
+ ) => {
2421
+ const { global, version, registry, extraArgs } = options;
2422
+
2423
+ const parts = ['pnpm', 'add'];
2424
+
2425
+ // 添加全局标记
2426
+ if (global) {
2427
+ parts.push('-g');
2428
+ }
2429
+
2430
+ // 添加包名和版本
2431
+ if (version && version !== 'latest') {
2432
+ parts.push(`${packageName}@${version}`);
2433
+ } else {
2434
+ parts.push(`${packageName}@latest`);
2435
+ }
2436
+
2437
+ // 添加 registry
2438
+ if (registry) {
2439
+ parts.push(`--registry=${registry}`);
2440
+ }
2441
+
2442
+ // 添加额外参数
2443
+ if (extraArgs.length > 0) {
2444
+ parts.push(...extraArgs);
2445
+ }
2446
+
2447
+ return parts.join(' ');
2448
+ };
2449
+ // end_aigc
2450
+
2451
+ // start_aigc
2452
+ /**
2453
+ * 执行 update 命令的内部实现
2454
+ */
2455
+ const executeUpdate = (
2456
+ packageName,
2457
+ options
2458
+
2459
+
2460
+
2461
+
2462
+
2463
+
2464
+ ,
2465
+ ) => {
2466
+ let logStream = null;
2467
+
2468
+ try {
2469
+ const { global, cwd, version, registry, logFile, extraArgs } = options;
2470
+
2471
+ // 准备日志
2472
+ const logFilePath = resolveLogFilePath(logFile);
2473
+
2474
+ // 调试:确认日志路径
2475
+ logger.info(`Log file path resolved to: ${logFilePath}`);
2476
+
2477
+ logStream = createLogStream(logFilePath);
2478
+
2479
+ // 调试:确认流已创建
2480
+ logger.info('Log stream created successfully');
2481
+
2482
+ logWithFile(logStream, 'info', `Updating package: ${packageName}`);
2483
+
2484
+ // 构建命令
2485
+ const command = buildPnpmCommand(packageName, {
2486
+ global,
2487
+ version,
2488
+ registry,
2489
+ extraArgs,
2490
+ });
2491
+
2492
+ // 确定工作目录
2493
+ const workingDir = cwd
2494
+ ? path.isAbsolute(cwd)
2495
+ ? cwd
2496
+ : path.join(process.cwd(), cwd)
2497
+ : process.cwd();
2498
+
2499
+ logWithFile(logStream, 'info', `Executing: ${command}`);
2500
+ logWithFile(logStream, 'info', `Working directory: ${workingDir}`);
2501
+ logWithFile(logStream, 'info', `Log file: ${logFilePath}`);
2502
+
2503
+ // 记录命令开始时间
2504
+ writeLogWithTimestamp(logStream, '--- Command execution started ---');
2505
+
2506
+ // 同步执行命令
2507
+ const result = shelljs.exec(command, {
2508
+ cwd: workingDir,
2509
+ silent: true, // 使用 silent 来捕获输出
2510
+ });
2511
+
2512
+ // 将输出写入控制台和日志文件(带时间戳)
2513
+ if (result.stdout) {
2514
+ process.stdout.write(result.stdout);
2515
+ writeLogWithTimestamp(logStream, result.stdout.trim());
2516
+ }
2517
+
2518
+ if (result.stderr) {
2519
+ process.stderr.write(result.stderr);
2520
+ writeLogWithTimestamp(logStream, result.stderr.trim());
2521
+ }
2522
+
2523
+ // 记录命令结束时间
2524
+ writeLogWithTimestamp(logStream, '--- Command execution ended ---');
2525
+
2526
+ // 检查执行结果并记录到日志
2527
+ if (result.code === 0) {
2528
+ logWithFile(logStream, 'success', 'Package updated successfully');
2529
+ logWithFile(logStream, 'info', `Log file: ${logFilePath}`);
2530
+ } else {
2531
+ logWithFile(
2532
+ logStream,
2533
+ 'error',
2534
+ `Command exited with code ${result.code}`,
2535
+ );
2536
+ logWithFile(
2537
+ logStream,
2538
+ 'error',
2539
+ `Check log file for details: ${logFilePath}`,
2540
+ );
2541
+ }
2542
+
2543
+ // 关闭日志流并等待写入完成
2544
+ logStream.end(() => {
2545
+ // 流关闭后再退出进程
2546
+ if (result.code !== 0) {
2547
+ process.exit(result.code || 1);
2548
+ }
2549
+ });
2550
+ } catch (error) {
2551
+ logger.error('Failed to update package:');
2552
+ logger.error(error instanceof Error ? error.message : String(error));
2553
+
2554
+ // 写入错误到日志文件
2555
+ if (logStream) {
2556
+ writeLogWithTimestamp(
2557
+ logStream,
2558
+ `[ERROR] ${error instanceof Error ? error.message : String(error)}`,
2559
+ );
2560
+ // 等待流关闭后再退出
2561
+ logStream.end(() => {
2562
+ process.exit(1);
2563
+ });
2564
+ } else {
2565
+ process.exit(1);
2566
+ }
2567
+ }
2568
+ };
2569
+ // end_aigc
2570
+
2571
+ /**
2572
+ * 注册 update 命令到 program
2573
+ */
2574
+ const registerCommand = program => {
2575
+ program
2576
+ .command('update <package>')
2577
+ .description('Update a package dependency')
2578
+ .option('-g, --global', 'Update package globally', false)
2579
+ .option('-c, --cwd <path>', 'Working directory for the update')
2580
+ .option(
2581
+ '--to <version>',
2582
+ 'Version to update to (default: latest)',
2583
+ 'latest',
2584
+ )
2585
+ .option('--registry <url>', 'Registry URL to use for the update')
2586
+ .option('--log-file <path>', 'Log file path')
2587
+ .allowUnknownOption() // 允许透传参数给 pnpm
2588
+ .action((packageName, options, command) => {
2589
+ // 收集所有未知选项作为额外参数
2590
+ const extraArgs = command.args.slice(1);
2591
+
2592
+ executeUpdate(packageName, {
2593
+ ...options,
2594
+ version: options.to, // 将 --to 映射到 version
2595
+ extraArgs,
2596
+ });
2597
+ });
2598
+ };
2599
+
2600
+ var version = "0.0.1-alpha.8058fa";
2119
2601
  var packageJson = {
2120
2602
  version: version};
2121
2603
 
2122
2604
  const commands = [
2123
- registerCommand,
2124
2605
  registerCommand$1,
2125
- registerCommand$3,
2126
2606
  registerCommand$2,
2607
+ registerCommand$4,
2608
+ registerCommand$3,
2609
+ registerCommand,
2127
2610
  ];
2128
2611
 
2129
2612
  const main = () => {