mirror of
https://github.com/MCSManager/MCSManager.git
synced 2025-04-06 17:10:29 +08:00
Feat: config version adapter
This commit is contained in:
parent
5747603fea
commit
5bf3e7bac9
@ -23,6 +23,13 @@ export default class StorageSubsystem {
|
||||
return fs.readFileSync(targetPath, { encoding: "utf-8" });
|
||||
}
|
||||
|
||||
public readDir(dirName: string) {
|
||||
const targetPath = path.normalize(path.join(StorageSubsystem.DATA_PATH, dirName));
|
||||
if (!fs.existsSync(targetPath)) return [];
|
||||
const files = fs.readdirSync(targetPath).map((v) => path.normalize(path.join(dirName, v)));
|
||||
return files;
|
||||
}
|
||||
|
||||
public deleteFile(name: string) {
|
||||
const targetPath = path.normalize(path.join(StorageSubsystem.DATA_PATH, name));
|
||||
fs.removeSync(targetPath);
|
||||
|
@ -1,6 +1,7 @@
|
||||
import "module-alias/register";
|
||||
import http from "http";
|
||||
import fs from "fs-extra";
|
||||
import versionAdapter from "./service/version_adapter";
|
||||
import { $t, i18next } from "./i18n";
|
||||
import { getVersion, initVersionManager } from "./service/version";
|
||||
import { globalConfiguration } from "./entity/config";
|
||||
@ -33,6 +34,9 @@ _ /_/ // /_/ // __/ / / / / / /_/ / / / /
|
||||
globalConfiguration.load();
|
||||
const config = globalConfiguration.config;
|
||||
|
||||
// Detect whether the configuration file is from an older version and update it if so.
|
||||
versionAdapter.detectConfig();
|
||||
|
||||
// Set language
|
||||
if (fs.existsSync(LOCAL_PRESET_LANG_PATH)) {
|
||||
i18next.changeLanguage(fs.readFileSync(LOCAL_PRESET_LANG_PATH, "utf-8"));
|
||||
|
32
daemon/src/service/version_adapter.ts
Normal file
32
daemon/src/service/version_adapter.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import StorageSubsystem from "../common/system_storage";
|
||||
import logger from "./log";
|
||||
import { t } from "i18next";
|
||||
|
||||
function readCategoryConfig(configCategory: string, callback: (config: any) => boolean) {
|
||||
const configPaths = StorageSubsystem.readDir(configCategory);
|
||||
for (const configPath of configPaths) {
|
||||
try {
|
||||
const config = JSON.parse(StorageSubsystem.readFile(configPath));
|
||||
if (callback(config)) {
|
||||
logger.info(t("已将旧版本配置升级为新版本配置:"), configPath);
|
||||
StorageSubsystem.writeFile(configPath, JSON.stringify(config, null, 4));
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(t("[配置升级机制] 解析配置文件错误,已自动忽略:"), error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function refactorInstanceConfig(config: any) {
|
||||
if (typeof config.endTime === "string") {
|
||||
config.endTime = new Date(config.endTime).getTime();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function detectConfig() {
|
||||
readCategoryConfig("InstanceConfig", refactorInstanceConfig);
|
||||
}
|
||||
|
||||
export default { detectConfig };
|
@ -17,6 +17,7 @@ import open from "open";
|
||||
import { fileLogger, logger } from "./app/service/log";
|
||||
import { middleware as protocolMiddleware } from "./app/middleware/protocol";
|
||||
import { mountRouters } from "./app/index";
|
||||
import versionAdapter from "./app/service/version_adapter";
|
||||
|
||||
function setupHttp(koaApp: Koa, port: number, host?: string) {
|
||||
const httpServer = http.createServer(koaApp.callback());
|
||||
@ -87,6 +88,10 @@ _ / / / / /___ ____/ /_ / / / / /_/ /_ / / / /_/ /_ /_/ // __/ /
|
||||
+ Version ${VERSION}
|
||||
`);
|
||||
|
||||
// Detect whether the configuration file is from an older version and update it if so.
|
||||
versionAdapter.detectConfig();
|
||||
|
||||
// Initialize services
|
||||
await SystemUser.initialize();
|
||||
await SystemRemoteService.initialize();
|
||||
|
||||
|
39
panel/src/app/service/version_adapter.ts
Normal file
39
panel/src/app/service/version_adapter.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import StorageSubsystem from "../common/system_storage";
|
||||
import { t } from "i18next";
|
||||
import { logger } from "./log";
|
||||
|
||||
function readCategoryConfig(configCategory: string, callback: (config: any) => boolean) {
|
||||
const configPaths = StorageSubsystem.readDir(configCategory);
|
||||
for (const configPath of configPaths) {
|
||||
try {
|
||||
const config = JSON.parse(StorageSubsystem.readFile(configPath));
|
||||
if (callback(config)) {
|
||||
logger.info(t("已将旧版本配置升级为新版本配置:"), configPath);
|
||||
StorageSubsystem.writeFile(configPath, JSON.stringify(config, null, 4));
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(t("[配置升级机制] 解析配置文件错误,已自动忽略:"), error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function refactorUserConfig(config: any) {
|
||||
let changed = false;
|
||||
const { instances } = config;
|
||||
if (instances instanceof Array && instances.length > 0) {
|
||||
for (const iterator of instances) {
|
||||
if (typeof iterator.serviceUuid === "string") {
|
||||
iterator.daemonId = iterator.serviceUuid;
|
||||
delete iterator.serviceUuid;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
function detectConfig() {
|
||||
readCategoryConfig("User", refactorUserConfig);
|
||||
}
|
||||
|
||||
export default { detectConfig };
|
Loading…
x
Reference in New Issue
Block a user