2022-12-05 21:52:23 +08:00
|
|
|
import * as fs from 'fs'
|
2018-10-07 02:50:06 +08:00
|
|
|
import * as path from 'path'
|
|
|
|
import * as yaml from 'js-yaml'
|
2022-06-16 03:30:03 +08:00
|
|
|
import { writeFile } from 'atomically'
|
2022-06-16 02:56:47 +08:00
|
|
|
|
2018-10-07 02:50:06 +08:00
|
|
|
|
2023-10-03 14:06:16 +08: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-10-01 03:05:57 +08:00
|
|
|
|
|
|
|
|
2022-05-31 05:27:01 +08:00
|
|
|
export function migrateConfig (): void {
|
|
|
|
if (fs.existsSync(legacyConfigPath) && (
|
|
|
|
!fs.existsSync(configPath) ||
|
|
|
|
fs.statSync(configPath).mtime < fs.statSync(legacyConfigPath).mtime
|
2021-06-30 05:57:04 +08:00
|
|
|
)) {
|
2022-05-31 05:27:01 +08:00
|
|
|
fs.writeFileSync(configPath, fs.readFileSync(legacyConfigPath))
|
2021-06-30 05:57:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 05:27:01 +08:00
|
|
|
export function loadConfig (): any {
|
|
|
|
migrateConfig()
|
2021-06-30 05:57:04 +08:00
|
|
|
|
2022-05-31 05:27:01 +08:00
|
|
|
if (fs.existsSync(configPath)) {
|
|
|
|
return yaml.load(fs.readFileSync(configPath, 'utf8'))
|
2018-10-07 02:50:06 +08:00
|
|
|
} else {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
}
|
2022-05-28 18:35:32 +08:00
|
|
|
|
|
|
|
export async function saveConfig (content: string): Promise<void> {
|
2022-06-16 03:30:03 +08:00
|
|
|
await writeFile(configPath, content, { encoding: 'utf8' })
|
|
|
|
await writeFile(configPath + '.backup', content, { encoding: 'utf8' })
|
2023-10-01 03:30:25 +08:00
|
|
|
}
|