-
Notifications
You must be signed in to change notification settings - Fork 1
/
lformctl.ts
86 lines (79 loc) · 2.89 KB
/
lformctl.ts
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
86
import { govnData, govnDataCLI as gdctl, path } from "./deps.ts";
const $VERSION = "v1.3.7";
const docoptSpec = `
Goverend LHC Form Data Controller (GLForm) ${$VERSION}.
GLForm is a wrapper around the GovSuite GDC Controller which allows a more convenient CLI for
managing LForm Schemas.
Usage:
lformctl lform type <lform-json-src> [--lform-schema-ts=<url>] [--validate] [--overwrite] [--verbose] [--dry-run]
lformctl -h | --help
lformctl --version
Options:
<lform-json-src> LHC Form JSON single local file name or glob (like "*.json" or "**/*.json")
--lform-schema-ts=<url> Where the lform.ts TypeScript schema can be found
--validate Validate the generated TypeScript
--overwrite If the destination file already exists, it's OK to replace it
--verbose Be explicit about what's going on
-h --help Show this screen
--version Show version
`;
export class LhcFormJsonTyper extends govnData.TypicalJsonTyper {
constructor({ "--lform-schema-ts": typeImportURL }: gdctl.docopt.DocOptions) {
super(govnData.defaultTypicalJsonTyperOptions(
(typeImportURL ? typeImportURL.toString() : undefined) ||
"https://denopkg.com/shah/ts-lhncbc-lforms/lform.ts",
"mod.NihLhcForm",
{ instanceName: "form", emittedFileExtn: ".lhc-form.auto.ts" },
));
}
}
// all CLI handlers are required to be async
// deno-lint-ignore require-await
export async function lhcFormJsonTyperCliHandler(
ctx: govnData.CliCmdHandlerContext,
): Promise<true | void> {
const {
"lform": json,
"type": type,
"<lform-json-src>": lformJsonSpec,
"--validate": validate,
} = ctx.cliOptions;
if (json && type && lformJsonSpec) {
const ctl = new govnData.TypicalController(
ctx.calledFromMetaURL,
{
...ctx.tco,
defaultJsonExtn: ".lhc-form.auto.json",
onAfterEmit: (result: govnData.StructuredDataTyperResult): void => {
if (validate && govnData.isFileDestinationResult(result)) {
const destRel = "." + path.SEP + result.destFileNameRel(Deno.cwd());
console.log(
`Using Deno dynamic import("${destRel}") to validate...`,
);
import(destRel);
}
},
},
);
ctl.jsonType({
jsonSrcSpec: lformJsonSpec?.toString() || "*.json",
typer: new LhcFormJsonTyper(ctx.cliOptions),
verbose: ctx.isVerbose || ctx.isDryRun,
overwrite: ctx.shouldOverwrite,
});
return true;
}
}
if (import.meta.main) {
gdctl.CLI<govnData.CliCmdHandlerContext>(
docoptSpec,
[lhcFormJsonTyperCliHandler],
(options: gdctl.docopt.DocOptions): govnData.CliCmdHandlerContext => {
return new govnData.CliCmdHandlerContext(
import.meta.url,
options,
govnData.defaultTypicalControllerOptions({ cli: "NO DATA INSTANCE" }),
);
},
);
}