From ef49aa213ed7a44949f3805c15e20c3aa2add880 Mon Sep 17 00:00:00 2001 From: Eugene Pankov Date: Wed, 15 Jun 2022 20:56:47 +0200 Subject: [PATCH] use write-file-atomic for config saving --- app/lib/config.ts | 21 ++++----------------- app/package.json | 4 +++- app/yarn.lock | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/app/lib/config.ts b/app/lib/config.ts index 9f13cfbe..25201fe1 100644 --- a/app/lib/config.ts +++ b/app/lib/config.ts @@ -1,10 +1,9 @@ import * as fs from 'mz/fs' import * as path from 'path' import * as yaml from 'js-yaml' -import { v4 as uuidv4 } from 'uuid' -import * as gracefulFS from 'graceful-fs' import { app } from 'electron' -import { promisify } from 'util' +import writeFileAtomic from 'write-file-atomic' + export function migrateConfig (): void { const configPath = path.join(app.getPath('userData'), 'config.yaml') @@ -28,21 +27,9 @@ export function loadConfig (): any { } } - const configPath = path.join(app.getPath('userData'), 'config.yaml') -let _configSaveInProgress = Promise.resolve() - -async function _saveConfigInternal (content: string): Promise { - const tempPath = configPath + '.new.' + uuidv4().toString() - await fs.writeFile(tempPath, content, 'utf8') - await fs.writeFile(configPath + '.backup', content, 'utf8') - await promisify(gracefulFS.rename)(tempPath, configPath) -} export async function saveConfig (content: string): Promise { - try { - await _configSaveInProgress - } catch { } - _configSaveInProgress = _saveConfigInternal(content) - await _configSaveInProgress + await writeFileAtomic(configPath, content, { encoding: 'utf8' }) + await writeFileAtomic(configPath + '.backup', content, { encoding: 'utf8' }) } diff --git a/app/package.json b/app/package.json index be9fc1f3..099e0070 100644 --- a/app/package.json +++ b/app/package.json @@ -45,8 +45,10 @@ "devDependencies": { "@types/mz": "2.7.4", "@types/node": "16.0.1", + "@types/write-file-atomic": "^4.0.0", "ngx-filesize": "^2.0.16", - "patch-package": "^6.4.7" + "patch-package": "^6.4.7", + "write-file-atomic": "^4.0.1" }, "peerDependencies": { "tabby-community-color-schemes": "*", diff --git a/app/yarn.lock b/app/yarn.lock index e105589a..7a586033 100644 --- a/app/yarn.lock +++ b/app/yarn.lock @@ -150,6 +150,13 @@ resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.9.tgz#152c6c20a7688c30b967ec1841d31ace569863fc" integrity sha512-L/TMpyURfBkf+o/526Zb6kd/tchUP3iBDEPjqjb+U2MAJhVRxxrmr2fwpe08E7QsV7YLcpq0tUaQ9O9x97ZIxQ== +"@types/write-file-atomic@^4.0.0": + version "4.0.0" + resolved "https://registry.yarnpkg.com/@types/write-file-atomic/-/write-file-atomic-4.0.0.tgz#ffcedcb1ae027e0a28ddfe218b72b3573797b5bc" + integrity sha512-piEKt2KKBUtye+feTlfdPjtW7uPFsAaLNX3/f6AJD+Y1T1YPTFwnqtlO9Y+gy9qGshrvxKa/Kay9vqbyVIuhwQ== + dependencies: + "@types/node" "*" + "@yarnpkg/lockfile@^1.1.0": version "1.1.0" resolved "https://registry.yarnpkg.com/@yarnpkg/lockfile/-/lockfile-1.1.0.tgz#e77a97fbd345b76d83245edcd17d393b1b41fb31" @@ -3301,6 +3308,11 @@ signal-exit@^3.0.0, signal-exit@^3.0.2: resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.3.tgz" integrity sha512-VUJ49FC8U1OxwZLxIbTTrDvLnf/6TDgxZcK8wxR8zs13xpx7xbG60ndBlhNrFi2EMuFRoeDoJO7wthSLq42EjA== +signal-exit@^3.0.7: + version "3.0.7" + resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.7.tgz#a9a1767f8af84155114eaabd73f99273c8f59ad9" + integrity sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ== + simple-concat@^1.0.0: version "1.0.1" resolved "https://registry.npmjs.org/simple-concat/-/simple-concat-1.0.1.tgz" @@ -3934,6 +3946,14 @@ write-file-atomic@^2.0.0, write-file-atomic@^2.3.0, write-file-atomic@^2.4.3: imurmurhash "^0.1.4" signal-exit "^3.0.2" +write-file-atomic@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/write-file-atomic/-/write-file-atomic-4.0.1.tgz#9faa33a964c1c85ff6f849b80b42a88c2c537c8f" + integrity sha512-nSKUxgAbyioruk6hU87QzVbY279oYT6uiwgDoujth2ju4mJ+TZau7SQBhtbTmUyuNYTuXnSyRn66FV0+eCgcrQ== + dependencies: + imurmurhash "^0.1.4" + signal-exit "^3.0.7" + x11@^2.3.0: version "2.3.0" resolved "https://registry.npmjs.org/x11/-/x11-2.3.0.tgz"