Make layers config typesafe.

This commit is contained in:
baldo 2022-08-24 23:47:55 +02:00
commit 59ef8256e6
2 changed files with 72 additions and 3 deletions

View file

@ -3,7 +3,15 @@ import commandLineUsage from "command-line-usage";
import fs from "graceful-fs";
import url from "url";
import { parse } from "sparkson";
import { Config, hasOwnProperty, Url, Version } from "./types";
import {
Config,
hasOwnProperty,
isLayerConfig,
isPlainObject,
isString,
Url,
Version,
} from "./types";
export let config: Config = {} as Config;
export let version: Version = "unknown" as Version;
@ -89,6 +97,24 @@ export function parseCommandLine(): void {
config = parse(Config, configJSON);
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);
}
}
function stripTrailingSlash(url: Url): Url {
return url.endsWith("/")
? (url.substring(0, url.length - 1) as Url)