tabby/app/lib/config.ts

34 lines
1014 B
TypeScript
Raw Permalink Normal View History

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'
import { writeFile } from 'atomically'
2018-10-07 02:50:06 +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')
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 {}
}
}
export async function saveConfig (content: string): Promise<void> {
await writeFile(configPath, content, { encoding: 'utf8' })
await writeFile(configPath + '.backup', content, { encoding: 'utf8' })
}