budibase/packages/cli/src/structures/ConfigManager.js

51 lines
982 B
JavaScript
Raw Normal View History

2021-03-30 11:50:42 +02:00
const fs = require("fs")
const path = require("path")
const os = require("os")
const { error } = require("../utils")
class ConfigManager {
constructor() {
this.path = path.join(os.homedir(), ".budibase.json")
2021-03-30 12:50:49 +02:00
if (!fs.existsSync(this.path)) {
2021-03-30 11:50:42 +02:00
fs.writeFileSync(this.path, "{}")
2021-03-30 12:50:49 +02:00
}
2021-03-30 11:50:42 +02:00
}
get config() {
try {
return JSON.parse(fs.readFileSync(this.path, "utf8"))
} catch (err) {
2021-03-30 12:50:49 +02:00
console.log(
error(
"Error parsing configuration file. Please check your .budibase.json is valid."
)
)
return {}
2021-03-30 11:50:42 +02:00
}
}
set config(json) {
fs.writeFileSync(this.path, JSON.stringify(json))
}
getValue(key) {
return this.config[key]
}
setValue(key, value) {
const updated = {
...this.config,
2021-03-30 12:50:49 +02:00
[key]: value,
2021-03-30 11:50:42 +02:00
}
this.config = updated
}
removeKey(key) {
const updated = { ...this.config }
delete updated[key]
this.config = updated
}
}
2021-03-30 12:50:49 +02:00
module.exports = ConfigManager