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'
|
|
|
|
import { app } from 'electron'
|
2022-06-15 22:30:03 +03:00
|
|
|
import { writeFile } from 'atomically'
|
2022-06-15 21:56:47 +03:00
|
|
|
|
2018-10-06 21:50:06 +03:00
|
|
|
|
2022-05-31 00:27:01 +03:00
|
|
|
export function migrateConfig (): 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-31 00:27:01 +03:00
|
|
|
if (fs.existsSync(legacyConfigPath) && (
|
|
|
|
!fs.existsSync(configPath) ||
|
|
|
|
fs.statSync(configPath).mtime < fs.statSync(legacyConfigPath).mtime
|
2021-06-30 00:57:04 +03:00
|
|
|
)) {
|
2022-05-31 00:27:01 +03:00
|
|
|
fs.writeFileSync(configPath, fs.readFileSync(legacyConfigPath))
|
2021-06-30 00:57:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 00:27:01 +03:00
|
|
|
export function loadConfig (): any {
|
|
|
|
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-31 00:27:01 +03:00
|
|
|
if (fs.existsSync(configPath)) {
|
|
|
|
return yaml.load(fs.readFileSync(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')
|
|
|
|
|
|
|
|
export async function saveConfig (content: string): Promise<void> {
|
2022-06-15 22:30:03 +03:00
|
|
|
await writeFile(configPath, content, { encoding: 'utf8' })
|
|
|
|
await writeFile(configPath + '.backup', content, { encoding: 'utf8' })
|
2022-05-28 13:35:32 +03:00
|
|
|
}
|