add cli arg for localdb path

This commit is contained in:
Arthur Lu 2023-08-03 19:31:11 +00:00
parent ab3bf15efe
commit ea94d31b0c
3 changed files with 16 additions and 7 deletions

View File

@ -11,6 +11,7 @@
"cookie-parser": "^1.4.6", "cookie-parser": "^1.4.6",
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.18.2", "express": "^4.18.2",
"minimist": "^1.2.8",
"morgan": "^1.10.0", "morgan": "^1.10.0",
"ws": "^8.13.0" "ws": "^8.13.0"
}, },

View File

@ -2,10 +2,11 @@ import { readFileSync, writeFileSync } from "fs";
import { exit } from "process"; import { exit } from "process";
class LocalDB { class LocalDB {
#filename = "config/localdb.json"; #path = null;
#data = null; #data = null;
constructor () { constructor (path) {
try { try {
this.#path = path;
this.load(); this.load();
this.pveAPI = this.getGlobalConfig().application.pveAPI; this.pveAPI = this.getGlobalConfig().application.pveAPI;
this.pveAPIToken = this.getGlobalConfig().application.pveAPIToken; this.pveAPIToken = this.getGlobalConfig().application.pveAPIToken;
@ -14,7 +15,7 @@ class LocalDB {
this.domain = this.getGlobalConfig().application.domain; this.domain = this.getGlobalConfig().application.domain;
} }
catch { catch {
console.log("Error: localdb.json was not found. Please follow the directions in the README to initialize localdb.json."); console.log(`Error: ${path} was not found. Please follow the directions in the README to initialize localdb.json.`);
exit(1); exit(1);
} }
} }
@ -23,14 +24,14 @@ class LocalDB {
* Load db from local file system. Reads from file path store in filename. * Load db from local file system. Reads from file path store in filename.
*/ */
load () { load () {
this.#data = JSON.parse(readFileSync(this.#filename)); this.#data = JSON.parse(readFileSync(this.#path));
} }
/** /**
* Save db to local file system. Saves to file path stored in filename. * Save db to local file system. Saves to file path stored in filename.
*/ */
save () { save () {
writeFileSync(this.#filename, JSON.stringify(this.#data)); writeFileSync(this.#path, JSON.stringify(this.#data));
} }
/** /**
@ -51,4 +52,4 @@ class LocalDB {
} }
} }
export default new LocalDB(); export default LocalDB;

View File

@ -9,10 +9,17 @@ import * as pve from "./pve.js";
import * as utils from "./utils.js"; import * as utils from "./utils.js";
import db from "./db.js"; import db from "./db.js";
import parseArgs from "minimist";
global.argv = parseArgs(process.argv.slice(2), {
default: {
localdb: "config/localdb.json"
}
});
global.api = api; global.api = api;
global.pve = pve; global.pve = pve;
global.utils = utils; global.utils = utils;
global.db = db; global.db = new db(global.argv.localdb);
const app = express(); const app = express();
global.app = app; global.app = app;