yonbip-code-gen-mcp
Version:
YonBIP高级版代码生成MCP
73 lines (72 loc) • 2.27 kB
JavaScript
import * as fs from 'fs';
import * as path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const LOCAL_PRE_PATH = path.resolve(__dirname, 'doc/template/');
/**
* 读取指定路径的文件内容
* @param filePath 文件路径
* @returns 文件内容字符串
*/
export function readFileContent(filePath) {
try {
const fullPath = path.resolve(filePath);
if (!fs.existsSync(fullPath)) {
throw new Error(`File not found: ${fullPath}`);
}
return fs.readFileSync(fullPath, 'utf-8');
}
catch (error) {
throw new Error(`Error reading file ${filePath}: ${error.message}`);
}
}
/**
* 读取模板目录下的MD文件内容
* @param fileName 文件名
* @param templateDir 模板目录路径,默认为 './doc/template'
* @returns 文件内容字符串
*/
export function readTemplateMDFile(fileName, templateDir = LOCAL_PRE_PATH) {
try {
const filePath = path.join(templateDir, fileName);
return readFileContent(filePath);
}
catch (error) {
throw new Error(`Error reading template file ${fileName}: ${error.message}`);
}
}
/**
* 获取模板目录下所有MD文件列表
* @param templateDir 模板目录路径,默认为 './doc/template'
* @returns 文件名数组
*/
export function listTemplateMDFiles(templateDir = LOCAL_PRE_PATH) {
try {
const fullPath = path.resolve(templateDir);
if (!fs.existsSync(fullPath)) {
throw new Error(`Template directory not found: ${fullPath}`);
}
const files = fs.readdirSync(fullPath);
return files.filter(file => path.extname(file).toLowerCase() === '.md');
}
catch (error) {
throw new Error(`Error listing template files in ${templateDir}: ${error.message}`);
}
}
/**
* 根据文件名读取模板内容
* @param fileName 文件名
* @returns 文件内容
*/
export function getTemplateContent(fileName) {
const templateDir = LOCAL_PRE_PATH;
return readTemplateMDFile(fileName, templateDir);
}
export default {
readFileContent,
readTemplateMDFile,
listTemplateMDFiles,
getTemplateContent
};