2022-05-28 13:35:32 +03:00
|
|
|
import * as fs from 'mz/fs'
|
2018-10-06 21:50:06 +03:00
|
|
|
import * as path from 'path'
|
|
|
|
import * as yaml from 'js-yaml'
|
2022-05-28 13:35:32 +03:00
|
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
|
|
import * as gracefulFS from 'graceful-fs'
|
2018-10-06 21:50:06 +03:00
|
|
|
import { app } from 'electron'
|
2022-05-28 13:35:32 +03:00
|
|
|
import { promisify } from 'util'
|
2018-10-06 21:50:06 +03:00
|
|
|
|
2022-05-28 13:35:32 +03:00
|
|
|
export async function migrateConfig (): Promise<void> {
|
2021-06-30 00:57:04 +03:00
|
|
|
const configPath = path.join(app.getPath('userData'), 'config.yaml')
|
|
|
|
const legacyConfigPath = path.join(app.getPath('userData'), '../terminus', 'config.yaml')
|
2022-05-28 13:35:32 +03:00
|
|
|
if (await fs.exists(legacyConfigPath) && (
|
|
|
|
!await fs.exists(configPath) ||
|
|
|
|
(await fs.stat(configPath)).mtime < (await fs.stat(legacyConfigPath)).mtime
|
2021-06-30 00:57:04 +03:00
|
|
|
)) {
|
2022-05-28 13:35:32 +03:00
|
|
|
await fs.writeFile(configPath, await fs.readFile(legacyConfigPath))
|
2021-06-30 00:57:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 13:35:32 +03:00
|
|
|
export async function loadConfig (): Promise<any> {
|
|
|
|
await migrateConfig()
|
2021-06-30 00:57:04 +03:00
|
|
|
|
2020-12-24 16:03:14 +03:00
|
|
|
const configPath = path.join(app.getPath('userData'), 'config.yaml')
|
2022-05-28 13:35:32 +03:00
|
|
|
if (await fs.exists(configPath)) {
|
|
|
|
return yaml.load(await fs.readFile(configPath, 'utf8'))
|
2018-10-06 21:50:06 +03:00
|
|
|
} else {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
}
|
2022-05-28 13:35:32 +03:00
|
|
|
|
|
|
|
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> {
|
|
|
|
try {
|
|
|
|
await _configSaveInProgress
|
|
|
|
} catch { }
|
|
|
|
_configSaveInProgress = _saveConfigInternal(content)
|
|
|
|
await _configSaveInProgress
|
|
|
|
}
|