@coze-arch/cli 0.0.1-alpha.f91253 → 0.0.1-alpha.fd3d56

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.
package/lib/cli.js CHANGED
@@ -1105,7 +1105,7 @@ const executeRun = async (
1105
1105
  logger.info(`Running ${commandName} command...`);
1106
1106
 
1107
1107
  // 1. 对于 build 命令,先执行 fix 以确保项目配置正确
1108
- if (commandName === 'build') {
1108
+ if (['dev', 'build'].includes(commandName)) {
1109
1109
  logger.info('\n🔧 Running fix command before build...\n');
1110
1110
  try {
1111
1111
  await executeFix();
@@ -1437,6 +1437,11 @@ const shouldIgnoreFile = (filePath) => {
1437
1437
  return directoryPatterns.some(dir => pathParts.includes(dir));
1438
1438
  };
1439
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
1440
1445
  /**
1441
1446
  * 递归获取目录中的所有文件
1442
1447
  *
@@ -1507,6 +1512,58 @@ const convertDotfileName = (filePath) => {
1507
1512
  return filePath;
1508
1513
  };
1509
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
1510
1567
  /**
1511
1568
  * 执行文件渲染钩子
1512
1569
  *
@@ -1552,22 +1609,25 @@ const executeFileRenderHook = async (
1552
1609
  };
1553
1610
 
1554
1611
  /**
1555
- * 处理单个文件
1612
+ * 准备单个文件的渲染信息(不实际写入)
1613
+ * 用于 dry-run 阶段,收集所有将要写入的文件信息
1614
+ *
1615
+ * @param options - 准备选项
1616
+ * @returns 文件渲染信息,或 null 表示该文件被跳过
1556
1617
  */
1557
- const processSingleFile = async (options
1558
-
1618
+ const prepareFileInfo = async (options
1559
1619
 
1560
1620
 
1561
1621
 
1562
1622
 
1563
1623
  ) => {
1564
- const { file, templatePath, outputPath, context, templateConfig } = options;
1624
+ const { file, templatePath, context, templateConfig } = options;
1565
1625
 
1566
1626
  const srcPath = path.join(templatePath, file);
1567
1627
  const destFile = convertDotfileName(file);
1568
1628
 
1569
1629
  logger.verbose(
1570
- ` - Processing: ${file}${destFile !== file ? ` -> ${destFile}` : ''}`,
1630
+ ` - Preparing: ${file}${destFile !== file ? ` -> ${destFile}` : ''}`,
1571
1631
  );
1572
1632
 
1573
1633
  // 判断是否为二进制文件
@@ -1601,38 +1661,195 @@ const processSingleFile = async (options
1601
1661
  context,
1602
1662
  );
1603
1663
 
1604
- // 如果返回 null,跳过该文件
1664
+ // 如果返回 null,表示该文件被 hook 跳过
1605
1665
  if (processedFileInfo === null) {
1606
1666
  logger.verbose(' ⊘ Skipped by onFileRender hook');
1607
- return;
1667
+ return null;
1608
1668
  }
1609
1669
 
1610
- // 使用处理后的目标路径
1611
- 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}`);
1612
1686
 
1613
1687
  // 确保目标目录存在
1614
- await ensureDir(path.dirname(finalDestPath));
1688
+ await ensureDir(path.dirname(destPath));
1615
1689
 
1616
1690
  // 写入文件
1617
- if (processedFileInfo.isBinary) {
1618
- // 二进制文件:如果内容没变,直接复制;否则从 base64 解码写入
1619
- if (processedFileInfo.content === content) {
1620
- 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);
1621
1698
  logger.verbose(' ✓ Copied (binary)');
1622
1699
  } else {
1623
- const buffer = Buffer.from(processedFileInfo.content, 'base64');
1624
- await fs$1.writeFile(finalDestPath, buffer);
1700
+ const modifiedBuffer = Buffer.from(fileInfo.content, 'base64');
1701
+ await fs$1.writeFile(destPath, modifiedBuffer);
1625
1702
  logger.verbose(' ✓ Written (binary, modified by hook)');
1626
1703
  }
1627
1704
  } else {
1628
1705
  // 文本文件
1629
- await fs$1.writeFile(finalDestPath, processedFileInfo.content, 'utf-8');
1706
+ await fs$1.writeFile(destPath, fileInfo.content, 'utf-8');
1630
1707
  logger.verbose(' ✓ Rendered and written');
1631
1708
  }
1632
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
+ };
1633
1840
 
1634
1841
  /**
1635
1842
  * 复制并处理模板文件到目标目录
1843
+ *
1844
+ * 流程:
1845
+ * 1. 验证模板目录
1846
+ * 2. 扫描所有模板文件
1847
+ * 3. Dry-run:收集将要写入的文件列表(考虑 hooks 影响)
1848
+ * 4. 冲突检测:检查是否有文件会被覆盖
1849
+ * 5. 实际写入:渲染并写入所有文件
1850
+ * 6. 复制 node_modules(如果存在)
1851
+ *
1852
+ * @param options - 处理选项
1636
1853
  */
1637
1854
  const processTemplateFiles = async (options
1638
1855
 
@@ -1645,7 +1862,7 @@ const processTemplateFiles = async (options
1645
1862
  logger.verbose(` - Template path: ${templatePath}`);
1646
1863
  logger.verbose(` - Output path: ${outputPath}`);
1647
1864
 
1648
- // 验证模板目录是否存在
1865
+ // 阶段 0: 验证模板目录是否存在
1649
1866
  try {
1650
1867
  const stat = await fs$1.stat(templatePath);
1651
1868
  logger.verbose(
@@ -1661,6 +1878,7 @@ const processTemplateFiles = async (options
1661
1878
  throw error;
1662
1879
  }
1663
1880
 
1881
+ // 阶段 1: 扫描所有模板文件
1664
1882
  const files = await getAllFiles(templatePath);
1665
1883
 
1666
1884
  logger.verbose(` - Found ${files.length} files to process`);
@@ -1670,6 +1888,29 @@ const processTemplateFiles = async (options
1670
1888
  return;
1671
1889
  }
1672
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...');
1673
1914
  await Promise.all(
1674
1915
  files.map(file =>
1675
1916
  processSingleFile({
@@ -1684,74 +1925,13 @@ const processTemplateFiles = async (options
1684
1925
 
1685
1926
  logger.verbose('✓ All files processed successfully');
1686
1927
 
1687
- // 单独处理 node_modules 目录(如果存在)
1928
+ // 阶段 5: 单独处理 node_modules 目录(如果存在)
1688
1929
  const sourceNodeModules = path.join(templatePath, 'node_modules');
1689
1930
  const targetNodeModules = path.join(outputPath, 'node_modules');
1690
1931
 
1691
- if (fs.existsSync(sourceNodeModules)) {
1692
- logger.info('\nCopying node_modules from pre-warmed template...');
1693
- logger.verbose(` From: ${sourceNodeModules}`);
1694
- logger.verbose(` To: ${targetNodeModules}`);
1695
-
1696
- const result = shelljs.exec(`cp -R "${sourceNodeModules}" "${targetNodeModules}"`, {
1697
- silent: true,
1698
- });
1699
-
1700
- if (result.stdout) {
1701
- process.stdout.write(result.stdout);
1702
- }
1703
-
1704
- if (result.stderr) {
1705
- process.stderr.write(result.stderr);
1706
- }
1707
-
1708
- if (result.code === 0) {
1709
- logger.success('✓ node_modules copied successfully');
1710
- } else {
1711
- logger.warn(
1712
- `Failed to copy node_modules: ${result.stderr || 'unknown error'}`,
1713
- );
1714
- logger.info('Will need to run pnpm install manually');
1715
- }
1716
- }
1717
- };
1718
-
1719
- /**
1720
- * 检查输出目录是否为空
1721
- * 注意:.git 目录会被忽略,允许在已初始化 git 的目录中创建项目
1722
- *
1723
- * @param outputPath - 输出目录路径
1724
- * @returns 是否为空
1725
- */
1726
- const isOutputDirEmpty = async (
1727
- outputPath,
1728
- ) => {
1729
- try {
1730
- const entries = await fs$1.readdir(outputPath);
1731
- // 过滤掉 .git 目录,允许在已初始化 git 的目录中创建项目
1732
- const filteredEntries = entries.filter(entry => entry !== '.git');
1733
- return filteredEntries.length === 0;
1734
- } catch (e) {
1735
- // 目录不存在,视为空
1736
- return true;
1737
- }
1738
- };
1739
-
1740
- /**
1741
- * 验证输出目录
1742
- *
1743
- * @param outputPath - 输出目录路径
1744
- * @throws 如果目录不为空则抛出错误
1745
- */
1746
- const validateOutputDir = async (outputPath) => {
1747
- const isEmpty = await isOutputDirEmpty(outputPath);
1748
- if (!isEmpty) {
1749
- throw new Error(
1750
- `Output directory is not empty: ${outputPath}\n` +
1751
- 'Please use an empty directory or remove existing files.',
1752
- );
1753
- }
1932
+ copyNodeModules(sourceNodeModules, targetNodeModules);
1754
1933
  };
1934
+ // end_aigc
1755
1935
 
1756
1936
  /**
1757
1937
  * 模板引擎执行选项
@@ -1834,9 +2014,9 @@ const executeAfterRenderHook = async (
1834
2014
  /**
1835
2015
  * 准备输出目录
1836
2016
  */
1837
- const prepareOutputDirectory = async (outputPath) => {
2017
+ const prepareOutputDirectory = (outputPath) => {
1838
2018
  const absolutePath = path.resolve(process.cwd(), outputPath);
1839
- await validateOutputDir(absolutePath);
2019
+ // 不再在这里验证目录是否为空,冲突检测已移至 processTemplateFiles 中
1840
2020
  return absolutePath;
1841
2021
  };
1842
2022
 
@@ -1863,7 +2043,7 @@ const execute = async (
1863
2043
  });
1864
2044
 
1865
2045
  // 5. 准备输出目录
1866
- const absoluteOutputPath = await prepareOutputDirectory(outputPath);
2046
+ const absoluteOutputPath = prepareOutputDirectory(outputPath);
1867
2047
 
1868
2048
  // 6. 处理模板文件
1869
2049
  await processTemplateFiles({
@@ -2121,7 +2301,7 @@ const registerCommand = program => {
2121
2301
  });
2122
2302
  };
2123
2303
 
2124
- var version = "0.0.1-alpha.f91253";
2304
+ var version = "0.0.1-alpha.fd3d56";
2125
2305
  var packageJson = {
2126
2306
  version: version};
2127
2307
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coze-arch/cli",
3
- "version": "0.0.1-alpha.f91253",
3
+ "version": "0.0.1-alpha.fd3d56",
4
4
  "private": false,
5
5
  "description": "coze coding devtools cli",
6
6
  "license": "MIT",