All files config.js

0% Statements 0/10
0% Branches 0/4
0% Functions 0/5
0% Lines 0/10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38                                                                           
// @flow
 
import fs from 'fs'
import path from 'path'
import os from 'os'
 
const ERN_RC_GLOBAL_FILE_PATH = path.join(os.homedir(), '.ern', '.ernrc')
const ERN_RC_LOCAL_FILE_PATH = path.join(process.cwd(), '.ernrc')
 
export class ErnConfig {
  get obj () : Object {
    return JSON.parse(fs.readFileSync(this.ernRcFilePath, 'utf-8'))
  }
 
  get ernRcFilePath () : string {
    return fs.existsSync(ERN_RC_LOCAL_FILE_PATH)
            ? ERN_RC_LOCAL_FILE_PATH
            : ERN_RC_GLOBAL_FILE_PATH
  }
 
  getValue (key: string, defaultValue: any) : any {
    return this.obj[key] !== undefined ? this.obj[key] : defaultValue
  }
 
  setValue (key: string, value: any) {
    let c = this.obj
    c[key] = value
    fs.writeFileSync(this.ernRcFilePath, JSON.stringify(c, null, 2))
  }
 
  writeConfig (obj: Object) {
    fs.writeFileSync(this.ernRcFilePath, JSON.stringify(obj, null, 2))
  }
}
const config = new ErnConfig()
 
export default config