long-git-cli
Version:
A CLI tool for Git tag management.
132 lines • 4.01 kB
JavaScript
;
/**
* 本地存储模块
* 处理本地 JSON 文件的读写操作
*/
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.Storage = void 0;
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
const constants_1 = require("../constants");
/**
* Storage 类
* 负责本地文件的读写操作
*/
class Storage {
constructor(storagePath = constants_1.CONFIG_DIR) {
this.storagePath = storagePath;
}
/**
* 读取文件
*/
async read(filename) {
try {
const filePath = path.join(this.storagePath, filename);
const data = await fs.readFile(filePath, "utf-8");
return JSON.parse(data);
}
catch (error) {
if (error.code === "ENOENT") {
/** 文件不存在 */
return null;
}
throw new Error(`读取文件失败: ${error.message}`);
}
}
/**
* 写入文件
*/
async write(filename, data) {
try {
/** 确保目录存在 */
await this.ensureDir();
const filePath = path.join(this.storagePath, filename);
const jsonData = JSON.stringify(data, null, 2);
await fs.writeFile(filePath, jsonData, "utf-8");
/** 设置文件权限为仅当前用户可读写 (600) */
await fs.chmod(filePath, 0o600);
}
catch (error) {
throw new Error(`写入文件失败: ${error.message}`);
}
}
/**
* 检查文件是否存在
*/
async exists(filename) {
try {
const filePath = path.join(this.storagePath, filename);
await fs.access(filePath);
return true;
}
catch {
return false;
}
}
/**
* 删除文件
*/
async delete(filename) {
try {
const filePath = path.join(this.storagePath, filename);
await fs.unlink(filePath);
}
catch (error) {
if (error.code !== "ENOENT") {
throw new Error(`删除文件失败: ${error.message}`);
}
}
}
/**
* 获取存储路径
*/
getStoragePath() {
return this.storagePath;
}
/**
* 确保目录存在
*/
async ensureDir() {
try {
await fs.mkdir(this.storagePath, { recursive: true });
}
catch (error) {
throw new Error(`创建目录失败: ${error.message}`);
}
}
}
exports.Storage = Storage;
//# sourceMappingURL=storage.js.map