config.ts 1.1 KB

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