-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmgctl.ts
391 lines (374 loc) · 12.6 KB
/
mgctl.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import {
docopt,
govnData as gd,
govnDataCLI as gdctl,
govnSvcHealth as gsh,
govnSvcVersion as gsv,
httpServer as http,
nihLhcForms as lf,
oakHelpers as oakH,
path,
} from "./deps.ts";
import * as server from "./server.ts";
import * as mod from "./mod.ts";
export async function determineVersion(importMetaURL: string): Promise<string> {
return gsv.determineVersionFromRepoTag(
importMetaURL,
{ repoIdentity: "medigy/governance" },
);
}
const $VERSION = await determineVersion(import.meta.url);
const docoptSpec = `
Medigy Governance Controller ${$VERSION}.
Usage:
mgctl server [--port=<port>] [--verbose]
mgctl offering-profile type lform <lform-json-src> [--validate] [--mg-mod-ref=<path>] [--mg-mod-deps=<deps.ts>] [--dry-run] [--overwrite] [--verbose] [--gd-mod-ref=<path>] [--gd-mod-deps=<deps.ts>]
mgctl offering-profile inspect lform <lform-json-src>
mgctl institution-profile type lform <lform-json-src> [--validate] [--mg-mod-ref=<path>] [--mg-mod-deps=<deps.ts>] [--dry-run] [--overwrite] [--verbose] [--gd-mod-ref=<path>] [--gd-mod-deps=<deps.ts>]
mgctl quant-eval type lform <lform-json-src> [--validate] [--mg-mod-ref=<path>] [--mg-mod-deps=<deps.ts>] [--dry-run] [--overwrite] [--verbose] [--gd-mod-ref=<path>] [--gd-mod-deps=<deps.ts>]
mgctl quant-eval type facet <lform-json-src> [--validate] [--mg-mod-ref=<path>] [--mg-mod-deps=<deps.ts>] [--dry-run] [--overwrite] [--verbose]
mgctl quant-eval type campaigns <start-path> <lform-json-src> [--mg-mod-ref=<path>] [--mg-mod-deps=<deps.ts>] [--verbose] [--overwrite]
mgctl -h | --help
mgctl --version
Options:
-h --help Show this screen
--version Show version
<lform-json-src> LHC Form JSON source(s) glob (like "**/*.lhc-form.json") or "-" for STDIN
<start-path> Starting path to search for LHC Form JSON sources
--validate Check the generated TypeScript file(s)
--mg-mod-ref=<path> Absolute or relative path of the Medigy Governance *.mod.ts to use
--mg-mod-deps=<deps.ts> Relative path to the deps.ts file which includes reference to '{ medigyGovn }' (ignored if --mg-mod-ref provided)
--gd-mod-ref=<path> Absolute or relative path of the GovSuite GSD module to use
--gd-mod-deps=<deps.ts> Relative path to the deps.ts file for access GovSuite GSD 'govnData' module (ignored if --gd-mod-ref provided)
--persist-on-error Saves the generated *.auto.ts file on error
--overwrite Replace files in case they already exist
--dry-run Only show what will be done, without executing
--verbose Be explicit about what's going on
`;
export function govnDataModuleImportDirective(
{ "--gd-mod-ref": moduleRef, "--gd-mod-deps": depsTs }: docopt.DocOptions,
): {
govnDataModuleRef: string;
govnDataModuleImportDirective: string;
govnDataModuleTypeImportDirective: string;
} {
return {
govnDataModuleRef: moduleRef
? moduleRef.toString()
: "https://denopkg.com/gov-suite/governed-structured-data/mod.ts",
govnDataModuleImportDirective: moduleRef
? `import * as govnData from "${moduleRef}";`
: `import { govnData } from "${
depsTs ? depsTs.toString() : "./deps.ts"
}";`,
govnDataModuleTypeImportDirective: moduleRef
? `import type * as govnData from "${moduleRef}";`
: `import type { govnData } from "${
depsTs ? depsTs.toString() : "./deps.ts"
}";`,
};
}
export function medigyGovnModuleRef(
{ "--mg-mod-ref": moduleRef, "--mg-mod-deps": depsTs }: docopt.DocOptions,
): {
medigyGovnModuleImportDirective: string;
medigyGovnModuleTypeImportDirective: string;
medigyGovnModuleRef: string;
} {
return {
medigyGovnModuleRef: moduleRef
? moduleRef.toString()
: "https://denopkg.com/medigy/governance/mod.ts",
medigyGovnModuleImportDirective: moduleRef
? `import * as medigyGovn from "${moduleRef}";`
: `import { medigyGovn } from "${
depsTs ? depsTs.toString() : "./deps.ts"
}";`,
medigyGovnModuleTypeImportDirective: moduleRef
? `import type * as medigyGovn from "${moduleRef}";`
: `import type { medigyGovn } from "${
depsTs ? depsTs.toString() : "./deps.ts"
}";`,
};
}
export function validateEmitted(result: gd.StructuredDataTyperResult): void {
if (gd.isFileDestinationResult(result)) {
const destRel = "." + path.SEP + result.destFileNameRel(Deno.cwd());
console.log(
`Using Deno dynamic import("${destRel}") to validate...`,
);
import(destRel);
}
}
export async function typeLhcFormJSON(
ctx: gd.CliCmdHandlerContext,
typer: gd.TypicalJsonTyper,
): Promise<void> {
const {
"<lform-json-src>": lformJsonSpec,
"--validate": validate,
} = ctx.cliOptions;
const ctl = new gd.TypicalController(
ctx.calledFromMetaURL,
{
...ctx.tco,
defaultJsonExtn: ".lch-form.auto.json",
onAfterEmit: (result: gd.StructuredDataTyperResult): void => {
if (validate && !ctx.isDryRun) validateEmitted(result);
},
},
);
ctl.jsonType({
jsonSrcSpec: lformJsonSpec?.toString() || "**/*.json",
typer: typer,
verbose: ctx.isVerbose || ctx.isDryRun,
overwrite: ctx.shouldOverwrite,
});
}
export async function inspectLhcFormJSON<F extends lf.NihLhcForm>(
ctx: gd.CliCmdHandlerContext,
inspector: {
inspect: (f: F) => Promise<lf.LhcFormInspectionDiagnostics<F>>;
inspectionDiagnosticsJSON?: (
diags: lf.LhcFormInspectionDiagnostics<F>,
) => string;
},
): Promise<void> {
const { "<lform-json-src>": lformJsonSpec } = ctx.cliOptions;
if (lformJsonSpec == "-") {
const supplier = new gd.BufferSupplier(
Deno.readAllSync(Deno.stdin),
gd.defaultBufferSupplierOptions("STDIN"),
);
supplier.forEach({
onEntry: async (
ctx: gd.UntypedDataSupplierEntryContext,
): Promise<void> => {
if (gd.isJsonSupplierEntryContext(ctx)) {
// TODO: should we "type" this first and not just cast it?
const lform: F = ctx.jsonValue as F;
const diags = await inspector.inspect(lform);
console.log(
inspector.inspectionDiagnosticsJSON
? inspector.inspectionDiagnosticsJSON(diags)
: JSON.stringify(diags, undefined, 2),
);
}
},
});
} else {
const supplier = new gd.FileSystemGlobSupplier(
lformJsonSpec?.toString() || "**/*.json",
);
supplier.forEach({
onEntry: async (
ctx: gd.UntypedDataSupplierEntryContext,
): Promise<void> => {
if (
gd.isJsonSupplierEntryContext(ctx) && gd.isGlobWalkEntryContext(ctx)
) {
// TODO: should we "type" this first and not just cast it?
const lform: F = ctx.jsonValue as F;
const diags = await inspector.inspect(lform);
Deno.writeTextFileSync(
`${ctx.fileNameWithoutExtn}.inspection-log.json`,
inspector.inspectionDiagnosticsJSON
? inspector.inspectionDiagnosticsJSON(diags)
: JSON.stringify(diags, undefined, 2),
);
}
},
});
}
}
export async function offeringProfileLhcFormJsonTyperCliHandler(
ctx: gd.CliCmdHandlerContext,
): Promise<true | void> {
const {
"offering-profile": offeringProfile,
"type": type,
"lform": lform,
"<lform-json-src>": lformJsonSpec,
} = ctx.cliOptions;
if (offeringProfile && type && lform && lformJsonSpec) {
typeLhcFormJSON(
ctx,
new mod.offerProfile.lf.OfferingProfileLhcFormJsonTyper({
...govnDataModuleImportDirective(ctx.cliOptions),
...medigyGovnModuleRef(ctx.cliOptions),
}),
);
return true;
}
}
export async function offeringProfileLhcFormInspectCliHandler(
ctx: gd.CliCmdHandlerContext,
): Promise<true | void> {
const {
"offering-profile": offeringProfile,
"inspect": inspect,
"lform": lform,
"<lform-json-src>": lformJsonSpec,
} = ctx.cliOptions;
if (offeringProfile && inspect && lform && lformJsonSpec) {
inspectLhcFormJSON<mod.offerProfile.lf.OfferingProfileLhcForm>(
ctx,
new mod.offerProfile.lf.OfferingProfileValidator(),
);
return true;
}
}
export async function institutionProfileLhcFormJsonTyperCliHandler(
ctx: gd.CliCmdHandlerContext,
): Promise<true | void> {
const {
"institution-profile": institutionProfile,
"type": type,
"lform": lform,
"<lform-json-src>": lformJsonSpec,
} = ctx.cliOptions;
if (institutionProfile && type && lform && lformJsonSpec) {
typeLhcFormJSON(
ctx,
new mod.instiProfile.lf.InstitutionProfileLhcFormJsonTyper({
...govnDataModuleImportDirective(ctx.cliOptions),
...medigyGovnModuleRef(ctx.cliOptions),
}),
);
return true;
}
}
export async function quantEvalFacetLhcFormJsonTyperCliHandler(
ctx: gd.CliCmdHandlerContext,
): Promise<true | void> {
const {
"quant-eval": quantEval,
"type": type,
"lform": lform,
"<lform-json-src>": lformJsonSpec,
} = ctx.cliOptions;
if (quantEval && type && lform && lformJsonSpec) {
typeLhcFormJSON(
ctx,
new mod.quantEval.lf.QuantEvalFacetLhcFormJsonTyper({
...govnDataModuleImportDirective(ctx.cliOptions),
...medigyGovnModuleRef(ctx.cliOptions),
}),
);
return true;
}
}
export async function quantEvalFacetTyperCliHandler(
ctx: gd.CliCmdHandlerContext,
): Promise<true | void> {
const {
"quant-eval": quantEval,
"type": type,
"facet": facet,
"<lform-json-src>": lformJsonSpec,
"--validate": validate,
} = ctx.cliOptions;
if (quantEval && type && facet && lformJsonSpec) {
const emitter = new gd.TypedDataFileSystemEmitter(
[
new mod.quantEval.EvaluationFacetTyper(
medigyGovnModuleRef(ctx.cliOptions),
),
],
);
emitter.emitTypedData({
udSupplier: new gd.FileSystemGlobSupplier(lformJsonSpec.toString()),
shouldEmit: gd.shouldEmitCheckOverwriteAndNotDryRun(
ctx.shouldOverwrite,
ctx.isDryRun,
ctx.isVerbose,
),
onAfterEmit: (result: gd.StructuredDataTyperResult): void => {
if (gd.isFileDestinationResult(result)) {
const destRel = "." + path.SEP + result.destFileNameRel(Deno.cwd());
if (ctx.isVerbose && !ctx.isDryRun) {
console.log(destRel);
}
if (validate && !ctx.isDryRun) {
validateEmitted(result);
}
}
},
});
return true;
}
}
export async function quantEvalCampaignsTyperCliHandler(
ctx: gd.CliCmdHandlerContext,
): Promise<true | void> {
const {
"quant-eval": quantEval,
"type": type,
"campaigns": campaigns,
"<start-path>": startPathSpec,
"<lform-json-src>": lhcFormJsonSrcSpec,
} = ctx.cliOptions;
if (quantEval && type && campaigns && startPathSpec && lhcFormJsonSrcSpec) {
mod.quantEval.EvaluationFacetCampaignsTyper({
...medigyGovnModuleRef(ctx.cliOptions),
startPath: startPathSpec.toString(),
lhcFormJsonSrcSpec: lhcFormJsonSrcSpec.toString(),
overwrite: ctx.shouldOverwrite,
verbose: ctx.isVerbose,
});
return true;
}
}
export async function httpServiceHandler(
ctx: gd.CliCmdHandlerContext,
): Promise<true | void> {
const {
"server": isServer,
"--port": portSpec,
} = ctx.cliOptions;
if (isServer) {
const port = typeof portSpec === "number" ? portSpec : 8159;
/* TODO: Pass the proper parameters to the server.httpServiceRouter() function
*/
const app = server.httpServer({
port: port,
router: server.httpServiceRouter(),
});
oakH.registerHealthRoute(app, {
serviceVersion: () => {
return $VERSION;
},
/* TODO: IF more details to be checked in the end point need to give that after the releaseID */
endpoint: async () => {
const hs = gsh.healthyService({
version: "1",
releaseID: $VERSION,
});
return gsh.healthStatusEndpoint(hs);
},
});
await app.listen({ port: port });
return true;
}
}
if (import.meta.main) {
gdctl.CLI<gd.CliCmdHandlerContext>(
docoptSpec,
[
offeringProfileLhcFormJsonTyperCliHandler,
offeringProfileLhcFormInspectCliHandler,
institutionProfileLhcFormJsonTyperCliHandler,
quantEvalFacetLhcFormJsonTyperCliHandler,
quantEvalFacetTyperCliHandler,
quantEvalCampaignsTyperCliHandler,
httpServiceHandler,
],
(options: docopt.DocOptions): gd.CliCmdHandlerContext => {
return new gd.CliCmdHandlerContext(
import.meta.url,
options,
gd.defaultTypicalControllerOptions({ cli: "NO DATA INSTANCE" }),
);
},
);
}