use write-file-atomic for config saving

This commit is contained in:
Eugene Pankov
2022-06-15 20:56:47 +02:00
parent 8d5dffcca1
commit ef49aa213e
3 changed files with 27 additions and 18 deletions

View File

@@ -1,10 +1,9 @@
import * as fs from 'mz/fs' import * as fs from 'mz/fs'
import * as path from 'path' import * as path from 'path'
import * as yaml from 'js-yaml' import * as yaml from 'js-yaml'
import { v4 as uuidv4 } from 'uuid'
import * as gracefulFS from 'graceful-fs'
import { app } from 'electron' import { app } from 'electron'
import { promisify } from 'util' import writeFileAtomic from 'write-file-atomic'
export function migrateConfig (): void { export function migrateConfig (): void {
const configPath = path.join(app.getPath('userData'), 'config.yaml') 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') const configPath = path.join(app.getPath('userData'), 'config.yaml')
let _configSaveInProgress = Promise.resolve()
async function _saveConfigInternal (content: string): Promise<void> {
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<void> { export async function saveConfig (content: string): Promise<void> {
try { await writeFileAtomic(configPath, content, { encoding: 'utf8' })
await _configSaveInProgress await writeFileAtomic(configPath + '.backup', content, { encoding: 'utf8' })
} catch { }
_configSaveInProgress = _saveConfigInternal(content)
await _configSaveInProgress
} }

View File

@@ -45,8 +45,10 @@
"devDependencies": { "devDependencies": {
"@types/mz": "2.7.4", "@types/mz": "2.7.4",
"@types/node": "16.0.1", "@types/node": "16.0.1",
"@types/write-file-atomic": "^4.0.0",
"ngx-filesize": "^2.0.16", "ngx-filesize": "^2.0.16",
"patch-package": "^6.4.7" "patch-package": "^6.4.7",
"write-file-atomic": "^4.0.1"
}, },
"peerDependencies": { "peerDependencies": {
"tabby-community-color-schemes": "*", "tabby-community-color-schemes": "*",

View File

@@ -150,6 +150,13 @@
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.9.tgz#152c6c20a7688c30b967ec1841d31ace569863fc" resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.9.tgz#152c6c20a7688c30b967ec1841d31ace569863fc"
integrity sha512-L/TMpyURfBkf+o/526Zb6kd/tchUP3iBDEPjqjb+U2MAJhVRxxrmr2fwpe08E7QsV7YLcpq0tUaQ9O9x97ZIxQ== 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": "@yarnpkg/lockfile@^1.1.0":
version "1.1.0" version "1.1.0"
resolved "https://registry.yarnpkg.com/@yarnpkg/lockfile/-/lockfile-1.1.0.tgz#e77a97fbd345b76d83245edcd17d393b1b41fb31" 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" resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.3.tgz"
integrity sha512-VUJ49FC8U1OxwZLxIbTTrDvLnf/6TDgxZcK8wxR8zs13xpx7xbG60ndBlhNrFi2EMuFRoeDoJO7wthSLq42EjA== 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: simple-concat@^1.0.0:
version "1.0.1" version "1.0.1"
resolved "https://registry.npmjs.org/simple-concat/-/simple-concat-1.0.1.tgz" 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" imurmurhash "^0.1.4"
signal-exit "^3.0.2" 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: x11@^2.3.0:
version "2.3.0" version "2.3.0"
resolved "https://registry.npmjs.org/x11/-/x11-2.3.0.tgz" resolved "https://registry.npmjs.org/x11/-/x11-2.3.0.tgz"