openai-compatible-task-master
Version:
使用MCP解析PRD文档并生成任务列表
114 lines • 5.13 kB
JavaScript
import * as fs from 'fs';
import * as path from 'path';
import chalk from 'chalk';
/**
* 基础复制函数,处理文件查找和复制的通用逻辑
* @param templateRoot 模板根目录(npm包位置)
* @param workingRoot 工作根目录(用户运行命令位置)
* @param sourcePath 源文件/目录相对路径
* @param targetPath 目标文件/目录绝对路径
* @param isDirectory 是否是目录复制
* @returns 找到的源路径,如果未找到则返回null
*/
function baseCopyTemplate(templateRoot, workingRoot, sourcePath, targetPath, isDirectory) {
// 尝试在不同位置查找源文件/目录
const possiblePaths = [
path.join(templateRoot, sourcePath), // npm包路径
path.join(workingRoot, sourcePath) // 当前工作目录
];
// 查找第一个存在的源路径
const sourceAbsolutePath = possiblePaths.find(p => {
if (!fs.existsSync(p))
return false;
return isDirectory ? fs.statSync(p).isDirectory() : true;
});
if (!sourceAbsolutePath) {
console.error(chalk.red(`✗ 源${isDirectory ? '目录' : '文件'}不存在: `) + chalk.white('已尝试以下路径:'));
possiblePaths.forEach(p => console.error(' ' + chalk.gray(p)));
return null;
}
// 获取/创建目标目录
const targetDir = isDirectory ? targetPath : path.dirname(targetPath);
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true });
console.log(chalk.green('✓ ') + chalk.white('已创建目录: ') + chalk.underline.cyan(targetDir));
}
return sourceAbsolutePath;
}
/**
* 复制单个模板文件到指定位置,增强版本支持多路径查找
* @param templateRoot 模板根目录(npm包位置)
* @param workingRoot 工作根目录(用户运行命令位置)
* @param sourcePath 源文件相对路径
* @param targetPath 目标文件绝对路径
* @returns 成功返回true,失败返回false
*/
export function copyTemplateFile(templateRoot, workingRoot, sourcePath, targetPath) {
// 使用基础复制函数处理通用逻辑
const sourceAbsolutePath = baseCopyTemplate(templateRoot, workingRoot, sourcePath, targetPath, false);
if (!sourceAbsolutePath) {
return false;
}
try {
// 复制文件 (fs.copyFileSync 会直接覆盖)
fs.copyFileSync(sourceAbsolutePath, targetPath);
console.log(chalk.green('✓ ') + chalk.white('已复制文件: ') + chalk.underline.cyan(targetPath));
console.log(chalk.blue('i ') + chalk.white('源文件路径: ') + chalk.underline.cyan(sourceAbsolutePath));
return true;
}
catch (error) {
console.error(chalk.red('✗ ') + chalk.white('复制文件失败: ') + chalk.underline.cyan(targetPath));
console.error(' ' + error.message);
return false;
}
}
/**
* 复制模板目录下的所有文件到目标目录
* @param templateRoot 模板根目录(npm包位置)
* @param workingRoot 工作根目录(用户运行命令位置)
* @param sourceDir 源目录相对路径
* @param targetDir 目标目录相对路径
* @returns 成功返回true,失败返回false
*/
export function copyTemplateDirectory(templateRoot, workingRoot, sourceDir, targetDir) {
// 使用基础复制函数处理通用逻辑
const sourceDirPath = baseCopyTemplate(templateRoot, workingRoot, sourceDir, targetDir, true);
if (!sourceDirPath) {
return false;
}
try {
// 读取源目录中的所有文件
const files = fs.readdirSync(sourceDirPath);
if (files.length === 0) {
console.log(chalk.yellow('! ') + chalk.white('源目录为空: ') + chalk.underline.cyan(sourceDirPath));
return true;
}
// 逐个复制文件
let filesCopied = 0;
for (const file of files) {
const sourceFilePath = path.join(sourceDirPath, file);
const targetFilePath = path.join(targetDir, file);
// 仅处理文件(不处理子目录)
if (fs.statSync(sourceFilePath).isFile()) {
try {
// 复制文件 (fs.copyFileSync 会直接覆盖)
fs.copyFileSync(sourceFilePath, targetFilePath);
console.log(chalk.green('✓ ') + chalk.white('已复制文件: ') + chalk.underline.cyan(targetFilePath));
filesCopied++;
}
catch (error) {
console.error(chalk.red('✗ ') + chalk.white('复制文件失败: ') + chalk.underline.cyan(sourceFilePath));
console.error(' ' + error.message);
}
}
}
console.log(chalk.green('✓ ') + chalk.white(`复制完成,共复制了 ${filesCopied} 个文件`));
return filesCopied > 0;
}
catch (error) {
console.error(chalk.red('✗ ') + chalk.white('读取目录失败: ') + chalk.underline.cyan(sourceDirPath));
console.error(' ' + error.message);
return false;
}
}
//# sourceMappingURL=file_utils.js.map