"feat: 新增 kevisual 同步配置,升级相关依赖"

This commit is contained in:
2025-05-27 01:39:15 +08:00
parent 1869164d3d
commit 552653c8f7
17 changed files with 1214 additions and 770 deletions

View File

@@ -1,4 +1,5 @@
import fs from 'node:fs';
import path from 'node:path';
/**
* 检查文件是否存在
@@ -18,6 +19,22 @@ export const checkFileExists = (filePath: string, checkIsFile = false) => {
}
};
/**
* 检查文件目录是否存在,如果不存在则创建
* @param filePath 文件路径
* @param create
* @returns
*/
export const checkFileDir = (filePath: string, create = true) => {
const dirPath = path.dirname(filePath);
const exist = checkFileExists(dirPath);
if (create && !exist) {
fs.mkdirSync(dirPath, { recursive: true });
}
return exist;
};
export const createDir = (dirPath: string) => {
if (!checkFileExists(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });

View File

@@ -0,0 +1,47 @@
import path from 'node:path';
import fs from 'node:fs';
import { checkFileDir, checkFileExists } from '../file/index.js';
type SimpleObject = {
[key: string]: any;
};
type StorageOptions = {
dir?: string;
};
export class StorageCore {
storagePath: string;
constructor(opts?: StorageOptions) {
this.storagePath = opts?.dir || process.cwd();
}
async getData<T = SimpleObject>(key: string = 'data.json', fileDirectory?: string): Promise<T | null> {
const storageFilePath = path.join(this.storagePath, fileDirectory, key);
if (!checkFileExists(storageFilePath)) {
return null;
}
try {
const data = fs.readFileSync(storageFilePath, 'utf-8');
return JSON.parse(data) as T;
} catch (error) {
console.error(`Error reading data from ${storageFilePath}:`, error);
return null;
}
}
async setData<T = SimpleObject>(data: T, key: string = 'data.json', fileDirectory?: string): Promise<void> {
const storageFilePath = path.join(this.storagePath, fileDirectory, key);
try {
checkFileDir(storageFilePath, true);
fs.writeFileSync(storageFilePath, JSON.stringify(data, null, 2), 'utf-8');
} catch (error) {
console.error(`Error writing data to ${storageFilePath}:`, error);
}
}
async deleteData(key: string = 'data.json', fileDirectory?: string): Promise<void> {
const storageFilePath = path.join(this.storagePath, fileDirectory, key);
try {
if (checkFileExists(storageFilePath)) {
fs.unlinkSync(storageFilePath);
}
} catch (error) {
console.error(`Error deleting data from ${storageFilePath}:`, error);
}
}
}