-
Notifications
You must be signed in to change notification settings - Fork 0
/
fhirPackages.ts
102 lines (91 loc) · 3.22 KB
/
fhirPackages.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
import { logger } from '../common/logger';
import * as fs from 'fs';
import path from 'path';
import { FHIRDefinitions, fpl } from 'fhir-package-loader';
import YAML from 'yaml';
import {RuntimeSettings} from './settings';
export async function identifyAndLoadDependencyPackages(settings: RuntimeSettings) {
const sushiConfigPath = findSushiConfig(settings.outputRoot);
if (sushiConfigPath) {
const dependencyList = extractIGDependenciesFromSushiConfig(sushiConfigPath);
const fhirDefinitions = await loadPackages(dependencyList);
if (fhirDefinitions) {
settings.fhirDefinitions = fhirDefinitions;
} else {
logger.error('failed to load dependency IGs');
}
}
}
function findSushiConfig(outputRoot: string): string | undefined {
const configPath = [
path.join(outputRoot, 'sushi-config.yaml'),
path.join(outputRoot, 'sushi-config.yml')
].find(fs.existsSync);
if (configPath) {
// The config already exists, so return it
const pathToReturn = path.resolve(configPath);
logger.info(`Using sushi configuration file: ${pathToReturn}`);
return pathToReturn;
} else {
logger.warn(`No sushi-config.yaml file found at output root '${outputRoot}'`);
return undefined;
}
}
function extractIGDependenciesFromSushiConfig(sushiConfigPath: string): string[] {
const dependencyList: string[] = [];
const configYaml = fs.readFileSync(sushiConfigPath, 'utf8');
try {
const parsed = YAML.parse(configYaml);
// core FHIR version
switch (parsed.fhirVersion) {
case '4.0.1':
dependencyList.push('hl7.fhir.r4.core@4.0.1');
logger.info('found dependency hl7.fhir.r4.core@4.0.1')
break;
case undefined:
logger.warn('no FHIR core version found to include in dependencies');
break;
default:
logger.warn('unsupported FHIR version: ' + parsed.fhirVersion);
}
// dependency IGs
const sushiDependencies = Object.entries(parsed.dependencies);
if (sushiDependencies.length === 0) {
logger.warn('no ig dependencies found in sushi-config file');
}
sushiDependencies.forEach(([key, dependency]: [string, any]) => {
const version = typeof dependency === "string" ? dependency : dependency.version;
logger.info(`found dependency ${key}@${version}`)
dependencyList.push(`${key}@${version}`)
});
} catch (error) {
logger.error(`Error parsing configuration: ${error}.`);
}
return dependencyList;
}
async function loadPackages(packageList: string[]) {
let loadedDefinitions: FHIRDefinitions | undefined = undefined;
if (packageList.length < 1) {
// only the default
logger.warn('No packages found to load!');
}
await fpl(packageList)
.then(results => {
// handle results
results.errors.forEach(oneErr => {
logger.error(oneErr);
});
results.warnings.forEach(oneWarning => {
logger.warn(oneWarning);
});
results.failedPackages.forEach(onePackage => {
logger.warn("Package didn't load: " + onePackage);
});
logger.info('loaded: ' + results.defs.size());
loadedDefinitions = results.defs;
})
.catch(err => {
logger.error('package loading failed: ' + err);
});
return loadedDefinitions;
}