config.ts 1014 B

123456789101112131415161718192021222324252627282930313233
  1. import * as fs from 'fs'
  2. import * as path from 'path'
  3. import * as yaml from 'js-yaml'
  4. import { writeFile } from 'atomically'
  5. export const configPath = path.join(process.env.TABBY_CONFIG_DIRECTORY!, 'config.yaml')
  6. const legacyConfigPath = path.join(process.env.TABBY_CONFIG_DIRECTORY!, '../terminus', 'config.yaml')
  7. export function migrateConfig (): void {
  8. if (fs.existsSync(legacyConfigPath) && (
  9. !fs.existsSync(configPath) ||
  10. fs.statSync(configPath).mtime < fs.statSync(legacyConfigPath).mtime
  11. )) {
  12. fs.writeFileSync(configPath, fs.readFileSync(legacyConfigPath))
  13. }
  14. }
  15. export function loadConfig (): any {
  16. migrateConfig()
  17. if (fs.existsSync(configPath)) {
  18. return yaml.load(fs.readFileSync(configPath, 'utf8'))
  19. } else {
  20. return {}
  21. }
  22. }
  23. export async function saveConfig (content: string): Promise<void> {
  24. await writeFile(configPath, content, { encoding: 'utf8' })
  25. await writeFile(configPath + '.backup', content, { encoding: 'utf8' })
  26. }