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
/
app.js
346 lines (315 loc) · 13.8 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
const fs = require('fs-extra');
const path = require('path');
const mkdirp = require('mkdirp');
const bunyan = require('bunyan');
const program = require('commander');
const chalk = require('chalk');
const { sanityCheckModules } = require('shr-models');
const shrTI = require('shr-text-import');
const shrEx = require('shr-expand');
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 shrDD = require('shr-data-dict-export');
const LogCounter = require('./logcounter');
const SpecificationsFilter = require('./filter');
/* eslint-disable no-console */
sanityCheckModules({shrTI, shrEx, 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>', /^(fatal|error|warn|info|debug|trace)$/i, 'info')
.option('-s, --skip <feature>', 'skip an export feature <fhir,json-schema,model-doc,data-dict,all>', collect, [])
.option('-m, --log-mode <mode>', 'the console log mode <normal,json,off>', /^(normal|json|off)$/i, 'normal')
.option('-o, --out <out>', `the path to the output folder`, path.join('.', 'out'))
.option('-c, --config <config>', 'the name of the config file', 'config.json')
.option('-d, --deduplicate', 'do not show duplicate error messages (default: false)')
.option('-j, --export-es6', 'export ES6 JavaScript classes (experimental, default: false)')
.option('-i, --import-cimcore', 'import CIMCORE files instead of CIMPL (default: false)')
.option('-n, --clean', 'Save archive of old output directory and perform clean build (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 doJSONSchema = program.skip.every(a => a.toLowerCase() != 'json-schema' && a.toLowerCase() != 'all');
const doModelDoc = program.skip.every(a => a.toLowerCase() != 'model-doc' && a.toLowerCase() != 'all');
const doDD = program.skip.every(a => a.toLowerCase() != 'data-dict' && a.toLowerCase() != 'all');
// Process the de-duplicate error flag
const showDuplicateErrors = !program.deduplicate;
const importCimcore = program.importCimcore;
const doES6 = program.exportEs6;
const clean = program.clean;
// Archive old output directory if it exists
if (clean && fs.existsSync(program.out)) {
let archiveDir;
let targetDir;
let slashIndex = program.out.lastIndexOf('/') > 0 ?
program.out.lastIndexOf('/') : program.out.lastIndexOf('\\');
// Figure out path to move directory into archive
if (slashIndex > 0) {
archiveDir = path.join(program.out.substring(0, slashIndex), 'archive');
targetDir = path.join(archiveDir, program.out.substr(slashIndex));
} else {
archiveDir = 'archive';
targetDir = path.join(archiveDir, program.out);
}
// If archive does not exist, create it
if (!fs.existsSync(archiveDir)) {
mkdirp.sync(archiveDir);
}
// Ensure no naming conflicts with previous archives
let counter = 1;
while(fs.existsSync(targetDir + '-' + counter)) { counter += 1; }
fs.renameSync(program.out, targetDir + '-' + counter);
}
// Create the output folder if necessary
mkdirp.sync(program.out);
const errorFiles = [shrTI.errorFilePath(), shrEx.errorFilePath(), shrFE.errorFilePath(), shrJDE.errorFilePath(),
shrEE.errorFilePath(), shrJSE.errorFilePath(), path.join(__dirname, "errorMessages.txt")]
const PrettyPrintDuplexStreamJson = require('./PrettyPrintDuplexStreamJson');
const mdpStream = new PrettyPrintDuplexStreamJson(null, errorFiles, showDuplicateErrors, path.join(program.out, 'out.log'));
// Set up the logger streams
const [ll, lm] = [program.logLevel.toLowerCase(), program.logMode.toLowerCase()];
const streams = [];
if (lm == 'normal') {
streams.push({ level: ll, stream: mdpStream });
mdpStream.pipe(process.stdout);
} 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
const logger = bunyan.createLogger({
name: 'shr',
module: 'shr-cli',
streams: streams
});
shrTI.setLogger(logger.child({module: 'shr-text-input'}));
shrEx.setLogger(logger.child({module: 'shr-expand'}));
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 (doES6) {
shrEE.setLogger(logger.child({ module: 'shr-es6-export'}));
}
if (doDD) {
shrDD.setLogger(logger.child({ module: 'shr-data-dict-export'}));
}
// Go!
// 05001, 'Starting CLI Import/Export',,
logger.info('05001');
let configSpecifications = shrTI.importConfigFromFilePath(input, program.config);
if (!configSpecifications) {
process.exit(1);
}
let specifications;
let expSpecifications;
if (!importCimcore) {
specifications = shrTI.importFromFilePath(input, configSpecifications);
configSpecifications.specPath = input;
expSpecifications = shrEx.expand(specifications, configSpecifications, shrFE);
} else {
[configSpecifications, expSpecifications] = shrTI.importCIMCOREFromFilePath(input);
}
let filter = false;
if (configSpecifications.filterStrategy != null) {
filter = configSpecifications.filterStrategy.filter;
// 05009, 'Using filterStrategy in the configuration file is deprecated and should be done in content profile instead',,
logger.warn('05009')
}
if (configSpecifications.implementationGuide && configSpecifications.implementationGuide.primarySelectionStrategy) {
// 05010, 'Using primarySelectionStrategy in the configuration file is deprecated and should be done in content profile instead',,
logger.warn('05010');
}
if (expSpecifications.contentProfiles.all.length > 0) {
filter = true;
}
if (filter) {
const specificationsFilter = new SpecificationsFilter(specifications, expSpecifications, configSpecifications);
[specifications, expSpecifications] = specificationsFilter.filter();
}
const failedExports = [];
if (doDD) {
try {
const hierarchyPath = path.join(program.out, 'data-dictionary');
shrDD.generateDDtoPath(expSpecifications, configSpecifications, hierarchyPath);
} catch (error) {
// 15006, 'Failure in data dictionary export. Aborting with error message: ${errorText}', 'Unknown, 'errorNumber'
logger.fatal({ errorText: error.stack }, '15006');
failedExports.push('shr-data-dict-export');
}
} else {
// 05004, 'Skipping Data Dictionary export',,
logger.info('05004');
}
let fhirResults = null;
if (doES6 || doFHIR){
fhirResults = shrFE.exportToFHIR(expSpecifications, configSpecifications);
}
if (doES6) {
try {
const es6Results = shrEE.exportToES6(expSpecifications, fhirResults);
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) {
// 15007, 'Failure in ES6 export. Aborting with error message: ${errorText}', 'Unknown, 'errorNumber'
logger.fatal({ errorText: error.stack }, '15007');
failedExports.push('shr-es6-export');
}
} else {
// 05005, 'Skipping ES6 export',,
logger.info('05005');
}
if (doFHIR) {
try {
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) {
// 15008, 'Failure in FHIR export. Aborting with error message: ${errorText}', 'Unknown, 'errorNumber'
logger.fatal({ errorText: error.stack }, '15008');
failedExports.push('shr-fhir-export');
}
} else {
// 05006, 'Skipping FHIR export',,
logger.info('05006');
}
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) {
// 15009, 'Failure in JSON Schema export. Aborting with error message: ${errorText}', 'Unknown, 'errorNumber'
logger.fatal({ errorText: error.stack }, '15009');
failedExports.push('shr-json-schema-export');
}
} else {
// 05007, 'Skipping JSON Schema export',,
logger.info('05007');
}
if (doModelDoc) {
try {
const hierarchyPath = path.join(program.out, 'modeldoc');
const modelDocConfig = {
namespaces: expSpecifications.namespaces.all,
dataElements: expSpecifications.dataElements.all,
projectInfo: configSpecifications
};
const javadocResults = shrJDE.compileJavadoc(modelDocConfig, hierarchyPath);
shrJDE.exportToPath(javadocResults, hierarchyPath);
if (doFHIR && configSpecifications.implementationGuide.includeModelDoc == true) {
const fhirPath = path.join(program.out, 'fhir', 'guide', 'pages', 'modeldoc');
const igJavadocResults = shrJDE.compileJavadoc(modelDocConfig, hierarchyPath, true);
shrJDE.exportToPath(igJavadocResults, fhirPath);
}
} catch (error) {
logger.fatal({ errorText: error.stack }, '15010');
failedExports.push('shr-model-doc');
}
} else {
// 05008, 'Skipping Model Docs export',,
logger.info('05008');
}
// 05002, 'Finished CLI Import/Export',,
logger.info('05002');
const ftlCounter = logCounter.fatal;
const errCounter = logCounter.error;
const wrnCounter = logCounter.warn;
let [errLabel, wrnLabel, ftlLabel] = ['errors', 'warnings', 'fatal errors'];
if (ftlCounter.count > 0) {
ftlLabel = `fatal errors (${failedExports.join(', ')})`;
}
if (errCounter.count > 0) {
errLabel = `errors (${errCounter.modules.join(', ')})`;
}
if (wrnCounter.count > 0) {
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(chalk.redBright('%d %s'), ftlCounter.count, ftlLabel);
console.log(chalk.bold.redBright('%d %s'), errCounter.count, errLabel);
console.log(chalk.bold.yellowBright('%d %s'), wrnCounter.count, wrnLabel);
console.log('------------------------------------------------------------');