This repository has been archived by the owner on Oct 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
422 lines (380 loc) · 17.1 KB
/
app.js
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const bunyan = require('bunyan');
const program = require('commander');
const bps = require('@ojolabs/bunyan-prettystream');
const { sanityCheckModules } = require('shr-models');
const shrTI = require('shr-text-import');
const shrEx = require('shr-expand');
const shrJE = require('shr-json-export');
const shrJSE = require('shr-json-schema-export');
const shrEE = require('shr-es6-export');
const shrFE = require('shr-fhir-export');
const shrJDE = require('shr-json-javadoc');
const shrAE = require('shr-adl-bmm-export');
const LogCounter = require('./logcounter');
const SpecificationsFilter = require('./filter');
/* eslint-disable no-console */
sanityCheckModules({shrTI, shrEx, shrJE, shrJSE, shrEE, shrFE });
// Record the time so we can print elapsed time
const hrstart = process.hrtime();
function collect(val, list) {
list.push(val);
return list;
}
let input;
program
.usage('<path-to-shr-defs> [options]')
.option('-l, --log-level <level>', 'the console log level <fatal,error,warn,info,debug,trace> (default: info)', /^(fatal|error|warn|info|debug|trace)$/i, 'info')
.option('-m, --log-mode <mode>', 'the console log mode <short,long,json,off> (default: short)', /^(short|long|json|off)$/i, 'short')
.option('-s, --skip <feature>', 'skip an export feature <fhir,json,cimcore,json-schema,es6,model-doc,all>', collect, [])
.option('-a, --adl', 'run the adl exporter (default: false)')
.option('-o, --out <out>', `the path to the output folder (default: ${path.join('.', 'out')})`, path.join('.', 'out'))
.option('-c, --config <config>', 'the name of the config file (default: config.json)', 'config.json')
.option('-d, --duplicate', 'show duplicate error messages (default: false)')
.option('-i, --import-cimcore', 'import CIMCORE files instead of CIMPL (default: false)')
.arguments('<path-to-shr-defs>')
.action(function (pathToShrDefs) {
input = pathToShrDefs;
})
.parse(process.argv);
// Check that input folder is specified
if (typeof input === 'undefined') {
console.error('\x1b[31m','Missing path to SHR definition folder or file','\x1b[0m');
program.help();
}
// Process the skip flags
const doFHIR = program.skip.every(a => a.toLowerCase() != 'fhir' && a.toLowerCase() != 'all');
const doJSON = program.skip.every(a => a.toLowerCase() != 'json' && a.toLowerCase() != 'all');
const doJSONSchema = program.skip.every(a => a.toLowerCase() != 'json-schema' && a.toLowerCase() != 'all');
const doES6 = program.skip.every(a => a.toLowerCase() != 'es6' && a.toLowerCase() != 'all');
const doModelDoc = program.skip.every(a => a.toLowerCase() != 'model-doc' && a.toLowerCase() != 'all');
const doCIMCORE = program.skip.every(a => a.toLowerCase() != 'cimcore' && a.toLowerCase() != 'all');
// Process the ADL flag
const doADL = program.adl;
// Process the de-duplicate error flag
const showDuplicateErrors = program.duplicate;
const importCimcore = program.importCimcore;
// Create the output folder if necessary
mkdirp.sync(program.out);
// Set up the logger streams
const [ll, lm] = [program.logLevel.toLowerCase(), program.logMode.toLowerCase()];
const streams = [];
if (lm == 'short' || lm == 'long') {
const prettyStdOut = new bps({mode: lm});
prettyStdOut.pipe(process.stdout);
streams.push({ level: ll, type: 'raw', stream: prettyStdOut});
} else if (lm == 'json') {
streams.push({ level: ll, stream: process.stdout });
}
// Setup a ringbuffer for counting the number of errors at the end
const logCounter = new LogCounter();
streams.push({ level: 'warn', type: 'raw', stream: logCounter});
// Always do a full JSON log
streams.push({ level: 'trace', path: path.join(program.out, 'out.log') });
const logger = bunyan.createLogger({
name: 'shr',
streams: streams
});
shrTI.setLogger(logger.child({module: 'shr-text-input'}));
shrEx.setLogger(logger.child({module: 'shr-expand'}));
if (doJSON) {
shrJE.setLogger(logger.child({module: 'shr-json-export'}));
}
if (doFHIR) {
shrFE.setLogger(logger.child({module: 'shr-fhir-export'}));
}
if (doJSONSchema) {
shrJSE.setLogger(logger.child({module: 'shr-json-schema-export'}));
}
if (doModelDoc) {
shrJDE.setLogger(logger.child({ module: 'shr-json-javadoc' }));
}
if (doADL) {
shrAE.setLogger(logger.child({module: 'shr-adl-export'}));
}
// NOTE: shr-es6-export does not currently support a Bunyan logger
// Go!
logger.info('Starting CLI Import/Export');
let configSpecifications;
let specifications;
let expSpecifications;
if (!importCimcore) {
configSpecifications = shrTI.importConfigFromFilePath(input, program.config);
if (!configSpecifications) {
logger.fatal('Project configuration not found! Exiting the program. ERROR_CODE:11032');
process.exit(1);
}
specifications = shrTI.importFromFilePath(input, configSpecifications);
expSpecifications = shrEx.expand(specifications, shrFE);
} else {
[configSpecifications, expSpecifications] = shrTI.importCIMCOREFromFilePath(`./cimcore-input/cimcore/`);
if (!configSpecifications) {
logger.fatal('Project configuration not found! Exiting the program. ERROR_CODE:11032');
process.exit(1);
}
}
configSpecifications.showDuplicateErrors = showDuplicateErrors;
let filter = false;
if (configSpecifications.filterStrategy != null) {
filter = configSpecifications.filterStrategy.filter;
}
if (filter) {
const specificationsFilter = new SpecificationsFilter(specifications, expSpecifications, configSpecifications);
[specifications, expSpecifications] = specificationsFilter.filter();
}
const failedExports = [];
let cimcoreSpecifications;
if (doCIMCORE) {
try {
cimcoreSpecifications = {
'dataElements': [],
'valueSets': [],
'mappings': [],
'namespaces': {},
//also includes 'projectInfo'
};
const baseCIMCOREPath = path.join(program.out, 'cimcore');
//meta project file
let versionInfo = {
'CIMPL_version': '5.6.0',
'CIMCORE_version': '1.1'
};
let projectMetaOutput = Object.assign({ 'fileType': 'ProjectInfo' }, configSpecifications, versionInfo); //project meta information
cimcoreSpecifications['projectInfo'] = projectMetaOutput;
const hierarchyPath = path.join(program.out, 'cimcore', 'project.json');
mkdirp.sync(path.dirname(hierarchyPath));
fs.writeFileSync(hierarchyPath, JSON.stringify(projectMetaOutput, null, ' '));
//meta namespace files
for (const ns of expSpecifications.namespaces.all) { //namespace files
let namespace = ns.namespace.replace(/\./, '-');
let out = Object.assign({ 'fileType': 'Namespace' }, ns.toJSON());
cimcoreSpecifications.namespaces[ns.namespace] = out;
const hierarchyPath = path.join(baseCIMCOREPath, namespace, `${namespace}.json`);
try {
mkdirp.sync(path.dirname(hierarchyPath));
fs.writeFileSync(hierarchyPath, JSON.stringify(out, null, ' '));
} catch (error) {
logger.error('Unable to successfully serialize namespace meta information %s into CIMCORE, failing with error "%s". ERROR_CODE:15004', namespace, error);
}
}
//data elements
for (const de of expSpecifications.dataElements.all) {
let namespace = de.identifier.namespace.replace(/\./, '-');
let fqn = de.identifier.fqn.replace(/\./g, '-');
let out = Object.assign({ 'fileType': 'DataElement' }, de.toJSON());
cimcoreSpecifications.dataElements.push(out);
const hierarchyPath = path.join(baseCIMCOREPath, namespace, `${fqn}.json`);
try {
mkdirp.sync(path.dirname(hierarchyPath));
fs.writeFileSync(hierarchyPath, JSON.stringify(out, null, ' '));
} catch (error) {
logger.error('Unable to successfully serialize element %s into CIMCORE, failing with error "%s". ERROR_CODE:15001', de.identifier.fqn, error);
}
}
//valuesets
for (const vs of expSpecifications.valueSets.all) {
let namespace = vs.identifier.namespace.replace(/\./, '-');
let name = vs.identifier.name.replace(/\./g, '-');
let out = Object.assign({ 'fileType': 'ValueSet' }, vs.toJSON());
cimcoreSpecifications.valueSets.push(out);
const hierarchyPath = path.join(baseCIMCOREPath, namespace, 'valuesets', `${name}.json`);
try {
mkdirp.sync(path.dirname(hierarchyPath));
fs.writeFileSync(hierarchyPath, JSON.stringify(out, null, ' '));
} catch (error) {
logger.error('Unable to successfully serialize value set %s into CIMCORE, failing with error "%s". ERROR_CODE:15002', vs.identifier.fqn, error);
}
}
//mappings
for (const target of expSpecifications.maps.targets) {
for (const mapping of expSpecifications.maps.getTargetMapSpecifications(target).all) {
let namespace = mapping.identifier.namespace.replace(/\./, '-');
let name = mapping.identifier.name;
let out = Object.assign({ 'fileType': 'Mapping' }, mapping.toJSON());
cimcoreSpecifications.mappings.push(out);
const hierarchyPath = path.join(baseCIMCOREPath, namespace, 'mappings', target, `${name}-mapping.json`);
try {
mkdirp.sync(path.dirname(hierarchyPath));
fs.writeFileSync(hierarchyPath, JSON.stringify(out, null, ' '));
} catch (error) {
logger.error('Unable to successfully serialize mapping %s into CIMCORE, failing with error "%s". ERROR_CODE:15003', mapping.identifier.fqn, error);
}
}
}
} catch (error) {
logger.fatal('Failure in CIMCORE export. Aborting with error message: %s', error);
failedExports.push('CIMCORE');
}
} else {
logger.info('Skipping CIMCORE export');
}
if (doADL) {
try {
shrAE.generateADLtoPath(expSpecifications, configSpecifications, program.out);
} catch (error) {
logger.fatal('Failure in ADL export. Aborting with error message: %s', error);
failedExports.push('shr-adl-bmm-export');
}
} else {
logger.info('Skipping ADL export');
}
if (doJSON) {
if (!importCimcore) {
try {
const jsonHierarchyResults = shrJE.exportToJSON(specifications, configSpecifications);
const hierarchyPath = path.join(program.out, 'json', 'definitions.json');
mkdirp.sync(path.dirname(hierarchyPath));
fs.writeFileSync(hierarchyPath, JSON.stringify(jsonHierarchyResults, null, ' '));
} catch (error) {
logger.fatal('Failure in JSON export. Aborting with error message: %s', error);
failedExports.push('shr-json-export');
}
} else {
//Skipping website generation legacy output for imported cimcore.
logger.info('Using imported CIMCORE, skipping JSON export');
}
} else {
logger.info('Skipping JSON export');
}
if (doES6) {
try {
const es6Results = shrEE.exportToES6(expSpecifications, configSpecifications);
const es6Path = path.join(program.out, 'es6');
const handleNS = (obj, fpath) => {
mkdirp.sync(fpath);
for (const key of Object.keys(obj)) {
if (key.endsWith('.js')) {
fs.writeFileSync(path.join(fpath, key), obj[key]);
} else {
handleNS(obj[key], path.join(fpath, key));
}
}
};
handleNS(es6Results, es6Path);
} catch (error) {
logger.fatal('Failure in ES6 export. Aborting with error message: %s', error);
failedExports.push('shr-es6-export');
}
} else {
logger.info('Skipping ES6 export');
}
if (doFHIR) {
try {
const fhirResults = shrFE.exportToFHIR(expSpecifications, configSpecifications);
const baseFHIRPath = path.join(program.out, 'fhir');
const baseFHIRProfilesPath = path.join(baseFHIRPath, 'profiles');
mkdirp.sync(baseFHIRProfilesPath);
for (const profile of fhirResults.profiles) {
fs.writeFileSync(path.join(baseFHIRProfilesPath, `${profile.id}.json`), JSON.stringify(profile, null, 2));
}
const baseFHIRExtensionsPath = path.join(baseFHIRPath, 'extensions');
mkdirp.sync(baseFHIRExtensionsPath);
for (const extension of fhirResults.extensions) {
fs.writeFileSync(path.join(baseFHIRExtensionsPath, `${extension.id}.json`), JSON.stringify(extension, null, 2));
}
const baseFHIRCodeSystemsPath = path.join(baseFHIRPath, 'codeSystems');
mkdirp.sync(baseFHIRCodeSystemsPath);
for (const codeSystem of fhirResults.codeSystems) {
fs.writeFileSync(path.join(baseFHIRCodeSystemsPath, `${codeSystem.id}.json`), JSON.stringify(codeSystem, null, 2));
}
const baseFHIRValueSetsPath = path.join(baseFHIRPath, 'valueSets');
mkdirp.sync(baseFHIRValueSetsPath);
for (const valueSet of fhirResults.valueSets) {
fs.writeFileSync(path.join(baseFHIRValueSetsPath, `${valueSet.id}.json`), JSON.stringify(valueSet, null, 2));
}
const baseFHIRModelsPath = path.join(baseFHIRPath, 'logical');
mkdirp.sync(baseFHIRModelsPath);
for (const model of fhirResults.models) {
fs.writeFileSync(path.join(baseFHIRModelsPath, `${model.id}.json`), JSON.stringify(model, null, 2));
}
fs.writeFileSync(path.join(baseFHIRPath, `shr_qa.html`), fhirResults.qaHTML);
shrFE.exportIG(expSpecifications, fhirResults, path.join(baseFHIRPath, 'guide'), configSpecifications, input);
} catch (error) {
logger.fatal('Failure in FHIR export. Aborting with error message: %s', error);
failedExports.push('shr-fhir-export');
}
} else {
logger.info('Skipping FHIR export');
}
if (doJSONSchema) {
try {
let typeURL = configSpecifications.entryTypeURL;
if (!typeURL) {
typeURL = 'http://nowhere.invalid/';
}
const baseSchemaNamespace = 'https://standardhealthrecord.org/schema';
const baseSchemaNamespaceWithSlash = baseSchemaNamespace + '/';
const jsonSchemaResults = shrJSE.exportToJSONSchema(expSpecifications, baseSchemaNamespace, typeURL);
const jsonSchemaPath = path.join(program.out, 'json-schema');
mkdirp.sync(jsonSchemaPath);
for (const schemaId in jsonSchemaResults) {
const filename = `${schemaId.substring(baseSchemaNamespaceWithSlash.length).replace(/\//g, '.')}.schema.json`;
fs.writeFileSync(path.join(jsonSchemaPath, filename), JSON.stringify(jsonSchemaResults[schemaId], null, ' '));
}
// Uncomment the following to get expanded schemas
// shrJSE.setLogger(logger.child({module: 'shr-json-schema-export-expanded'}));
// const baseSchemaExpandedNamespace = 'https://standardhealthrecord.org/schema-expanded';
// const baseSchemaExpandedNamespaceWithSlash = baseSchemaExpandedNamespace + '/';
// const jsonSchemaExpandedResults = shrJSE.exportToJSONSchema(expSpecifications, baseSchemaExpandedNamespace, typeURL, true);
// const jsonSchemaExpandedPath = path.join(program.out, 'json-schema-expanded');
// mkdirp.sync(jsonSchemaExpandedPath);
// for (const schemaId in jsonSchemaExpandedResults) {
// const filename = `${schemaId.substring(baseSchemaExpandedNamespaceWithSlash.length).replace(/\//g, '.')}.schema.json`;
// fs.writeFileSync(path.join(jsonSchemaExpandedPath, filename), JSON.stringify(jsonSchemaExpandedResults[schemaId], null, ' '));
// }
} catch (error) {
logger.fatal('Failure in JSON Schema export. Aborting with error message: %s', error);
failedExports.push('shr-json-schema-export');
}
} else {
logger.info('Skipping JSON Schema export');
}
if (doModelDoc) {
if (doCIMCORE) {
try {
const hierarchyPath = path.join(program.out, 'modeldoc');
const fhirPath = path.join(program.out, 'fhir', 'guide', 'pages', 'modeldoc');
const javadocResults = shrJDE.compileJavadoc(cimcoreSpecifications, hierarchyPath);
shrJDE.exportToPath(javadocResults, hierarchyPath);
if (doFHIR && configSpecifications.implementationGuide.includeModelDoc == true) {
const igJavadocResults = shrJDE.compileJavadoc(cimcoreSpecifications, hierarchyPath, true);
shrJDE.exportToPath(igJavadocResults, fhirPath);
}
} catch (error) {
logger.fatal('Failure in Model Doc export. Aborting with error message: %s', error);
failedExports.push('shr-model-doc');
}
} else {
logger.fatal('CIMCORE is required for generating Model Doc. Skipping Model Docs export.');
failedExports.push('shr-model-doc');
}
} else {
logger.info('Skipping Model Docs export');
}
logger.info('Finished CLI Import/Export');
const ftlCounter = logCounter.fatal;
const errCounter = logCounter.error;
const wrnCounter = logCounter.warn;
let [errColor, errLabel, wrnColor, wrnLabel, resetColor, ftlColor, ftlLabel] = ['\x1b[32m', 'errors', '\x1b[32m', 'warnings', '\x1b[0m', '\x1b[31m', 'fatal errors'];
if (ftlCounter.count > 0) {
// logger.fatal('');
ftlLabel = `fatal errors (${failedExports.join(', ')})`;
}
if (errCounter.count > 0) {
errColor = '\x1b[31m'; // red
errLabel = `errors (${errCounter.modules.join(', ')})`;
}
if (wrnCounter.count > 0) {
wrnColor = '\x1b[35m'; // magenta
wrnLabel = `warnings (${wrnCounter.modules.join(', ')})`;
}
// Get the elapsed time
const hrend = process.hrtime(hrstart);
console.log('------------------------------------------------------------');
console.log('Elapsed time: %d.%ds', hrend[0], Math.floor(hrend[1]/1000000));
if (ftlCounter.count > 0) console.log('%s%d %s%s', ftlColor, ftlCounter.count, ftlLabel, resetColor);
console.log('%s%d %s%s', errColor, errCounter.count, errLabel, resetColor);
console.log('%s%d %s%s', wrnColor, wrnCounter.count, wrnLabel, resetColor);
console.log('------------------------------------------------------------');