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.
This commit is contained in:
Wolvan 2021-12-29 15:25:43 +01:00
parent 8a304c763b
commit 16ca2ee8b1
3 changed files with 17 additions and 14 deletions

View file

@ -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<void> {
const polls = await persist.create({
dir: program.opts().dataDirectory
dir: resolve(process.cwd(), program.opts().dataDirectory)
});
await polls.init();

View file

@ -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>", "Path to the configuration file to load", "../config")
.option("-h, --help", "Don't. Just don't.")
.parse(process.argv);
async function loadPackageJSONVersion(): Promise<string> {
try {
const packageJSON = await fs.readFile(resolve(__dirname, "../package.json"), "utf8");
@ -27,11 +20,21 @@ async function loadPackageJSONVersion(): Promise<string> {
}
}
async function loadConfig(options: CommanderOption[] = [], homedirConfigFilename: (string | null) = null): Promise<void> {
const version = await loadPackageJSONVersion();
const configCommander = new Command();
configCommander
.allowUnknownOption()
.version(version)
.option("-c, --config-path <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)

View file

@ -11,7 +11,7 @@ async function main(): Promise<void> {
await loadConfig([
["--no-frontend", "Do not start the frontend server"],
["--no-backend", "Do not start the backend server"],
["-d, --data-directory <path>", "Path to the data directory", "../data"],
["-d, --data-directory <path>", "Path to the data directory", "./data"],
["-p, --port <port>", "Port to listen on", (port: any) => parseInt(port), 6969],
["--backend-base-url <url>", "Base URL for the backend server", null],
], ".poll-horse-config");