feat: 增加配置导入导出功能
This commit is contained in:
+83
-4
@@ -9,13 +9,17 @@
|
||||
*/
|
||||
|
||||
const path = require('node:path');
|
||||
const fs = require('node:fs');
|
||||
const {
|
||||
deepMerge, readJson, writeJson, uid, toNum, maskSecret, log, isPlainObject,
|
||||
deepMerge, readJson, writeJson, uid, toNum, maskSecret, log, isPlainObject, ensureDir, nowText,
|
||||
} = require('./util');
|
||||
const { PRESETS, buildInstrument } = require('./presets');
|
||||
|
||||
const ROOT = path.resolve(__dirname, '..');
|
||||
const CONFIG_FILE = path.join(ROOT, 'config', 'config.json');
|
||||
/** 导入前的自动备份目录,最多保留 20 份 */
|
||||
const BACKUP_DIR = path.join(ROOT, 'config', 'backups');
|
||||
const MAX_BACKUPS = 20;
|
||||
const SCHEMA_VERSION = 1;
|
||||
|
||||
/** 允许的通知渠道 */
|
||||
@@ -383,14 +387,87 @@ function forClient() {
|
||||
};
|
||||
}
|
||||
|
||||
/** 导出完整配置(含真实密码,用于备份,仅本地接口使用) */
|
||||
function exportFile() {
|
||||
return get();
|
||||
/**
|
||||
* 导出配置
|
||||
* @param {boolean} includeSecrets 是否带上真实密钥(邮箱密码 / WebHook 加签)
|
||||
* @returns {object} 可直接下载为 JSON 的配置对象
|
||||
*/
|
||||
function exportFile(includeSecrets = false) {
|
||||
const cfg = JSON.parse(JSON.stringify(get()));
|
||||
if (!includeSecrets) {
|
||||
// 默认脱敏:方便把配置文件发给别人或贴到聊天里
|
||||
if (cfg.email) cfg.email.pass = '';
|
||||
if (cfg.webhook) cfg.webhook.secret = '';
|
||||
}
|
||||
return {
|
||||
...cfg,
|
||||
exportedAt: nowText(),
|
||||
exportTool: 'gold-price-watcher',
|
||||
exportVersion: SCHEMA_VERSION,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成文件名用的时间戳(本地时间,精确到秒)
|
||||
* @param {Date} date 时间
|
||||
* @returns {string} 例如 20261124-165012
|
||||
*/
|
||||
function fileStamp(date = new Date()) {
|
||||
const pad = (n) => String(n).padStart(2, '0');
|
||||
return `${date.getFullYear()}${pad(date.getMonth() + 1)}${pad(date.getDate())}`
|
||||
+ `-${pad(date.getHours())}${pad(date.getMinutes())}${pad(date.getSeconds())}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 把当前配置备份到 config/backups/,返回备份文件名
|
||||
* 导入前先调用,出问题可以直接拿这个文件回滚
|
||||
* @returns {string|null} 备份文件名
|
||||
*/
|
||||
function backupCurrent() {
|
||||
try {
|
||||
ensureDir(BACKUP_DIR);
|
||||
const name = `config-${fileStamp()}.json`;
|
||||
writeJson(path.join(BACKUP_DIR, name), get());
|
||||
// 只保留最近 MAX_BACKUPS 份,避免越攒越多
|
||||
const files = fs.readdirSync(BACKUP_DIR).filter((f) => f.endsWith('.json')).sort();
|
||||
while (files.length > MAX_BACKUPS) {
|
||||
const oldest = files.shift();
|
||||
try { fs.unlinkSync(path.join(BACKUP_DIR, oldest)); } catch { /* 删不掉就下次再说 */ }
|
||||
}
|
||||
return name;
|
||||
} catch (err) {
|
||||
log.warn(`备份当前配置失败:${err.message}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 列出最近的配置备份
|
||||
* @param {number} limit 返回条数
|
||||
* @returns {{name: string, size: number, at: string}[]}
|
||||
*/
|
||||
function listBackups(limit = 5) {
|
||||
if (!fs.existsSync(BACKUP_DIR)) return [];
|
||||
return fs.readdirSync(BACKUP_DIR)
|
||||
.filter((f) => f.endsWith('.json'))
|
||||
.sort()
|
||||
.slice(-limit)
|
||||
.reverse()
|
||||
.map((name) => {
|
||||
const filePath = path.join(BACKUP_DIR, name);
|
||||
const stat = fs.statSync(filePath);
|
||||
return {
|
||||
name,
|
||||
size: stat.size,
|
||||
at: nowText(new Date(stat.mtimeMs)),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ROOT,
|
||||
CONFIG_FILE,
|
||||
BACKUP_DIR,
|
||||
defaultConfig,
|
||||
normalizeConfig,
|
||||
load,
|
||||
@@ -399,4 +476,6 @@ module.exports = {
|
||||
patch,
|
||||
forClient,
|
||||
exportFile,
|
||||
backupCurrent,
|
||||
listBackups,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user