From 16ca2ee8b136a22e4309cd154a75169006ac4e4b Mon Sep 17 00:00:00 2001 From: Wolvan Date: Wed, 29 Dec 2021 15:25:43 +0100 Subject: [PATCH] Fix relative pathing issues Running from a subdirectory makes the entire script a bit cumbersome to wrap around when it comes to relative paths. They should now all work properly though. Paths that are relative to the Project Root now work as expected (eg. package.json path) and other paths like the data and config path are based on the current execution directory. --- src/backend.ts | 3 ++- src/config-loader.ts | 26 ++++++++++++++------------ src/main.ts | 2 +- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/backend.ts b/src/backend.ts index f4b51b0..7a8677f 100644 --- a/src/backend.ts +++ b/src/backend.ts @@ -3,6 +3,7 @@ import { Router } from "express"; import persist from "node-persist"; import { program } from "commander"; +import { resolve } from "path"; type Poll = { id: string, @@ -27,7 +28,7 @@ function randomString(length = 10, charset = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKL export default async function init(router: Router): Promise { const polls = await persist.create({ - dir: program.opts().dataDirectory + dir: resolve(process.cwd(), program.opts().dataDirectory) }); await polls.init(); diff --git a/src/config-loader.ts b/src/config-loader.ts index 71e799d..4896b5e 100644 --- a/src/config-loader.ts +++ b/src/config-loader.ts @@ -8,13 +8,6 @@ import replaceArguments from "./config-handlers"; type CommanderOption = [flags: string, description: string, defaultValueOrHandler?: (((...args:any[]) => any) | any), defaultValue?: any]; -const configCommander = new Command(); -configCommander - .allowUnknownOption() - .option("-c, --config-path ", "Path to the configuration file to load", "../config") - .option("-h, --help", "Don't. Just don't.") - .parse(process.argv); - async function loadPackageJSONVersion(): Promise { try { const packageJSON = await fs.readFile(resolve(__dirname, "../package.json"), "utf8"); @@ -27,11 +20,21 @@ async function loadPackageJSONVersion(): Promise { } } async function loadConfig(options: CommanderOption[] = [], homedirConfigFilename: (string | null) = null): Promise { + const version = await loadPackageJSONVersion(); + + const configCommander = new Command(); + configCommander + .allowUnknownOption() + .version(version) + .option("-c, --config-path ", "Path to the configuration file to load", "./config") + .option("-h, --help", "Don't. Just don't.") + .parse(process.argv); + const config = await (async () => { try { const opts = configCommander.opts(); - const config = await import(opts.configPath || "../config"); - console.log(`Loaded config from ${opts.configPath || "../config"}`); + const config = await import(resolve(opts.configPath || "./config")); + console.log(`Loaded config from ${opts.configPath || "./config"}`); return config; } catch (error) { try { @@ -41,8 +44,8 @@ async function loadConfig(options: CommanderOption[] = [], homedirConfigFilename return config; } catch (error) { try { - const config = await import(resolve(__dirname, "config")); - console.log(`Loaded config from ${resolve(__dirname, "config")}`); + const config = await import(resolve(__dirname, "..", "config")); + console.log(`Loaded config from ${resolve(__dirname, "..", "config")}`); return config; } catch (error) { return {}; @@ -50,7 +53,6 @@ async function loadConfig(options: CommanderOption[] = [], homedirConfigFilename } } })(); - const version = await loadPackageJSONVersion(); program .version(version) diff --git a/src/main.ts b/src/main.ts index 69c419c..0bf0811 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,7 +11,7 @@ async function main(): Promise { await loadConfig([ ["--no-frontend", "Do not start the frontend server"], ["--no-backend", "Do not start the backend server"], - ["-d, --data-directory ", "Path to the data directory", "../data"], + ["-d, --data-directory ", "Path to the data directory", "./data"], ["-p, --port ", "Port to listen on", (port: any) => parseInt(port), 6969], ["--backend-base-url ", "Base URL for the backend server", null], ], ".poll-horse-config");