1 | 'use strict'
|
2 |
|
3 | const fs = require('fs')
|
4 | function configLoc() {
|
5 | const configDir = process.env['_4EVERLINKCONF'] || process.env.HOME + "/.4everlink"
|
6 |
|
7 | const fs = require('fs')
|
8 | const path = require('path')
|
9 | const fileDir = path.resolve(configDir)
|
10 | fs.mkdirSync(fileDir, {recursive: true})
|
11 | return fileDir + "/conf.yaml"
|
12 | }
|
13 | function configExists() {
|
14 | const config = configLoc
|
15 | try{
|
16 | const stats = fs.statSync(config)
|
17 | return stats.isFile
|
18 | }catch(e) {
|
19 | }
|
20 | return false
|
21 | }
|
22 | function initConf(configContent) {
|
23 | const loc = configLoc()
|
24 | try{
|
25 | const stats = fs.statSync(config)
|
26 | if(stats.isDirectory()) {
|
27 | fs.rmdirSync(config, {recursive: true})
|
28 | }else{
|
29 | return stats.isFile
|
30 | }
|
31 | }catch(e) {
|
32 | }
|
33 | const YAML = require('yaml')
|
34 | fs.writeFileSync(loc, YAML.stringify(configContent))
|
35 | return true
|
36 | }
|
37 | function updateConf(configContent) {
|
38 | if(!configExists()) {
|
39 | initConf(configContent)
|
40 | }
|
41 | const loc = configLoc()
|
42 | const stats = fs.statSync(loc)
|
43 | if(stats.isDirectory()) {
|
44 | throw new Error(`conf file: ${loc} is a directory`)
|
45 | }
|
46 | const YAML = require('yaml')
|
47 | fs.writeFileSync(loc, YAML.stringify(configContent))
|
48 | }
|
49 |
|
50 | exports = module.exports = {
|
51 | updateConf, initConf, configExists, configLoc
|
52 | } |
\ | No newline at end of file |