-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.js
152 lines (131 loc) · 4.53 KB
/
utils.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
import path from 'path';
import fs from 'fs-extra';
import axios from 'axios';
import yaml from 'js-yaml';
import jsonata from 'jsonata';
const server = axios.create({ timeout: 15000 });
const workingDir = path.resolve('.');
const sushiConfigPath = path.join(workingDir, 'sushi-config.yaml');
export const fetch = async (url) => {
console.log(`Fetching ${url}...`);
const res = await server.get(url, { responseType: 'arraybuffer' });
return res;
};
export const getJrePath = () => {
return path.join(workingDir, 'jre');
};
const getJreVersionPath = () => {
const jrePath = getJrePath();
if (fs.existsSync(jrePath) === false) {
console.log('No JRE versions installed :(');
return undefined;
};
const versions = fs.readdirSync(jrePath);
if (versions.length === 1) {
return path.join(jrePath, versions[0]);
};
if (versions.length > 1) {
console.log('Multiple versions of jre found... Deleting all of them!');
};
if (versions.length === 0) {
console.log('No JRE versions installed :(');
};
fs.rmSync(jrePath, { recursive: true, force: true });
return undefined;
};
export const getJreBin = () => {
const versionPath = getJreVersionPath();
if (versionPath) {
return path.join(versionPath, 'bin', 'java');
};
return undefined;
};
export const getValidatorPath = () => {
return path.join(workingDir, 'validator_cli.jar');
};
export const getValidationOutputPath = () => {
return path.join(workingDir, 'validator_cli_output');
};
export const getFshOutputFolder = () => {
return path.join(workingDir, 'fsh-generated', 'resources');
};
export const getFshInputFolder = () => {
return path.join(workingDir, 'input', 'fsh');
};
export const overwriteRuleSet = (fsh) => {
const fshFile = path.join(getFshInputFolder(), 'RuleSet-metadata.fsh');
return fs.writeFileSync(fshFile, fsh);
}
export const getExamplesFolder = () => {
return path.join(workingDir, 'examples');
};
export const getDiffFolder = () => {
return path.join(workingDir, 'differentials', 'fsh-generated', 'resources');
};
export const deleteIgResource = () => {
const igFilePath = path.join(getDiffFolder(), `ImplementationGuide-${readSushiConfig().id}.json`);
return fs.unlinkSync(igFilePath);
};
export const readSushiConfig = () => {
try {
const doc = yaml.load(fs.readFileSync(sushiConfigPath, 'utf8'));
return doc;
} catch (e) {
console.log(e);
}
};
export const readValidationResults = (filePath) => {
try {
const doc = JSON.parse(fs.readFileSync(filePath, 'utf8'));
return doc;
} catch (e) {
console.log(e);
}
};
export const getDependencies = (sushiConfig) => {
if (sushiConfig?.dependencies) {
const igs = Object.entries(sushiConfig.dependencies);
const igParamArray = igs.map((kv) => `-ig ${kv[0]}#${kv[1].version}`);
return igParamArray.join(' ');
} else {
return ''
}
};
export const extractErrorSummary = async (resource) => {
const expr = jsonata(`(
resourceType='Bundle' ?
entry.resource.issue[severity in ['fatal','error','warning']]{
severity: $count($)
}
:
issue[severity in ['fatal','error','warning']]{
severity: $count($)
}
)`)
return await expr.evaluate(resource)
}
export const generatePackageManifest = async () => {
const igFilePath = path.join(getFshOutputFolder(), `ImplementationGuide-${readSushiConfig().id}.json`);
const igResource = JSON.parse(fs.readFileSync(igFilePath, 'utf8'));
const expr = jsonata(`{
'name' : ImplementationGuide.packageId,
'version' : ImplementationGuide.version,
'canonical' : ImplementationGuide.url,
'url' : ImplementationGuide.manifest.rendering,
'title' : ImplementationGuide.title,
'description' : ImplementationGuide.description,
'fhirVersions': ImplementationGuide.fhirVersion[],
'dependencies' : ImplementationGuide.dependsOn ? ImplementationGuide.dependsOn{packageId: version},
'author' : ImplementationGuide.publisher,
'maintainers' : ImplementationGuide.contact.{
'name': name,
'email': telecom[system='email'].value,
'url': telecom[system='url'].value
}[],
'license' : ImplementationGuide.license,
'jurisdiction' : ImplementationGuide.jurisdiction.coding[system="urn:iso:std:iso:3166"][0].(system & '#' & code)
}`);
const packageManifest = await expr.evaluate({ ImplementationGuide: igResource });
fs.writeFileSync(path.join(getFshOutputFolder(), 'package.json'), JSON.stringify(packageManifest, null, 2));
return true;
}