132 lines
3.9 KiB
TypeScript
132 lines
3.9 KiB
TypeScript
import { Cache } from './login-cache.ts';
|
|
import { homedir } from 'node:os';
|
|
import { join, dirname } from 'node:path';
|
|
import fs from 'node:fs';
|
|
import { readFileSync, writeFileSync, accessSync } from 'node:fs';
|
|
import { readFile, writeFile, unlink, mkdir } from 'node:fs/promises';
|
|
export const fileExists = async (
|
|
filePath: string,
|
|
{ createIfNotExists = true, isFile = true, isDir = false }: { createIfNotExists?: boolean; isFile?: boolean; isDir?: boolean } = {},
|
|
) => {
|
|
try {
|
|
accessSync(filePath, fs.constants.F_OK);
|
|
return true;
|
|
} catch (error) {
|
|
if (createIfNotExists && isDir) {
|
|
await mkdir(filePath, { recursive: true });
|
|
return true;
|
|
} else if (createIfNotExists && isFile) {
|
|
await mkdir(dirname(filePath), { recursive: true });
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
};
|
|
export const readConfigFile = (filePath: string) => {
|
|
try {
|
|
const data = readFileSync(filePath, 'utf-8');
|
|
const jsonData = JSON.parse(data);
|
|
return jsonData;
|
|
} catch (error) {
|
|
return {};
|
|
}
|
|
};
|
|
export const writeConfigFile = (filePath: string, data: any) => {
|
|
writeFileSync(filePath, JSON.stringify(data, null, 2));
|
|
};
|
|
export const getHostName = () => {
|
|
const configDir = join(homedir(), '.config', 'envision');
|
|
const configFile = join(configDir, 'config.json');
|
|
const config = readConfigFile(configFile);
|
|
const baseURL = config.baseURL || 'https://kevisual.cn';
|
|
const hostname = new URL(baseURL).hostname;
|
|
return hostname;
|
|
};
|
|
export class StorageNode implements Storage {
|
|
cacheData: any;
|
|
filePath: string;
|
|
constructor() {
|
|
this.cacheData = {};
|
|
const configDir = join(homedir(), '.config', 'envision');
|
|
const hostname = getHostName();
|
|
this.filePath = join(configDir, 'config', `${hostname}-storage.json`);
|
|
fileExists(this.filePath, { isFile: true });
|
|
}
|
|
async loadCache() {
|
|
const filePath = this.filePath;
|
|
try {
|
|
const data = await readConfigFile(filePath);
|
|
this.cacheData = data;
|
|
} catch (error) {
|
|
this.cacheData = {};
|
|
await writeFile(filePath, JSON.stringify(this.cacheData, null, 2));
|
|
}
|
|
}
|
|
get length() {
|
|
return Object.keys(this.cacheData).length;
|
|
}
|
|
getItem(key: string) {
|
|
return this.cacheData[key];
|
|
}
|
|
setItem(key: string, value: any) {
|
|
this.cacheData[key] = value;
|
|
writeFile(this.filePath, JSON.stringify(this.cacheData, null, 2));
|
|
}
|
|
removeItem(key: string) {
|
|
delete this.cacheData[key];
|
|
writeFile(this.filePath, JSON.stringify(this.cacheData, null, 2));
|
|
}
|
|
clear() {
|
|
this.cacheData = {};
|
|
writeFile(this.filePath, JSON.stringify(this.cacheData, null, 2));
|
|
}
|
|
key(index: number) {
|
|
return Object.keys(this.cacheData)[index];
|
|
}
|
|
}
|
|
export class LoginNodeCache implements Cache {
|
|
filepath: string;
|
|
|
|
constructor(filepath?: string) {
|
|
this.filepath = filepath || join(homedir(), '.config', 'envision', 'config', `${getHostName()}-login.json`);
|
|
fileExists(this.filepath, { isFile: true });
|
|
}
|
|
async get(_key: string) {
|
|
try {
|
|
const filePath = this.filepath;
|
|
const data = readConfigFile(filePath);
|
|
return data;
|
|
} catch (error) {
|
|
console.log('get error', error);
|
|
return {};
|
|
}
|
|
}
|
|
async set(_key: string, value: any) {
|
|
try {
|
|
const data = readConfigFile(this.filepath);
|
|
const newData = { ...data, ...value };
|
|
writeConfigFile(this.filepath, newData);
|
|
} catch (error) {
|
|
console.log('set error', error);
|
|
}
|
|
}
|
|
async del() {
|
|
await unlink(this.filepath);
|
|
}
|
|
async loadCache(filePath: string) {
|
|
try {
|
|
const data = await readFile(filePath, 'utf-8');
|
|
const jsonData = JSON.parse(data);
|
|
return jsonData;
|
|
} catch (error) {
|
|
console.log('loadCache error', error);
|
|
const defaultData = { loginUsers: [] };
|
|
writeConfigFile(filePath, defaultData);
|
|
return defaultData;
|
|
}
|
|
}
|
|
async init() {
|
|
return await this.loadCache(this.filepath);
|
|
}
|
|
}
|