mirror of
https://github.com/labring/FastGPT.git
synced 2026-05-08 01:08:43 +08:00
189 lines
5.3 KiB
JavaScript
189 lines
5.3 KiB
JavaScript
const { execSync } = require('child_process');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// 始终基于脚本位置定位 document 根目录,避免对 CWD 的依赖。
|
|
const DOCUMENT_ROOT = path.resolve(__dirname, '..');
|
|
const DEFAULT_CONTENT_DIR = path.join(DOCUMENT_ROOT, 'content');
|
|
const DEFAULT_OUTPUT_PATH = path.join(DOCUMENT_ROOT, 'data', 'doc-last-modified.json');
|
|
|
|
/**
|
|
* 获取文件的 git 最后修改时间
|
|
* @param {string} filePath - 文件路径
|
|
* @returns {string|null} - 最后修改时间(ISO 格式)或 null
|
|
*/
|
|
|
|
function getFileLastModifiedTime(filePath) {
|
|
try {
|
|
// 使用 git log 获取文件的最后修改时间
|
|
const command = `git log -1 --format="%aI" -- "${filePath}"`;
|
|
const result = execSync(command, {
|
|
encoding: 'utf8',
|
|
cwd: DOCUMENT_ROOT,
|
|
stdio: ['pipe', 'pipe', 'pipe']
|
|
});
|
|
|
|
return result.trim() || null;
|
|
} catch (error) {
|
|
console.warn(`无法获取文件 ${filePath} 的 git 信息:`, error.message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 递归获取目录下所有文档文件
|
|
* @param {string} dirPath - 目录路径
|
|
* @param {string[]} extensions - 文件扩展名数组
|
|
* @returns {string[]} - 文件路径数组
|
|
*/
|
|
function getAllDocFiles(dirPath, extensions = ['.md', '.mdx']) {
|
|
const files = [];
|
|
|
|
function traverse(currentPath) {
|
|
const items = fs.readdirSync(currentPath);
|
|
|
|
for (const item of items) {
|
|
const fullPath = path.join(currentPath, item);
|
|
const stat = fs.statSync(fullPath);
|
|
|
|
if (stat.isDirectory()) {
|
|
// 跳过 node_modules 和 .git 目录
|
|
if (item !== 'node_modules' && item !== '.git' && !item.startsWith('.')) {
|
|
traverse(fullPath);
|
|
}
|
|
} else if (stat.isFile()) {
|
|
const ext = path.extname(item);
|
|
if (extensions.includes(ext)) {
|
|
files.push(fullPath);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
traverse(dirPath);
|
|
return files;
|
|
}
|
|
|
|
/**
|
|
* 获取所有文档的最新修改时间
|
|
* @param {string} contentDir - 文档内容目录
|
|
* @returns {Object} - 文件路径到修改时间的映射
|
|
*/
|
|
function getAllDocLastModifiedTimes(contentDir = DEFAULT_CONTENT_DIR) {
|
|
const docFiles = getAllDocFiles(contentDir);
|
|
const result = {};
|
|
|
|
console.log(`正在处理 ${docFiles.length} 个文档文件...`);
|
|
|
|
for (const filePath of docFiles) {
|
|
const relativePath = path.relative(DOCUMENT_ROOT, filePath);
|
|
const lastModified = getFileLastModifiedTime(relativePath);
|
|
|
|
if (lastModified) {
|
|
result[relativePath] = lastModified;
|
|
}
|
|
|
|
// 显示进度
|
|
process.stdout.write('.');
|
|
}
|
|
|
|
console.log('\n完成!');
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 将结果保存到 JSON 文件
|
|
* @param {Object} data - 要保存的数据
|
|
* @param {string} outputPath - 输出文件路径
|
|
*/
|
|
function saveToJsonFile(data, outputPath = DEFAULT_OUTPUT_PATH) {
|
|
try {
|
|
// 确保目录存在
|
|
const dir = path.dirname(outputPath);
|
|
if (!fs.existsSync(dir)) {
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
}
|
|
|
|
fs.writeFileSync(outputPath, JSON.stringify(data, null, 2), 'utf8');
|
|
console.log(`结果已保存到: ${outputPath}`);
|
|
} catch (error) {
|
|
console.error('保存文件失败:', error.message);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取单个文件的详细信息
|
|
* @param {string} filePath - 文件路径
|
|
* @returns {Object} - 文件信息对象
|
|
*/
|
|
function getFileInfo(filePath) {
|
|
const relativePath = path.relative(DOCUMENT_ROOT, filePath);
|
|
const lastModified = getFileLastModifiedTime(relativePath);
|
|
const stat = fs.statSync(filePath);
|
|
|
|
return {
|
|
path: relativePath,
|
|
lastModified,
|
|
size: stat.size,
|
|
created: stat.birthtime.toISOString(),
|
|
modified: stat.mtime.toISOString()
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 主函数 - 获取所有文档的最新修改时间并保存
|
|
*/
|
|
function main() {
|
|
console.log('开始获取文档的最新修改时间...');
|
|
|
|
// lint-staged 会传入暂存的 mdx 文件列表(不是目录),这里统一忽略,
|
|
// 始终对 content/ 全量扫描以保证 doc-last-modified.json 的完整性。
|
|
const argDir = process.argv[2];
|
|
let contentDir = DEFAULT_CONTENT_DIR;
|
|
if (argDir && fs.existsSync(argDir) && fs.statSync(argDir).isDirectory()) {
|
|
contentDir = argDir;
|
|
}
|
|
|
|
if (!fs.existsSync(contentDir)) {
|
|
console.error(`目录不存在: ${contentDir}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const result = getAllDocLastModifiedTimes(contentDir);
|
|
|
|
// 保存简单的文件路径到修改时间的映射
|
|
saveToJsonFile(result, DEFAULT_OUTPUT_PATH);
|
|
|
|
// 显示统计信息
|
|
console.log('\n统计信息:');
|
|
console.log(`- 总文件数: ${Object.keys(result).length}`);
|
|
console.log(`- 成功获取时间: ${Object.values(result).filter(Boolean).length}`);
|
|
console.log(`- 失败文件数: ${Object.values(result).filter((v) => !v).length}`);
|
|
|
|
// 显示最近修改的文件
|
|
const recentFiles = Object.entries(result)
|
|
.filter(([, time]) => time)
|
|
.sort(([, a], [, b]) => new Date(b) - new Date(a))
|
|
.slice(0, 5);
|
|
|
|
if (recentFiles.length > 0) {
|
|
console.log('\n最近修改的文件:');
|
|
recentFiles.forEach(([file, time]) => {
|
|
console.log(`- ${file}: ${new Date(time).toLocaleString()}`);
|
|
});
|
|
}
|
|
}
|
|
|
|
// 如果直接运行此文件
|
|
if (require.main === module) {
|
|
main();
|
|
}
|
|
|
|
module.exports = {
|
|
getFileLastModifiedTime,
|
|
getAllDocFiles,
|
|
getAllDocLastModifiedTimes,
|
|
getFileInfo,
|
|
saveToJsonFile
|
|
};
|