diff --git a/package.json b/package.json index 69425cf..6b7ea8a 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "cookie-parser": "^1.4.6", "cors": "^2.8.5", "express": "^4.18.2", + "minimist": "^1.2.8", "morgan": "^1.10.0", "ws": "^8.13.0" }, diff --git a/src/db.js b/src/db.js index 76d2552..37fed7b 100644 --- a/src/db.js +++ b/src/db.js @@ -2,10 +2,11 @@ import { readFileSync, writeFileSync } from "fs"; import { exit } from "process"; class LocalDB { - #filename = "config/localdb.json"; + #path = null; #data = null; - constructor () { + constructor (path) { try { + this.#path = path; this.load(); this.pveAPI = this.getGlobalConfig().application.pveAPI; this.pveAPIToken = this.getGlobalConfig().application.pveAPIToken; @@ -14,7 +15,7 @@ class LocalDB { this.domain = this.getGlobalConfig().application.domain; } 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); } } @@ -23,14 +24,14 @@ class LocalDB { * Load db from local file system. Reads from file path store in filename. */ 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 () { - 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; diff --git a/src/main.js b/src/main.js index 49ad14d..a6a4f2d 100644 --- a/src/main.js +++ b/src/main.js @@ -9,10 +9,17 @@ import * as pve from "./pve.js"; import * as utils from "./utils.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.pve = pve; global.utils = utils; -global.db = db; +global.db = new db(global.argv.localdb); const app = express(); global.app = app;