@zzclub/z-cli
Version:
all-in-one 工具箱,专为提升日常及工作效率而生
126 lines • 3.77 kB
JavaScript
import { homedir, platform } from 'node:os';
import { basename, join } from 'node:path';
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
import { DEFAULT_CONFIG, CONFIG_DIR, CONFIG_FILE } from '../types/config.js';
/**
* 配置管理器
*/
export class ConfigManager {
configPath;
config;
constructor() {
const configDir = this.getConfigDir();
this.configPath = join(configDir, CONFIG_FILE);
this.migrateLegacyConfig(configDir, this.configPath);
// 确保配置目录存在
if (!existsSync(configDir)) {
mkdirSync(configDir, { recursive: true });
}
// 加载配置
this.config = this.load();
}
getConfigDir() {
const home = homedir();
const currentPlatform = platform();
if (currentPlatform === 'win32') {
const appData = process.env.APPDATA;
if (appData) {
return join(appData, CONFIG_DIR);
}
return join(home, 'AppData', 'Roaming', CONFIG_DIR);
}
if (currentPlatform === 'darwin') {
return join(home, 'Library', 'Application Support', CONFIG_DIR);
}
const xdgConfigHome = process.env.XDG_CONFIG_HOME;
if (xdgConfigHome && xdgConfigHome.trim().length > 0) {
return join(xdgConfigHome, CONFIG_DIR);
}
return join(home, '.config', CONFIG_DIR);
}
migrateLegacyConfig(configDir, nextConfigPath) {
const legacyDir = join(homedir(), `.${CONFIG_DIR}`);
const legacyPath = join(legacyDir, CONFIG_FILE);
if (!existsSync(legacyPath) || existsSync(nextConfigPath)) {
return;
}
if (!existsSync(configDir)) {
mkdirSync(configDir, { recursive: true });
}
renameSync(legacyPath, nextConfigPath);
if (existsSync(legacyDir) && basename(legacyDir) === `.${CONFIG_DIR}`) {
try {
rmSync(legacyDir, { recursive: true, force: true });
}
catch {
return;
}
}
}
/**
* 加载配置
*/
load() {
if (!existsSync(this.configPath)) {
// 首次运行,创建默认配置
this.save(DEFAULT_CONFIG);
return DEFAULT_CONFIG;
}
try {
const content = readFileSync(this.configPath, 'utf-8');
const loadedConfig = JSON.parse(content);
// 合并默认配置(处理新增字段)
return {
tiny: { ...DEFAULT_CONFIG.tiny, ...loadedConfig.tiny },
};
}
catch (error) {
console.error(`配置文件解析失败,使用默认配置: ${error}`);
return DEFAULT_CONFIG;
}
}
/**
* 保存配置
*/
save(config) {
try {
writeFileSync(this.configPath, JSON.stringify(config, null, 2), 'utf-8');
}
catch (error) {
throw new Error(`配置保存失败: ${error}`);
}
}
/**
* 获取完整配置
*/
getConfig() {
return this.config;
}
/**
* 获取 Tiny 配置
*/
getTinyConfig() {
return this.config.tiny;
}
/**
* 更新 Tiny 配置
*/
updateTinyConfig(updates) {
this.config.tiny = { ...this.config.tiny, ...updates };
this.save(this.config);
}
/**
* 重置为默认配置
*/
reset() {
this.config = DEFAULT_CONFIG;
this.save(this.config);
}
/**
* 获取配置文件路径
*/
getConfigPath() {
return this.configPath;
}
}
//# sourceMappingURL=config-manager.js.map