tabby/app/lib/config.ts

34 lines
1,014 B
TypeScript
Raw Normal View History

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