feat: 更新版本至 0.0.53,升级依赖,添加 query-login-node 构建配置,优化文件操作同步化
This commit is contained in:
@@ -2,9 +2,8 @@ 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 (
|
||||
import { readFileSync, writeFileSync, accessSync, unlinkSync, mkdirSync } from 'node:fs';
|
||||
export const fileExists = (
|
||||
filePath: string,
|
||||
{ createIfNotExists = true, isFile = true, isDir = false }: { createIfNotExists?: boolean; isFile?: boolean; isDir?: boolean } = {},
|
||||
) => {
|
||||
@@ -13,10 +12,10 @@ export const fileExists = async (
|
||||
return true;
|
||||
} catch (error) {
|
||||
if (createIfNotExists && isDir) {
|
||||
await mkdir(filePath, { recursive: true });
|
||||
mkdirSync(filePath, { recursive: true });
|
||||
return true;
|
||||
} else if (createIfNotExists && isFile) {
|
||||
await mkdir(dirname(filePath), { recursive: true });
|
||||
mkdirSync(dirname(filePath), { recursive: true });
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
@@ -34,32 +33,53 @@ export const readConfigFile = (filePath: string) => {
|
||||
export const writeConfigFile = (filePath: string, data: any) => {
|
||||
writeFileSync(filePath, JSON.stringify(data, null, 2));
|
||||
};
|
||||
export const getHostName = () => {
|
||||
/**
|
||||
* 获取主机名,
|
||||
* 例如:https://kevisual.cn 返回 kevisual.cn
|
||||
* 这里是读取默认的配置文件 ~/.config/envision/config.json 中的 baseURL 字段来获取主机名的,如果没有配置 baseURL 则默认使用 https://kevisual.cn
|
||||
* @returns 主机名
|
||||
*/
|
||||
export const getHostName = (baseURL?: string) => {
|
||||
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;
|
||||
const _baseURL = baseURL || config.baseURL || 'https://kevisual.cn';
|
||||
const hostname = new URL(_baseURL).hostname;
|
||||
return hostname;
|
||||
};
|
||||
export class StorageNode implements Storage {
|
||||
cacheData: any;
|
||||
filePath: string;
|
||||
constructor() {
|
||||
filePath: string = '';
|
||||
hostname: string = '';
|
||||
isLoaded: boolean = false;
|
||||
constructor(opts?: { baseURL?: string, load?: boolean }) {
|
||||
this.cacheData = {};
|
||||
const configDir = join(homedir(), '.config', 'envision');
|
||||
const hostname = getHostName();
|
||||
this.filePath = join(configDir, 'config', `${hostname}-storage.json`);
|
||||
fileExists(this.filePath, { isFile: true });
|
||||
const hostname = getHostName(opts?.baseURL);
|
||||
this.setHostName(hostname, { load: opts?.load });
|
||||
}
|
||||
async loadCache() {
|
||||
setHostName(hostname: string, opts?: { load?: boolean }) {
|
||||
const load = opts?.load ?? false;
|
||||
if (hostname.startsWith('http')) {
|
||||
hostname = new URL(hostname).hostname;
|
||||
}
|
||||
const configDir = join(homedir(), '.config', 'envision');
|
||||
this.filePath = join(configDir, 'config', `${hostname}-storage.json`);
|
||||
this.hostname = hostname;
|
||||
fileExists(this.filePath, { isFile: true });
|
||||
if (load) {
|
||||
this.loadCache();
|
||||
}
|
||||
}
|
||||
loadCache(force?: boolean) {
|
||||
if (this.isLoaded && !force) return;
|
||||
const filePath = this.filePath;
|
||||
try {
|
||||
const data = await readConfigFile(filePath);
|
||||
const data = readConfigFile(filePath);
|
||||
this.cacheData = data;
|
||||
this.isLoaded = true;
|
||||
} catch (error) {
|
||||
this.cacheData = {};
|
||||
await writeFile(filePath, JSON.stringify(this.cacheData, null, 2));
|
||||
writeFileSync(filePath, JSON.stringify(this.cacheData, null, 2));
|
||||
}
|
||||
}
|
||||
get length() {
|
||||
@@ -70,15 +90,15 @@ export class StorageNode implements Storage {
|
||||
}
|
||||
setItem(key: string, value: any) {
|
||||
this.cacheData[key] = value;
|
||||
writeFile(this.filePath, JSON.stringify(this.cacheData, null, 2));
|
||||
writeFileSync(this.filePath, JSON.stringify(this.cacheData, null, 2));
|
||||
}
|
||||
removeItem(key: string) {
|
||||
delete this.cacheData[key];
|
||||
writeFile(this.filePath, JSON.stringify(this.cacheData, null, 2));
|
||||
writeFileSync(this.filePath, JSON.stringify(this.cacheData, null, 2));
|
||||
}
|
||||
clear() {
|
||||
this.cacheData = {};
|
||||
writeFile(this.filePath, JSON.stringify(this.cacheData, null, 2));
|
||||
writeFileSync(this.filePath, JSON.stringify(this.cacheData, null, 2));
|
||||
}
|
||||
key(index: number) {
|
||||
return Object.keys(this.cacheData)[index];
|
||||
@@ -86,10 +106,13 @@ export class StorageNode implements Storage {
|
||||
}
|
||||
export class LoginNodeCache implements Cache {
|
||||
filepath: string;
|
||||
|
||||
constructor(filepath?: string) {
|
||||
this.filepath = filepath || join(homedir(), '.config', 'envision', 'config', `${getHostName()}-login.json`);
|
||||
isLoaded: boolean = false;
|
||||
constructor(opts?: { baseURL?: string, load?: boolean }) {
|
||||
this.filepath = join(homedir(), '.config', 'envision', 'config', `${getHostName(opts?.baseURL)}-login.json`);
|
||||
fileExists(this.filepath, { isFile: true });
|
||||
if (opts?.load) {
|
||||
this.loadCache(this.filepath);
|
||||
}
|
||||
}
|
||||
async get(_key: string) {
|
||||
try {
|
||||
@@ -111,12 +134,14 @@ export class LoginNodeCache implements Cache {
|
||||
}
|
||||
}
|
||||
async del() {
|
||||
await unlink(this.filepath);
|
||||
unlinkSync(this.filepath);
|
||||
}
|
||||
async loadCache(filePath: string) {
|
||||
loadCache(filePath: string, force?: boolean) {
|
||||
if (this.isLoaded && !force) return;
|
||||
try {
|
||||
const data = await readFile(filePath, 'utf-8');
|
||||
const data = readFileSync(filePath, 'utf-8');
|
||||
const jsonData = JSON.parse(data);
|
||||
this.isLoaded = true;
|
||||
return jsonData;
|
||||
} catch (error) {
|
||||
// console.log('loadCache error', error);
|
||||
@@ -126,7 +151,7 @@ export class LoginNodeCache implements Cache {
|
||||
return defaultData;
|
||||
}
|
||||
}
|
||||
async init() {
|
||||
return await this.loadCache(this.filepath);
|
||||
init() {
|
||||
return this.loadCache(this.filepath);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user