UNPKG

@xuanqikai/one-click-upload

Version:

A CLI tool for one-click file upload to cloud storage services (OSS, TOS)

270 lines 9.43 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigManager = void 0; const fs = __importStar(require("fs-extra")); const os = __importStar(require("os")); const path = __importStar(require("path")); const types_1 = require("../types"); class ConfigManager { constructor() { this.configLoaded = false; this.configPath = path.join(os.homedir(), '.one-upload-config.json'); this.config = { services: [] }; } /** * 确保配置已加载 */ async ensureConfigLoaded() { if (!this.configLoaded) { await this.loadConfig(); this.configLoaded = true; } } /** * 加载配置文件 */ async loadConfig() { try { if (await fs.pathExists(this.configPath)) { const configData = await fs.readJson(this.configPath); this.config = { ...this.config, ...configData }; } } catch (error) { console.warn('Failed to load config file, using default config'); this.config = { services: [] }; } } /** * 同步加载配置文件 */ loadConfigSync() { try { if (fs.pathExistsSync(this.configPath)) { const configData = fs.readJsonSync(this.configPath); this.config = { ...this.config, ...configData }; this.configLoaded = true; } } catch (error) { console.warn('Failed to load config file, using default config'); this.config = { services: [] }; } } /** * 保存配置文件 */ async saveConfig() { try { await fs.ensureFile(this.configPath); await fs.writeJson(this.configPath, this.config, { spaces: 2 }); } catch (error) { throw new Error(`Failed to save config: ${error.message || 'Unknown error'}`); } } /** * 添加服务配置 */ async addService(serviceConfig) { await this.ensureConfigLoaded(); // 检查服务名是否已存在 if (this.config.services.find(s => s.name === serviceConfig.name)) { throw new Error(`Service '${serviceConfig.name}' already exists`); } // 验证配置 this.validateServiceConfig(serviceConfig); // 如果是第一个服务或者设置为默认,则设为默认服务 if (this.config.services.length === 0 || serviceConfig.isDefault) { this.config.services.forEach(s => s.isDefault = false); serviceConfig.isDefault = true; this.config.defaultService = serviceConfig.name; } this.config.services.push(serviceConfig); await this.saveConfig(); } /** * 更新服务配置 */ async updateService(serviceName, updates) { await this.ensureConfigLoaded(); const serviceIndex = this.config.services.findIndex(s => s.name === serviceName); if (serviceIndex === -1) { throw new Error(`Service '${serviceName}' not found`); } const updatedService = { ...this.config.services[serviceIndex], ...updates }; // 如果更新了名称,检查新名称是否冲突 if (updates.name && updates.name !== serviceName) { if (this.config.services.find(s => s.name === updates.name)) { throw new Error(`Service '${updates.name}' already exists`); } } this.validateServiceConfig(updatedService); // 如果设置为默认服务 if (updates.isDefault) { this.config.services.forEach(s => s.isDefault = false); updatedService.isDefault = true; this.config.defaultService = updatedService.name; } this.config.services[serviceIndex] = updatedService; await this.saveConfig(); } /** * 删除服务配置 */ async removeService(serviceName) { await this.ensureConfigLoaded(); const serviceIndex = this.config.services.findIndex(s => s.name === serviceName); if (serviceIndex === -1) { throw new Error(`Service '${serviceName}' not found`); } const removedService = this.config.services[serviceIndex]; this.config.services.splice(serviceIndex, 1); // 如果删除的是默认服务,设置新的默认服务 if (removedService.isDefault && this.config.services.length > 0) { this.config.services[0].isDefault = true; this.config.defaultService = this.config.services[0].name; } else if (this.config.services.length === 0) { this.config.defaultService = undefined; } await this.saveConfig(); } /** * 获取服务配置 */ getService(serviceName) { // 同步方法,需要确保配置已加载 if (!this.configLoaded) { // 如果配置未加载,尝试同步加载 this.loadConfigSync(); } return this.config.services.find(s => s.name === serviceName); } /** * 获取所有服务配置 */ getAllServices() { // 同步方法,需要确保配置已加载 if (!this.configLoaded) { // 如果配置未加载,尝试同步加载 this.loadConfigSync(); } return [...this.config.services]; } /** * 获取默认服务配置 */ getDefaultService() { // 同步方法,需要确保配置已加载 if (!this.configLoaded) { // 如果配置未加载,尝试同步加载 this.loadConfigSync(); } return this.config.services.find(s => s.isDefault); } /** * 设置默认服务 */ async setDefaultService(serviceName) { await this.ensureConfigLoaded(); const service = this.getService(serviceName); if (!service) { throw new Error(`Service '${serviceName}' not found`); } this.config.services.forEach(s => s.isDefault = false); service.isDefault = true; this.config.defaultService = serviceName; await this.saveConfig(); } /** * 验证服务配置 */ validateServiceConfig(serviceConfig) { if (!serviceConfig.name || !serviceConfig.name.trim()) { throw new Error('Service name is required'); } if (!Object.values(types_1.ServiceType).includes(serviceConfig.type)) { throw new Error(`Invalid service type: ${serviceConfig.type}`); } switch (serviceConfig.type) { case types_1.ServiceType.OSS: this.validateOSSConfig(serviceConfig.config); break; case types_1.ServiceType.TOS: this.validateTOSConfig(serviceConfig.config); break; default: throw new Error(`Unsupported service type: ${serviceConfig.type}`); } } /** * 验证 OSS 配置 */ validateOSSConfig(config) { const required = ['region', 'accessKeyId', 'accessKeySecret', 'bucket']; for (const field of required) { if (!config[field]) { throw new Error(`OSS config missing required field: ${field}`); } } } /** * 验证 TOS 配置 */ validateTOSConfig(config) { const required = ['region', 'accessKeyId', 'accessKeySecret', 'bucket']; for (const field of required) { if (!config[field]) { throw new Error(`TOS config missing required field: ${field}`); } } } /** * 检查配置文件是否存在 */ async configExists() { return fs.pathExists(this.configPath); } /** * 获取配置文件路径 */ getConfigPath() { return this.configPath; } } exports.ConfigManager = ConfigManager; //# sourceMappingURL=ConfigManager.js.map