codn_ts
Version:
智能代码分析工具 - 支持语义搜索、调用链分析和代码结构可视化,对大模型/AI agent 友好
182 lines • 6.13 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.catFile = catFile;
exports.getFileInfo = getFileInfo;
exports.detectFileType = detectFileType;
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
async function catFile(filePath, options = {}) {
const { showLineNumbers = false, startLine = 1, endLine = -1, highlight = false, encoding = "utf-8", } = options;
const resolvedPath = path.resolve(filePath);
try {
// 检查文件是否存在
if (!fs.existsSync(resolvedPath)) {
throw new Error(`文件不存在: ${filePath}`);
}
const stats = fs.statSync(resolvedPath);
if (!stats.isFile()) {
throw new Error(`路径不是文件: ${filePath}`);
}
// 读取文件内容
const content = fs.readFileSync(resolvedPath, {
encoding: encoding,
});
const lines = content.split("\n");
const totalLines = lines.length;
// 处理行范围
const actualStartLine = Math.max(1, startLine);
const actualEndLine = endLine === -1 ? totalLines : Math.min(totalLines, endLine);
if (actualStartLine > actualEndLine) {
throw new Error(`起始行 (${actualStartLine}) 不能大于结束行 (${actualEndLine})`);
}
// 提取指定范围的内容
const selectedLines = lines.slice(actualStartLine - 1, actualEndLine);
let resultContent = selectedLines.join("\n");
// 添加行号
if (showLineNumbers) {
resultContent = selectedLines
.map((line, index) => {
const lineNumber = actualStartLine + index;
return `${lineNumber.toString().padStart(4)}: ${line}`;
})
.join("\n");
}
return {
file: path.relative(process.cwd(), resolvedPath),
content: resultContent,
totalLines,
startLine: actualStartLine,
endLine: actualEndLine,
encoding,
size: stats.size,
modified: stats.mtime.toISOString().split("T")[0],
};
}
catch (error) {
throw new Error(`读取文件失败: ${error}`);
}
}
// 获取文件信息(不读取内容)
function getFileInfo(filePath) {
const resolvedPath = path.resolve(filePath);
try {
if (!fs.existsSync(resolvedPath)) {
return {
file: path.relative(process.cwd(), resolvedPath),
exists: false,
isFile: false,
};
}
const stats = fs.statSync(resolvedPath);
return {
file: path.relative(process.cwd(), resolvedPath),
exists: true,
isFile: stats.isFile(),
size: stats.size,
modified: stats.mtime.toISOString().split("T")[0],
encoding: "utf-8",
};
}
catch (error) {
return {
file: path.relative(process.cwd(), resolvedPath),
exists: false,
isFile: false,
};
}
}
// 检测文件类型
function detectFileType(filePath) {
const ext = path.extname(filePath).toLowerCase();
const fileName = path.basename(filePath).toLowerCase();
// 常见文件类型映射
const typeMap = {
// 代码文件
".ts": "typescript",
".js": "javascript",
".tsx": "typescript-react",
".jsx": "javascript-react",
".py": "python",
".java": "java",
".cpp": "cpp",
".c": "c",
".h": "c-header",
".hpp": "cpp-header",
".go": "go",
".rs": "rust",
".php": "php",
".rb": "ruby",
".swift": "swift",
".kt": "kotlin",
".scala": "scala",
// 配置文件
".json": "json",
".yaml": "yaml",
".yml": "yaml",
".toml": "toml",
".ini": "ini",
".conf": "config",
".config": "config",
// 标记语言
".md": "markdown",
".html": "html",
".xml": "xml",
".svg": "svg",
// 样式文件
".css": "css",
".scss": "scss",
".less": "less",
".sass": "sass",
// 脚本文件
".sh": "shell",
".bash": "bash",
".zsh": "zsh",
".fish": "fish",
".ps1": "powershell",
".bat": "batch",
// 其他
".txt": "text",
".log": "log",
".sql": "sql",
".dockerfile": "dockerfile",
};
// 特殊文件名
if (fileName === "dockerfile" || fileName === "makefile") {
return fileName;
}
return typeMap[ext] || "unknown";
}
//# sourceMappingURL=cat.js.map