2022-12-05 13:52:23 +00:00
|
|
|
import * as fs from 'fs'
|
2018-10-06 18:50:06 +00:00
|
|
|
import * as path from 'path'
|
|
|
|
import * as yaml from 'js-yaml'
|
2022-06-15 19:30:03 +00:00
|
|
|
import { writeFile } from 'atomically'
|
2022-06-15 18:56:47 +00:00
|
|
|
|
2018-10-06 18:50:06 +00:00
|
|
|
|
2023-10-03 06:06:16 +00:00
|
|
|
export const configPath = path.join(process.env.TABBY_CONFIG_DIRECTORY!, 'config.yaml')
|
|
|
|
const legacyConfigPath = path.join(process.env.TABBY_CONFIG_DIRECTORY!, '../terminus', 'config.yaml')
|
2023-09-30 19:05:57 +00:00
|
|
|
|
|
|
|
|
2022-05-30 21:27:01 +00:00
|
|
|
export function migrateConfig (): void {
|
|
|
|
if (fs.existsSync(legacyConfigPath) && (
|
|
|
|
!fs.existsSync(configPath) ||
|
|
|
|
fs.statSync(configPath).mtime < fs.statSync(legacyConfigPath).mtime
|
2021-06-29 21:57:04 +00:00
|
|
|
)) {
|
2022-05-30 21:27:01 +00:00
|
|
|
fs.writeFileSync(configPath, fs.readFileSync(legacyConfigPath))
|
2021-06-29 21:57:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 21:27:01 +00:00
|
|
|
export function loadConfig (): any {
|
|
|
|
migrateConfig()
|
2021-06-29 21:57:04 +00:00
|
|
|
|
2022-05-30 21:27:01 +00:00
|
|
|
if (fs.existsSync(configPath)) {
|
|
|
|
return yaml.load(fs.readFileSync(configPath, 'utf8'))
|
2018-10-06 18:50:06 +00:00
|
|
|
} else {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
}
|
2022-05-28 10:35:32 +00:00
|
|
|
|
|
|
|
export async function saveConfig (content: string): Promise<void> {
|
2022-06-15 19:30:03 +00:00
|
|
|
await writeFile(configPath, content, { encoding: 'utf8' })
|
|
|
|
await writeFile(configPath + '.backup', content, { encoding: 'utf8' })
|
2023-09-30 19:30:25 +00:00
|
|
|
}
|