145 lines
4.7 KiB
JavaScript
145 lines
4.7 KiB
JavaScript
/**
|
||
* 文件操作工具类
|
||
* 提供文件读写、目录创建等常用操作
|
||
*/
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
|
||
class FileUtils {
|
||
/**
|
||
* 获取数据文件的完整路径
|
||
* @param {string} fileName 文件名
|
||
* @param {string} baseDir 基础目录 (默认为 'data')
|
||
* @param {string} subDir 子目录名
|
||
* @returns {string} 完整的文件路径
|
||
*/
|
||
static getDataFilePath(fileName, baseDir = 'data', subDir = '') {
|
||
const parts = [process.cwd(), baseDir];
|
||
if (subDir) {
|
||
parts.push(subDir);
|
||
}
|
||
parts.push(fileName);
|
||
return path.join(...parts);
|
||
}
|
||
|
||
/**
|
||
* 确保目录存在,如果不存在则创建
|
||
* @param {string} dirPath 目录路径
|
||
* @returns {boolean} 操作是否成功
|
||
*/
|
||
static ensureDirectoryExists(dirPath) {
|
||
try {
|
||
if (!fs.existsSync(dirPath)) {
|
||
fs.mkdirSync(dirPath, { recursive: true });
|
||
console.log(`目录已创建: ${dirPath}`);
|
||
}
|
||
return true;
|
||
} catch (error) {
|
||
console.error(`创建目录失败: ${dirPath}`, error);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 保存对象到JSON文件
|
||
* @param {Object} data 要保存的数据对象
|
||
* @param {string} filePath 文件路径
|
||
* @param {Object} options 选项
|
||
* @param {boolean} options.pretty 是否美化JSON (默认: true)
|
||
* @param {string} options.encoding 文件编码 (默认: 'utf8')
|
||
* @returns {boolean} 操作是否成功
|
||
*/
|
||
static saveToJsonFile(data, filePath, options = {}) {
|
||
try {
|
||
const { pretty = true, encoding = 'utf8' } = options;
|
||
const indent = pretty ? 2 : 0;
|
||
|
||
// 确保目录存在
|
||
const dirPath = path.dirname(filePath);
|
||
this.ensureDirectoryExists(dirPath);
|
||
|
||
// 将对象转换为JSON字符串并写入文件
|
||
fs.writeFileSync(filePath, JSON.stringify(data, null, indent), encoding);
|
||
console.log(`数据已保存到: ${filePath}`);
|
||
return true;
|
||
} catch (error) {
|
||
console.error(`保存JSON文件失败: ${filePath}`, error);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 从JSON文件加载对象
|
||
* @param {string} filePath 文件路径
|
||
* @param {Object} options 选项
|
||
* @param {string} options.encoding 文件编码 (默认: 'utf8')
|
||
* @returns {Object|null} 加载的对象,如果失败则返回null
|
||
*/
|
||
static loadFromJsonFile(filePath, options = {}) {
|
||
try {
|
||
const { encoding = 'utf8' } = options;
|
||
|
||
if (!fs.existsSync(filePath)) {
|
||
console.error(`文件不存在: ${filePath}`);
|
||
return null;
|
||
}
|
||
|
||
const data = fs.readFileSync(filePath, encoding);
|
||
return JSON.parse(data);
|
||
} catch (error) {
|
||
console.error(`加载JSON文件失败: ${filePath}`, error);
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 保存文本到文件
|
||
* @param {string} text 要保存的文本
|
||
* @param {string} filePath 文件路径
|
||
* @param {Object} options 选项
|
||
* @param {string} options.encoding 文件编码 (默认: 'utf8')
|
||
* @returns {boolean} 操作是否成功
|
||
*/
|
||
static saveTextToFile(text, filePath, options = {}) {
|
||
try {
|
||
const { encoding = 'utf8' } = options;
|
||
|
||
// 确保目录存在
|
||
const dirPath = path.dirname(filePath);
|
||
this.ensureDirectoryExists(dirPath);
|
||
|
||
// 写入文件
|
||
fs.writeFileSync(filePath, text, encoding);
|
||
console.log(`文本已保存到: ${filePath}`);
|
||
return true;
|
||
} catch (error) {
|
||
console.error(`保存文本文件失败: ${filePath}`, error);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 从文件加载文本
|
||
* @param {string} filePath 文件路径
|
||
* @param {Object} options 选项
|
||
* @param {string} options.encoding 文件编码 (默认: 'utf8')
|
||
* @returns {string|null} 加载的文本,如果失败则返回null
|
||
*/
|
||
static loadTextFromFile(filePath, options = {}) {
|
||
try {
|
||
const { encoding = 'utf8' } = options;
|
||
|
||
if (!fs.existsSync(filePath)) {
|
||
console.error(`文件不存在: ${filePath}`);
|
||
return null;
|
||
}
|
||
|
||
return fs.readFileSync(filePath, encoding);
|
||
} catch (error) {
|
||
console.error(`加载文本文件失败: ${filePath}`, error);
|
||
return null;
|
||
}
|
||
}
|
||
}
|
||
|
||
module.exports = FileUtils;
|