|
1 | | -import { readFile, writeFile, mkdir } from 'fs/promises'; |
2 | | -import { dirname } from 'path'; |
| 1 | +import { readFile, writeFile, mkdir, copyFile, stat } from 'fs/promises'; |
| 2 | +import { dirname, join } from 'path'; |
3 | 3 |
|
4 | 4 | /** |
5 | 5 | * Base interface that all MCP config formats must implement. |
@@ -92,10 +92,28 @@ export abstract class ConfigManager<T extends MCPConfig, S = unknown> { |
92 | 92 | const wasEnabled = this.serverName in servers; |
93 | 93 | const serverConfig = this.createServerConfig(baseUrl, apiToken); |
94 | 94 | servers[this.serverName] = serverConfig; |
| 95 | + await this.createBackup(); |
95 | 96 | await this.writeConfig(config); |
96 | 97 | return wasEnabled; |
97 | 98 | } |
98 | 99 |
|
| 100 | + async createBackup(): Promise<void> { |
| 101 | + try { |
| 102 | + await stat(this.configPath); |
| 103 | + } catch (error) { |
| 104 | + if ((error as NodeJS.ErrnoException).code === 'ENOENT') { |
| 105 | + return; |
| 106 | + } |
| 107 | + throw error; |
| 108 | + } |
| 109 | + |
| 110 | + const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); |
| 111 | + const dir = dirname(this.configPath); |
| 112 | + const backupPath = join(dir, `mcp.backup.${timestamp}.json`); |
| 113 | + |
| 114 | + await copyFile(this.configPath, backupPath); |
| 115 | + } |
| 116 | + |
99 | 117 | /** |
100 | 118 | * Disable the server configuration. |
101 | 119 | * @returns true if the server was enabled and has been disabled, false if it was not enabled |
|
0 commit comments