UNPKG

@nexe/config-manager

Version:

Nexe Config Manager - A flexible configuration management solution with multiple sources and hot reload support

65 lines 2.64 kB
"use strict"; // /** // * 配置系统热更新测试 // * // * 这个示例专门测试配置热更新功能 // * // * 运行方式: // * bun src/shared/infrastructure/config/examples/hot-reload-test.ts // * // * 测试步骤: // * 1. 运行此脚本 // * 2. 在另一个终端中修改配置文件 // * echo "server:\n maxConnections: 500" > src/shared/infrastructure/config/examples/config/test-hot-reload.yaml // * 3. 观察控制台输出,应该看到配置变更通知 // */ // import * as fs from 'fs'; // import * as path from 'path'; // import 'reflect-metadata'; // import { ConfigManager } from '..'; // import { YamlConfigSource } from '../sources/yaml-config-source'; // import { ConfigLogger } from '../utils/config-logger'; // // 启用日志 // ConfigLogger.getInstance().setLogEnabled(true); // // 获取当前脚本目录 // const SCRIPT_DIR = path.dirname(new URL(import.meta.url).pathname); // const CONFIG_DIR = path.join(SCRIPT_DIR, 'config'); // const TEST_CONFIG_PATH = path.join(CONFIG_DIR, 'test-hot-reload.yaml'); // // 确保配置目录存在 // if (!fs.existsSync(CONFIG_DIR)) { // fs.mkdirSync(CONFIG_DIR, { recursive: true }); // } // // 创建测试配置文件 // const yamlConfig = `server: // maxConnections: 100 // `; // fs.writeFileSync(TEST_CONFIG_PATH, yamlConfig, 'utf-8'); // console.log(`✅ 创建测试配置文件: ${TEST_CONFIG_PATH}`); // async function main(): Promise<void> { // console.log('🚀 启动热更新测试...'); // // 创建配置管理器 // const configManager = new ConfigManager(); // // 添加YAML配置源并启用监听 // console.log(`📦 添加YAML配置源: ${TEST_CONFIG_PATH}`); // configManager.addSource(new YamlConfigSource(TEST_CONFIG_PATH, true)); // // 读取初始值 // const initialValue = await configManager.get<number>('server.maxConnections'); // console.log(`📊 初始值 server.maxConnections = ${initialValue}`); // // 设置变更监听 // console.log('👀 设置配置变更监听...'); // configManager.subscribe<number>('server.maxConnections', (newValue) => { // console.log(`🔄 配置已更新: server.maxConnections = ${newValue}`); // }); // console.log('\n📝 请在另一个终端中修改配置文件:'); // console.log( // `echo "server:\\n maxConnections: 500" > "${TEST_CONFIG_PATH}"\n`, // ); // // 保持进程运行 // console.log('⏳ 等待配置变更 (按 Ctrl+C 退出)...\n'); // } // // 运行主函数 // main().catch((error) => { // console.error('❌ 错误:', error); // process.exit(1); // }); //# sourceMappingURL=hot-reload-test.js.map