-
Notifications
You must be signed in to change notification settings - Fork 1
/
FSHingTrip.js
147 lines (138 loc) · 4.17 KB
/
FSHingTrip.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
#!/usr/bin/env node
const path = require("path");
const fs = require("fs-extra");
const diff = require("diff");
const execSync = require("child_process").execSync;
if (process.argv.length < 3) {
console.log("Please specify the path to the input directory");
console.log("Example usage: node FSHingTrip.js ./FHIRDefinitions");
process.exit(1);
}
// Determine the input/output dirs
const inDir = process.argv[2];
const outDir = process.argv[3]
? process.argv[3]
: path.join(process.cwd(), "build");
const goFSHOutDir = path.join(outDir, "goFSH");
const sushiOutDir = path.join(outDir, "sushi");
const differentialOutDir = path.join(outDir, "diff");
// goFSH
console.log('\nRunning goFSH\n');
try {
execSync(`ihsus ${inDir} -o ${goFSHOutDir}`, { stdio: "inherit" });
} catch {}
// sushi
console.log('\nRunning sushi\n')
if (!fs.existsSync(path.join(inDir, "config.yaml"))) {
console.log(
`A config.yaml is required for SUSHI to run. Add a config.yaml to ${inDir}`
);
} else {
fs.copyFileSync(
path.join(inDir, "config.yaml"),
path.join(outDir, "config.yaml")
);
}
try {
execSync(`sushi ${outDir} -o ${sushiOutDir}`, { stdio: "inherit" });
} catch {}
console.log('\nGenerating differentials')
// Get the original and round-trip versions of the files
const originalFiles = getFilesRecursive(inDir).filter((file) =>
file.endsWith(".json")
);
const roundTripFiles = getFilesRecursive(
path.join(outDir, "sushi")
).filter((file) => file.endsWith(".json"));
const originalJSON = [];
originalFiles.forEach((file) => {
originalJSON.push(fs.readJSONSync(file));
});
const roundTripJSON = [];
roundTripFiles.forEach((file) => {
roundTripJSON.push(fs.readJSONSync(file));
});
// Generate a differential for each original JSON file
originalJSON.forEach((originalArtifact) => {
let html = `
<head>
<style>
* {
font-family: Consolas, sans-serif
}
table {
white-space: pre-wrap;
width: 100%;
border-collapse: collapse;
}
table, th, td {
border: 1px solid black
}
</style>
</head>
<table>
<tr>
<th>Element</th>
<th>Difference</th>
<th>Original</th>
<th>Round-trip</th>
</tr>
`;
const roundTripArtifact = roundTripJSON.find((art) => art.id === originalArtifact.id);
// Compare the original elements to the round-trip version
originalArtifact.differential.element.forEach((originalED) => {
const roundTripED = roundTripArtifact.differential.element.find(
e => e.id === originalED.id
);
html += createDifferentialEntry(originalED, roundTripED, originalED.id);
});
// Track if there are any elements in the round-trip version not in the original
roundTripArtifact.differential.element.forEach((roundTripED) => {
const originalED = originalArtifact.differential.element.find(
(e) => e.id === roundTripED.id
);
if (!originalED) {
html += createDifferentialEntry(originalED, roundTripED, roundTripED.id);
}
});
html += "</table>";
fs.ensureDirSync(path.join(differentialOutDir));
fs.writeFileSync(
path.join(
differentialOutDir,
`${originalArtifact.resourceType}-${originalArtifact.id}-diff.html`
),
html
);
});
function createDifferentialEntry(originalED, roundTripED, id) {
const originalElementJSON = JSON.stringify(originalED, null, 2);
const roundTripElementJSON = JSON.stringify(roundTripED, null, 2);
const diffParts = diff.diffJson(
originalElementJSON ? originalElementJSON : "",
roundTripElementJSON ? roundTripElementJSON : ""
);
let diffString = "";
diffParts.forEach((part) => {
const color = part.added ? "green" : part.removed ? "red" : "gray";
diffString += `<span style="color:${color};">${part.value}</span>`;
});
return `
<tr>
<td>${id}</td>
<td>${diffString}</td>
<td>${originalElementJSON}</td>
<td>${roundTripElementJSON}</td>
</tr>
`;
}
function getFilesRecursive(dir) {
if (fs.statSync(dir).isDirectory()) {
const ancestors = fs
.readdirSync(dir, "utf8")
.map((f) => getFilesRecursive(path.join(dir, f)));
return [].concat(...ancestors);
} else {
return [dir];
}
}