2022-08-23 20:08:53 +02:00
|
|
|
import commandLineArgs from "command-line-args";
|
|
|
|
import commandLineUsage from "command-line-usage";
|
|
|
|
import fs from "graceful-fs";
|
|
|
|
import url from "url";
|
|
|
|
import { parse } from "sparkson";
|
2022-08-24 23:47:55 +02:00
|
|
|
import {
|
|
|
|
Config,
|
|
|
|
hasOwnProperty,
|
|
|
|
isLayerConfig,
|
|
|
|
isPlainObject,
|
|
|
|
Url,
|
|
|
|
Version,
|
|
|
|
} from "./types";
|
2020-04-08 17:55:45 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export let config: Config = {} as Config;
|
2022-07-18 17:49:42 +02:00
|
|
|
export let version: Version = "unknown" as Version;
|
2020-04-08 17:55:45 +02:00
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
export function parseCommandLine(): void {
|
2020-04-08 17:55:45 +02:00
|
|
|
const commandLineDefs = [
|
2022-08-23 20:08:53 +02:00
|
|
|
{
|
|
|
|
name: "help",
|
|
|
|
alias: "h",
|
|
|
|
type: Boolean,
|
|
|
|
description: "Show this help",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "config",
|
|
|
|
alias: "c",
|
|
|
|
type: String,
|
|
|
|
description: "Location of config.json",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "version",
|
|
|
|
alias: "v",
|
|
|
|
type: Boolean,
|
|
|
|
description: "Show ffffng version",
|
|
|
|
},
|
2020-04-08 17:55:45 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
let commandLineOptions;
|
|
|
|
try {
|
|
|
|
commandLineOptions = commandLineArgs(commandLineDefs);
|
2022-08-23 20:08:53 +02:00
|
|
|
} catch (error) {
|
|
|
|
if (hasOwnProperty(error, "message")) {
|
|
|
|
console.error(error.message);
|
2021-10-14 17:18:30 +02:00
|
|
|
} else {
|
2022-08-23 20:08:53 +02:00
|
|
|
console.error(error);
|
2021-10-14 17:18:30 +02:00
|
|
|
}
|
2022-08-23 20:08:53 +02:00
|
|
|
console.error("Try '--help' for more information.");
|
2020-04-08 17:55:45 +02:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const packageJsonFile = __dirname + "/../package.json";
|
2020-04-08 17:55:45 +02:00
|
|
|
if (fs.existsSync(packageJsonFile)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
version = JSON.parse(fs.readFileSync(packageJsonFile, "utf8")).version;
|
2020-04-08 17:55:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
function usage() {
|
2022-08-23 20:08:53 +02:00
|
|
|
console.log(
|
|
|
|
commandLineUsage([
|
|
|
|
{
|
|
|
|
header:
|
|
|
|
"ffffng - " +
|
|
|
|
version +
|
|
|
|
" - Freifunk node management form",
|
|
|
|
optionList: commandLineDefs,
|
|
|
|
},
|
|
|
|
])
|
|
|
|
);
|
2020-04-08 17:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (commandLineOptions.help) {
|
|
|
|
usage();
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (commandLineOptions.version) {
|
2022-08-23 20:08:53 +02:00
|
|
|
console.log("ffffng - " + version);
|
2020-04-08 17:55:45 +02:00
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!commandLineOptions.config) {
|
|
|
|
usage();
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const configJSONFile = commandLineOptions.config;
|
|
|
|
let configJSON = {};
|
|
|
|
|
|
|
|
if (fs.existsSync(configJSONFile)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
configJSON = JSON.parse(fs.readFileSync(configJSONFile, "utf8"));
|
2020-04-08 17:55:45 +02:00
|
|
|
} else {
|
2022-08-23 20:08:53 +02:00
|
|
|
console.error("config.json not found: " + configJSONFile);
|
2020-04-08 17:55:45 +02:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
config = parse(Config, configJSON);
|
2020-04-08 17:55:45 +02:00
|
|
|
|
2022-08-24 23:47:55 +02:00
|
|
|
if (!isPlainObject(config.client.coordsSelector.layers)) {
|
|
|
|
console.error(
|
|
|
|
"Error in config.json: client.coordsSelector.layers is not an JSON object."
|
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [id, layerConfig] of Object.entries(
|
|
|
|
config.client.coordsSelector.layers
|
|
|
|
)) {
|
|
|
|
if (!isLayerConfig(layerConfig)) {
|
|
|
|
console.error(
|
|
|
|
`Error in config.json: client.coordsSelector.layers[${id}] is not a valid layer config.`
|
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
function stripTrailingSlash(url: Url): Url {
|
|
|
|
return url.endsWith("/")
|
2022-08-23 20:08:53 +02:00
|
|
|
? (url.substring(0, url.length - 1) as Url)
|
2022-07-18 17:49:42 +02:00
|
|
|
: url;
|
2020-04-08 17:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
config.server.baseUrl = stripTrailingSlash(config.server.baseUrl);
|
|
|
|
config.client.map.mapUrl = stripTrailingSlash(config.client.map.mapUrl);
|
|
|
|
|
|
|
|
config.server.rootPath = url.parse(config.server.baseUrl).pathname || "/";
|
|
|
|
config.client.rootPath = config.server.rootPath;
|
|
|
|
}
|