From b2df0cc7d703dff928e7e5629320b2bb99850a68 Mon Sep 17 00:00:00 2001 From: Tsunekazu Omija Date: Tue, 22 Oct 2024 14:59:58 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E3=82=A2=E3=83=88=E3=83=9F=E3=83=83?= =?UTF-8?q?=E3=82=AF=E3=81=AA=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E3=82=92=E3=83=98=E3=83=AB=E3=83=91=E3=83=BC=E9=96=A2?= =?UTF-8?q?=E6=95=B0=E3=81=AB=E5=88=87=E3=82=8A=E5=87=BA=E3=81=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/electron/electronConfig.ts | 12 +++--------- src/helpers/fileHelper.ts | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/backend/electron/electronConfig.ts b/src/backend/electron/electronConfig.ts index 0fc90bf23b..846ad637fd 100644 --- a/src/backend/electron/electronConfig.ts +++ b/src/backend/electron/electronConfig.ts @@ -1,9 +1,9 @@ import { join } from "path"; import fs from "fs"; import { app } from "electron"; -import { moveFile } from "move-file"; import { BaseConfigManager, Metadata } from "@/backend/common/ConfigManager"; import { ConfigType } from "@/type/preload"; +import { writeFileSafely } from "@/helpers/fileHelper"; export class ElectronConfigManager extends BaseConfigManager { protected getAppVersion() { @@ -23,16 +23,10 @@ export class ElectronConfigManager extends BaseConfigManager { } protected async save(config: ConfigType & Metadata) { - // ファイル書き込みに失敗したときに設定が消えないように、tempファイル書き込み後上書き移動する - const temp_path = `${this.configPath}.tmp`; - await fs.promises.writeFile( - temp_path, + await writeFileSafely( + this.configPath, JSON.stringify(config, undefined, 2), ); - - await moveFile(temp_path, this.configPath, { - overwrite: true, - }); } private get configPath(): string { diff --git a/src/helpers/fileHelper.ts b/src/helpers/fileHelper.ts index d39640189c..d616c4628c 100644 --- a/src/helpers/fileHelper.ts +++ b/src/helpers/fileHelper.ts @@ -1,5 +1,20 @@ +import fs from "fs"; +import { moveFile } from "move-file"; import { ResultError } from "@/type/result"; +export async function writeFileSafely( + path: string, + data: string | NodeJS.ArrayBufferView, +) { + // ファイル書き込みに失敗したときに設定が消えないように、tempファイル書き込み後上書き移動する + const temp_path = `${path}.tmp`; + await fs.promises.writeFile(temp_path, data); + + await moveFile(temp_path, path, { + overwrite: true, + }); +} + /** ファイル書き込み時のエラーメッセージを生成する */ // instanceof ResultErrorで生まれるResultErrorを受け取れるようにするため、anyを許容する // eslint-disable-next-line @typescript-eslint/no-explicit-any From ab6ad25a65ff0545e11613a28530d7afc334333d Mon Sep 17 00:00:00 2001 From: Tsunekazu Omija Date: Tue, 22 Oct 2024 15:01:11 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E3=81=AE=E4=BF=9D=E5=AD=98=E3=82=92=E3=82=A2=E3=83=88=E3=83=9F?= =?UTF-8?q?=E3=83=83=E3=82=AF=E3=81=AA=E6=93=8D=E4=BD=9C=E3=81=AB=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/electron/main.ts | 5 +++-- src/backend/electron/manager/RuntimeInfoManager.ts | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/backend/electron/main.ts b/src/backend/electron/main.ts index 42a908ecca..e194853282 100644 --- a/src/backend/electron/main.ts +++ b/src/backend/electron/main.ts @@ -40,6 +40,7 @@ import { TextAsset, } from "@/type/preload"; import { themes } from "@/domain/theme"; +import { writeFileSafely } from "@/helpers/fileHelper"; type SingleInstanceLockData = { filePath: string | undefined; @@ -742,9 +743,9 @@ registerIpcMainHandle({ win.show(); }, - WRITE_FILE: (_, { filePath, buffer }) => { + WRITE_FILE: async (_, { filePath, buffer }) => { try { - fs.writeFileSync( + await 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..579f9b2c75 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 "@/helpers/fileHelper"; /** * ランタイム情報書き出しに必要なEngineInfo @@ -99,7 +99,7 @@ export class RuntimeInfoManager { // ファイル書き出し try { - await fs.promises.writeFile( + await writeFileSafely( this.runtimeInfoPath, JSON.stringify(runtimeInfoFormatFor3rdParty), // FIXME: zod化する );