diff --git a/src/backend/electron/electronConfig.ts b/src/backend/electron/electronConfig.ts index 0fc90bf23b..59dbfba4a5 100644 --- a/src/backend/electron/electronConfig.ts +++ b/src/backend/electron/electronConfig.ts @@ -1,7 +1,7 @@ import { join } from "path"; import fs from "fs"; import { app } from "electron"; -import { moveFile } from "move-file"; +import { writeFileSafely } from "./fileHelper"; import { BaseConfigManager, Metadata } from "@/backend/common/ConfigManager"; import { ConfigType } from "@/type/preload"; @@ -23,16 +23,7 @@ export class ElectronConfigManager extends BaseConfigManager { } protected async save(config: ConfigType & Metadata) { - // ファイル書き込みに失敗したときに設定が消えないように、tempファイル書き込み後上書き移動する - const temp_path = `${this.configPath}.tmp`; - await fs.promises.writeFile( - temp_path, - JSON.stringify(config, undefined, 2), - ); - - await moveFile(temp_path, this.configPath, { - overwrite: true, - }); + writeFileSafely(this.configPath, JSON.stringify(config, undefined, 2)); } private get configPath(): string { diff --git a/src/backend/electron/fileHelper.ts b/src/backend/electron/fileHelper.ts new file mode 100644 index 0000000000..9d80ac0c44 --- /dev/null +++ b/src/backend/electron/fileHelper.ts @@ -0,0 +1,18 @@ +import fs from "fs"; +import { moveFileSync } from "move-file"; + +/** + * 書き込みに失敗したときにファイルが消えないように、 + * tmpファイル書き込み後、保存先ファイルに上書きする + */ +export function writeFileSafely( + path: string, + data: string | NodeJS.ArrayBufferView, +) { + const tmpPath = `${path}.tmp`; + fs.writeFileSync(tmpPath, data); + + moveFileSync(tmpPath, path, { + overwrite: true, + }); +} diff --git a/src/backend/electron/main.ts b/src/backend/electron/main.ts index 42a908ecca..463262dd6f 100644 --- a/src/backend/electron/main.ts +++ b/src/backend/electron/main.ts @@ -28,6 +28,7 @@ import { RuntimeInfoManager } from "./manager/RuntimeInfoManager"; import { registerIpcMainHandle, ipcMainSendProxy, IpcMainHandle } from "./ipc"; import { getConfigManager } from "./electronConfig"; import { EngineAndVvppController } from "./engineAndVvppController"; +import { writeFileSafely } from "./fileHelper"; import { failure, success } from "@/type/result"; import { AssetTextFileNames } from "@/type/staticResources"; import { @@ -744,7 +745,7 @@ registerIpcMainHandle({ WRITE_FILE: (_, { filePath, buffer }) => { try { - fs.writeFileSync( + writeFileSafely( filePath, new DataView(buffer instanceof Uint8Array ? buffer.buffer : buffer), ); diff --git a/src/backend/electron/manager/RuntimeInfoManager.ts b/src/backend/electron/manager/RuntimeInfoManager.ts index e3ec4340ca..04683ea375 100644 --- a/src/backend/electron/manager/RuntimeInfoManager.ts +++ b/src/backend/electron/manager/RuntimeInfoManager.ts @@ -3,11 +3,11 @@ * ランタイム情報には起動しているエンジンのURLなどが含まれる。 */ -import fs from "fs"; import AsyncLock from "async-lock"; import log from "electron-log/main"; import type { AltPortInfos } from "@/store/type"; import { EngineId, EngineInfo } from "@/type/preload"; +import { writeFileSafely } from "@/backend/electron/fileHelper"; /** * ランタイム情報書き出しに必要なEngineInfo @@ -99,7 +99,7 @@ export class RuntimeInfoManager { // ファイル書き出し try { - await fs.promises.writeFile( + writeFileSafely( this.runtimeInfoPath, JSON.stringify(runtimeInfoFormatFor3rdParty), // FIXME: zod化する );