UNPKG

sync-upstream

Version:

A tool for synchronizing code with upstream repositories with incremental updates and parallel processing.

140 lines (139 loc) 5.11 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadConfig = loadConfig; exports.saveConfig = saveConfig; const node_path_1 = __importDefault(require("node:path")); const toml_1 = __importDefault(require("@iarna/toml")); // src/config.ts const fs_extra_1 = __importDefault(require("fs-extra")); const js_yaml_1 = __importDefault(require("js-yaml")); const types_1 = require("./types"); const DEFAULT_CONFIG = { upstreamBranch: 'main', companyBranch: 'main', commitMessage: 'Sync upstream changes to specified directories', autoPush: false, forceOverwrite: true, verbose: false, silent: false, dryRun: false, previewOnly: false, concurrencyLimit: 5, retryConfig: { maxRetries: 3, initialDelay: 2000, backoffFactor: 1.5, }, conflictResolutionConfig: { defaultStrategy: types_1.ConflictResolutionStrategy.PROMPT_USER, }, }; const CONFIG_FILES = [ '.sync-toolrc.json', '.sync-toolrc.yaml', '.sync-toolrc.yml', '.sync-toolrc.toml', '.sync-toolrc', 'sync-tool.config.json', 'sync-tool.config.yaml', 'sync-tool.config.yml', 'sync-tool.config.toml', ]; /** * 验证配置是否有效 */ function validateConfig(config) { // 验证冲突解决策略 if (config.conflictResolutionConfig?.defaultStrategy) { const validStrategies = Object.values(types_1.ConflictResolutionStrategy); if (!validStrategies.includes(config.conflictResolutionConfig.defaultStrategy)) { throw new Error(`无效的冲突解决策略: ${config.conflictResolutionConfig.defaultStrategy}. 有效策略: ${validStrategies.join(', ')}`); } } // 验证重试配置 if (config.retryConfig) { if (config.retryConfig.maxRetries !== undefined && config.retryConfig.maxRetries < 0) { throw new Error('最大重试次数不能为负数'); } if (config.retryConfig.initialDelay !== undefined && config.retryConfig.initialDelay < 0) { throw new Error('初始重试延迟不能为负数'); } if (config.retryConfig.backoffFactor !== undefined && config.retryConfig.backoffFactor < 1) { throw new Error('重试退避因子必须大于或等于1'); } } // 验证并行限制 if (config.concurrencyLimit !== undefined && config.concurrencyLimit < 1) { throw new Error('并行处理数量必须大于或等于1'); } } /** * 查找并加载配置文件 */ async function loadConfig() { // 检查当前目录是否有配置文件 for (const filename of CONFIG_FILES) { const configPath = node_path_1.default.join(process.cwd(), filename); if (await fs_extra_1.default.pathExists(configPath)) { try { const fileContent = await fs_extra_1.default.readFile(configPath, 'utf8'); let config = {}; // 根据文件扩展名选择解析方法 if (filename.endsWith('.json')) { config = JSON.parse(fileContent); } else if (filename.endsWith('.yaml') || filename.endsWith('.yml')) { config = js_yaml_1.default.load(fileContent); } else if (filename.endsWith('.toml')) { config = toml_1.default.parse(fileContent); } else { // 尝试作为JSON解析(保持向后兼容) config = JSON.parse(fileContent); } // 验证配置 validateConfig(config); return { ...DEFAULT_CONFIG, ...config }; } catch (error) { console.error(`读取配置文件 ${filename} 失败:`, error); } } } return DEFAULT_CONFIG; } /** * 保存配置到文件 * @param config 配置对象 * @param format 保存格式,可选值: 'json', 'yaml', 'toml',默认为 'json' */ async function saveConfig(config, format = 'json') { let configPath; let fileContent; switch (format) { case 'yaml': configPath = node_path_1.default.join(process.cwd(), '.sync-toolrc.yaml'); fileContent = js_yaml_1.default.dump(config); break; case 'toml': configPath = node_path_1.default.join(process.cwd(), '.sync-toolrc.toml'); fileContent = toml_1.default.stringify(config); break; case 'json': default: configPath = node_path_1.default.join(process.cwd(), '.sync-toolrc.json'); fileContent = JSON.stringify(config, null, 2); break; } try { await fs_extra_1.default.writeFile(configPath, fileContent, 'utf8'); console.log(`配置已保存到 ${configPath}`); } catch (error) { console.error('保存配置失败:', error); } }