-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharg-parse.ts
More file actions
86 lines (64 loc) · 2.4 KB
/
arg-parse.ts
File metadata and controls
86 lines (64 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Filename: arg-parse.ts
* Author: simshadows <[email protected]>
* License: GNU Affero General Public License v3 (AGPL-3.0)
*/
import path from "path";
import {Command} from "commander";
// The commander package seems poorly typed, so I'll need to make explicit checks.
// TODO: Drill into the typing issues later.
function ensureStr(obj: any, argName: string): string {
if (typeof obj === "string") return obj;
throw new Error(`'${argName}' argument is not a string.`);
}
/********************************************************************/
export interface ArgValues {
specFilePath: string;
sourceOutputDirPath: string;
appOutputDirPath: string;
allowOverwriteOutput: boolean;
startDevServer: boolean;
}
const program = new Command();
program.name("solves");
const cwd = process.cwd();
program.requiredOption(
"-s, --spec <path>",
"Specification file input.",
);
program.option(
"--source-out <path>",
"Output directory for the generated source code.",
"../../generated-source", // TODO: How to make this relative to invocation?
);
program.option(
"-o, --app-out <path>",
"Output directory for the generated static web app.",
"../../generated-web-app", // TODO: How to make this relative to invocation?
);
program.option(
"-f, --force",
"Allows overwriting of output directories (--source-out and --app-out).",
);
program.option(
"--start-dev-server",
"Instead of emitting a generated static web app (via. -o/--app-out), start the Webpack dev server. Using --start-dev-server will cause -o/--app-out to be ignored.",
);
program.parse();
const rawOptions = program.opts();
const typedOptions: ArgValues = {
specFilePath: path.join(cwd, ensureStr(rawOptions["spec"], "spec")),
sourceOutputDirPath: path.join(cwd, ensureStr(rawOptions["sourceOut"], "source-out")),
appOutputDirPath: path.join(cwd, ensureStr(rawOptions["appOut"], "app-out")),
allowOverwriteOutput: (rawOptions["force"] === true),
startDevServer: (rawOptions["startDevServer"] === true),
};
// Some really crude validation for this prototype, for safety
if (typedOptions.sourceOutputDirPath === "") throw "can't be empty";
if (typedOptions.appOutputDirPath === "") throw "can't be empty";
export function getCLIArgs(): Readonly<ArgValues> {
return typedOptions;
}
console.info("Arguments:");
console.info(typedOptions);
console.info();