libs/config/_utils.js (view raw)
1const fs = require("fs");
2const path = require("path");
3const homedir = require("os").homedir();
4const CONFIG_PATH = path.join(homedir, ".dbee");
5
6const getConfig = () => {
7 createConfigIfNotExists();
8 const rawContent = fs.readFileSync(CONFIG_PATH, "utf8");
9 return JSON.parse(rawContent);
10};
11
12const storeConfig = config => {
13 createConfigIfNotExists();
14 fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2));
15};
16
17const createConfigIfNotExists = () => {
18 if (!fs.existsSync(CONFIG_PATH)) {
19 try {
20 fs.writeFileSync(CONFIG_PATH, "{}", { flag: "w+" });
21 console.log(`Config file created at ${CONFIG_PATH}`);
22 } catch (error) {
23 console.error(error);
24 }
25 }
26};
27
28module.exports = { CONFIG_PATH, getConfig, storeConfig };