Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.14でconfig.jsonの場所が変わるのでマイグレーション #1094

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ import {
import log from "electron-log";
import dayjs from "dayjs";
import windowStateKeeper from "electron-window-state";
import zodToJsonSchema from "zod-to-json-schema";

import EngineManager from "./background/engineManager";
import VvppManager from "./background/vvppManager";
import zodToJsonSchema from "zod-to-json-schema";
import configMigration014 from "./background/configMigration014";

type SingleInstanceLockData = {
filePath: string | undefined;
Expand All @@ -48,6 +50,7 @@ type SingleInstanceLockData = {
const isDevelopment = process.env.NODE_ENV !== "production";

// Electronの設定ファイルの保存場所を変更
const beforeUserDataDir = app.getPath("userData"); // 設定ファイルのマイグレーション用
const fixedUserDataDir = path.join(
app.getPath("appData"),
`voicevox${isDevelopment ? "-dev" : ""}`
Expand All @@ -56,6 +59,9 @@ if (!fs.existsSync(fixedUserDataDir)) {
fs.mkdirSync(fixedUserDataDir);
}
app.setPath("userData", fixedUserDataDir);
if (!isDevelopment) {
configMigration014({ fixedUserDataDir, beforeUserDataDir }); // 以前のファイルがあれば持ってくる
}

// silly以上のログをコンソールに出力
log.transports.console.format = "[{h}:{i}:{s}.{ms}] [{level}] {text}";
Expand Down
58 changes: 58 additions & 0 deletions src/background/configMigration014.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// configファイルの場所が0.14で変わったので、以前のファイルを持ってくる
import fs from "fs";
import path from "path";

import semver from "semver";

function _logInfo(message: string) {
console.info(`configMigration014: ${message}`);
}

function _logError(message: string) {
console.error(`configMigration014: ${message}`);
}

export default function ({
fixedUserDataDir,
beforeUserDataDir,
}: {
fixedUserDataDir: string;
beforeUserDataDir: string;
}) {
try {
// ファイルが存在していてバージョン0.14以上であれば何もしない
const configPath = path.join(fixedUserDataDir, "config.json");
if (fs.existsSync(configPath)) {
const config = JSON.parse(fs.readFileSync(configPath, "utf-8"));

if (config?.__internal__?.migrations?.version === undefined) {
throw new Error("configMigration014: config.json is invalid");
}

if (semver.satisfies(config.__internal__.migrations.version, ">=0.14")) {
_logInfo(`>=0.14 ${configPath} exists, do nothing`);
return;
}
}

// 以前のファイルが存在していればコピー
const beforeConfigPath = path.join(beforeUserDataDir, "config.json");
if (fs.existsSync(beforeConfigPath)) {
// 今のconfigがあれば念のためバックアップ
if (fs.existsSync(configPath)) {
const backupPath = path.join(fixedUserDataDir, "config-backup.json");
_logInfo(`backup to ${backupPath}`);
fs.copyFileSync(configPath, backupPath);
}

_logInfo(`copy from ${beforeConfigPath} to ${configPath}`);
fs.copyFileSync(beforeConfigPath, configPath);
return;
} else {
_logInfo(`${beforeConfigPath} not exists, do nothing`);
}
} catch (e) {
_logError("error!");
console.error(e);
}
}