import path from 'node:path'; import fs from 'node:fs'; import { Config, SyncList } from './type.ts'; import { fileIsExist } from '@/uitls/file.ts'; export type SyncOptions = { dir?: string; configFilename?: string; baseURL?: string; }; export class SyncBase { config: Config; #filename: string; #dir: string; baseURL: string; constructor(opts?: SyncOptions) { const filename = opts?.configFilename || 'kevisual.json'; const dir = opts?.dir || process.cwd(); this.#filename = filename; this.#dir = path.resolve(dir); this.baseURL = opts?.baseURL ?? ''; this.init(); } async init() { try { const dir = this.#dir; const filename = this.#filename; const filepath = path.join(dir, filename); if (!fileIsExist(filepath)) throw new Error('config file not found'); const config = JSON.parse(fs.readFileSync(filepath, 'utf-8')); this.config = config; return config; } catch (err) { this.config = {} as Config; return {} as Config; } } async getSyncList(): Promise { const config = this.config!; const sync = config?.sync || {}; const syncKeys = Object.keys(sync); const baseURL = this.baseURL; const syncList = syncKeys.map((key) => { const value = sync[key]; const filepath = path.join(this.#dir, key); // 文件的路径 const checkAuth = (value: string = '', baseURL: string = '') => { if (value.startsWith(baseURL)) { return true; } return false; }; if (typeof value === 'string') { return { filepath, url: value, auth: checkAuth(value, baseURL), }; } return { filepath, ...value, auth: checkAuth(value.url, baseURL), }; }); return syncList; } async getDir(filepath: string, check = false) { const dir = path.dirname(filepath); if (check) { if (!fileIsExist(dir)) { fs.mkdirSync(dir, { recursive: true }); } } return dir; } async download() { // const syncList = await this.getSyncList(); // for (const item of syncList) { // } } async upload() { // need check permission } }