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

設定ファイルの保存をアトミック操作に変更 #2098

Merged
merged 1 commit into from
May 27, 2024
Merged
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
9 changes: 8 additions & 1 deletion src/backend/electron/electronConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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";

Expand All @@ -21,10 +22,16 @@ export class ElectronConfigManager extends BaseConfigManager {
}

protected async save(config: ConfigType & Metadata) {
// ファイル書き込みに失敗したときに設定が消えないように、tempファイル書き込み後上書き移動する
const temp_path = `${this.configPath}.tmp`;
await fs.promises.writeFile(
this.configPath,
temp_path,
JSON.stringify(config, undefined, 2),
);

await moveFile(temp_path, this.configPath, {
overwrite: true,
});
}

private get configPath(): string {
Expand Down
Loading