2022-05-28 12:35:32 +02:00
|
|
|
import * as fs from 'mz/fs'
|
2018-10-06 20:50:06 +02:00
|
|
|
import * as path from 'path'
|
|
|
|
import * as yaml from 'js-yaml'
|
|
|
|
import { app } from 'electron'
|
2022-06-15 21:30:03 +02:00
|
|
|
import { writeFile } from 'atomically'
|
2022-06-15 20:56:47 +02:00
|
|
|
|
2018-10-06 20:50:06 +02:00
|
|
|
|
2022-05-30 23:27:01 +02:00
|
|
|
export function migrateConfig (): void {
|
2021-06-29 23:57:04 +02:00
|
|
|
const configPath = path.join(app.getPath('userData'), 'config.yaml')
|
|
|
|
const legacyConfigPath = path.join(app.getPath('userData'), '../terminus', 'config.yaml')
|
2022-05-30 23:27:01 +02:00
|
|
|
if (fs.existsSync(legacyConfigPath) && (
|
|
|
|
!fs.existsSync(configPath) ||
|
|
|
|
fs.statSync(configPath).mtime < fs.statSync(legacyConfigPath).mtime
|
2021-06-29 23:57:04 +02:00
|
|
|
)) {
|
2022-05-30 23:27:01 +02:00
|
|
|
fs.writeFileSync(configPath, fs.readFileSync(legacyConfigPath))
|
2021-06-29 23:57:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 23:27:01 +02:00
|
|
|
export function loadConfig (): any {
|
|
|
|
migrateConfig()
|
2021-06-29 23:57:04 +02:00
|
|
|
|
2020-12-24 14:03:14 +01:00
|
|
|
const configPath = path.join(app.getPath('userData'), 'config.yaml')
|
2022-05-30 23:27:01 +02:00
|
|
|
if (fs.existsSync(configPath)) {
|
|
|
|
return yaml.load(fs.readFileSync(configPath, 'utf8'))
|
2018-10-06 20:50:06 +02:00
|
|
|
} else {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
}
|
2022-05-28 12:35:32 +02:00
|
|
|
|
|
|
|
const configPath = path.join(app.getPath('userData'), 'config.yaml')
|
|
|
|
|
|
|
|
export async function saveConfig (content: string): Promise<void> {
|
2022-06-15 21:30:03 +02:00
|
|
|
await writeFile(configPath, content, { encoding: 'utf8' })
|
|
|
|
await writeFile(configPath + '.backup', content, { encoding: 'utf8' })
|
2022-05-28 12:35:32 +02:00
|
|
|
}
|