mirror of
https://github.com/Wolvan/poll.horse.git
synced 2025-02-16 17:54:22 +01:00
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:
parent
8a304c763b
commit
16ca2ee8b1
3 changed files with 17 additions and 14 deletions
|
@ -3,6 +3,7 @@
|
||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
import persist from "node-persist";
|
import persist from "node-persist";
|
||||||
import { program } from "commander";
|
import { program } from "commander";
|
||||||
|
import { resolve } from "path";
|
||||||
|
|
||||||
type Poll = {
|
type Poll = {
|
||||||
id: string,
|
id: string,
|
||||||
|
@ -27,7 +28,7 @@ function randomString(length = 10, charset = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKL
|
||||||
|
|
||||||
export default async function init(router: Router): Promise<void> {
|
export default async function init(router: Router): Promise<void> {
|
||||||
const polls = await persist.create({
|
const polls = await persist.create({
|
||||||
dir: program.opts().dataDirectory
|
dir: resolve(process.cwd(), program.opts().dataDirectory)
|
||||||
});
|
});
|
||||||
await polls.init();
|
await polls.init();
|
||||||
|
|
||||||
|
|
|
@ -8,13 +8,6 @@ import replaceArguments from "./config-handlers";
|
||||||
|
|
||||||
type CommanderOption = [flags: string, description: string, defaultValueOrHandler?: (((...args:any[]) => any) | any), defaultValue?: any];
|
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> {
|
async function loadPackageJSONVersion(): Promise<string> {
|
||||||
try {
|
try {
|
||||||
const packageJSON = await fs.readFile(resolve(__dirname, "../package.json"), "utf8");
|
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> {
|
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 () => {
|
const config = await (async () => {
|
||||||
try {
|
try {
|
||||||
const opts = configCommander.opts();
|
const opts = configCommander.opts();
|
||||||
const config = await import(opts.configPath || "../config");
|
const config = await import(resolve(opts.configPath || "./config"));
|
||||||
console.log(`Loaded config from ${opts.configPath || "../config"}`);
|
console.log(`Loaded config from ${opts.configPath || "./config"}`);
|
||||||
return config;
|
return config;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
try {
|
try {
|
||||||
|
@ -41,8 +44,8 @@ async function loadConfig(options: CommanderOption[] = [], homedirConfigFilename
|
||||||
return config;
|
return config;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
try {
|
try {
|
||||||
const config = await import(resolve(__dirname, "config"));
|
const config = await import(resolve(__dirname, "..", "config"));
|
||||||
console.log(`Loaded config from ${resolve(__dirname, "config")}`);
|
console.log(`Loaded config from ${resolve(__dirname, "..", "config")}`);
|
||||||
return config;
|
return config;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {};
|
return {};
|
||||||
|
@ -50,7 +53,6 @@ async function loadConfig(options: CommanderOption[] = [], homedirConfigFilename
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
const version = await loadPackageJSONVersion();
|
|
||||||
|
|
||||||
program
|
program
|
||||||
.version(version)
|
.version(version)
|
||||||
|
|
|
@ -11,7 +11,7 @@ async function main(): Promise<void> {
|
||||||
await loadConfig([
|
await loadConfig([
|
||||||
["--no-frontend", "Do not start the frontend server"],
|
["--no-frontend", "Do not start the frontend server"],
|
||||||
["--no-backend", "Do not start the backend 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],
|
["-p, --port <port>", "Port to listen on", (port: any) => parseInt(port), 6969],
|
||||||
["--backend-base-url <url>", "Base URL for the backend server", null],
|
["--backend-base-url <url>", "Base URL for the backend server", null],
|
||||||
], ".poll-horse-config");
|
], ".poll-horse-config");
|
||||||
|
|
Loading…
Reference in a new issue