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

50 lines
979 B
TypeScript
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")
export class ConfigManager {
path: string
2021-03-30 11:50:42 +02:00
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: any) {
2021-03-30 11:50:42 +02:00
fs.writeFileSync(this.path, JSON.stringify(json))
}
getValue(key: string) {
2021-03-30 11:50:42 +02:00
return this.config[key]
}
setValue(key: string, value: any) {
this.config = {
2021-03-30 11:50:42 +02:00
...this.config,
2021-03-30 12:50:49 +02:00
[key]: value,
2021-03-30 11:50:42 +02:00
}
}
removeKey(key: string) {
2021-03-30 11:50:42 +02:00
const updated = { ...this.config }
delete updated[key]
this.config = updated
}
}