This repository has been archived by the owner on Feb 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathConfig.js
70 lines (60 loc) · 1.46 KB
/
Config.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const fs = require('fs')
const path = require('path')
const createRenderer = require('./electron-shell')
const { getUserDataPath } = require('./utils/main/util')
const INITIAL_CONFIG = {
registries: [],
plugins: [],
apps: [],
settings: {},
flags: {}
}
class Config {
constructor(filePath) {
this.filePath = filePath
if (!fs.existsSync(filePath)) {
this.reset()
}
}
reset(config) {
fs.writeFileSync(this.filePath, JSON.stringify(config || INITIAL_CONFIG))
}
setConfig(newConfig) {
if (newConfig === null) {
return
}
if (newConfig.constructor.name !== 'Object') {
console.log('config must be object literal')
return
}
fs.writeFileSync(this.filePath, JSON.stringify(newConfig, null, 2))
}
setItem(key, value) {
const config = this.getItem()
config[key] = value
this.setConfig(config)
}
getItem(key, default_val) {
const config = JSON.parse(fs.readFileSync(this.filePath))
if (!key) {
return default_val || config
}
return config[key]
}
}
const UserConfig = new Config(path.join(getUserDataPath(), 'config.json'))
const registerGlobalUserConfig = () => {
global.UserConfig = UserConfig
}
const startConfigEditor = () => {
const win = createRenderer(`file://${__dirname}/ui/json-editor.html`, {
width: 800,
height: 600
})
win.removeMenu()
}
module.exports = {
startConfigEditor,
registerGlobalUserConfig,
getUserConfig: () => UserConfig
}