2021-03-30 11:50:42 +02:00
|
|
|
const fs = require("fs")
|
|
|
|
const path = require("path")
|
|
|
|
const os = require("os")
|
|
|
|
const { error } = require("../utils")
|
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
set config(json: any) {
|
2021-03-30 11:50:42 +02:00
|
|
|
fs.writeFileSync(this.path, JSON.stringify(json))
|
|
|
|
}
|
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
getValue(key: string) {
|
2021-03-30 11:50:42 +02:00
|
|
|
return this.config[key]
|
|
|
|
}
|
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
setValue(key: string, value: any) {
|
2022-09-28 20:11:22 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 19:21:45 +01:00
|
|
|
removeKey(key: string) {
|
2021-03-30 11:50:42 +02:00
|
|
|
const updated = { ...this.config }
|
|
|
|
delete updated[key]
|
|
|
|
this.config = updated
|
|
|
|
}
|
|
|
|
}
|