forked from FHIR/FSHOnline-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.js
82 lines (70 loc) · 2.76 KB
/
manifest.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
const fs = require('fs');
const path = require('path');
const moment = require('moment');
const lineReaderComponent = require('readline');
const manifestPath = path.join(__dirname, 'index.json');
/***
* Reads the first two lines of a FSH file, returning the name and description properties
*/
async function getMetadata(filePath, fileName) {
let metaData = {name: fileName.replace('.fsh', '')};
let lineReader = lineReaderComponent.createInterface({
input: fs.createReadStream(filePath)
});
const parseLines = async () => {
for await (const line of lineReader) {
if (line.includes('@Name:')) {
const splitLine = line.split('@Name:');
metaData.name = splitLine[1].trim();
} else if (line.includes('@Description')) {
const splitLine = line.split('@Description:');
metaData.description = splitLine[1].trim();
}
if (Object.keys(metaData).length == 2) return;
}
}
await parseLines();
return metaData;
}
/***
* Takes the local file path of a file and retuns the corresponding Github url suffix
*/
function generatePath(filePath) {
let urlPath = filePath.split('FSHOnline-Examples/Examples/')[1];
return urlPath.replace(' ', '%20'); // Spaces appear as '%20' in Github urls
}
/***
* Returns an array of manifest objects when provided a directory's filepath
*/
async function getChildren(baseDirName) {
let workingArray = [];
// First we'll take in all files in the provided directory
const files = fs.readdirSync(baseDirName);
for (const file of files) {
const filePath = path.join(baseDirName, file); // We're appending the child file name to the base directory name
let fileStats = fs.statSync(filePath); // This contains directory information
if (file.endsWith('fsh')) {
const index = workingArray.push({ path: generatePath(filePath) }) - 1;
const metaData = await getMetadata(filePath, file);
workingArray[index].name = metaData.name;
workingArray[index].description = metaData.description;
workingArray[index].type = "file";
} else if (fileStats.isDirectory()) {
const index = workingArray.push({ name: file, path: generatePath(filePath) }) - 1;
workingArray[index].type = 'category';
workingArray[index].children = [];
const children = await getChildren(filePath);
workingArray[index].children = children;
if (workingArray[index].children.length === 0) {
workingArray.pop();
}
}
}
return workingArray;
}
async function generateManifest() {
const examplesArr = await getChildren(path.join(__dirname, 'Examples'));
let manifestObj = {timestamp: moment(Date.now()).format(), children: examplesArr };
fs.writeFileSync(manifestPath, JSON.stringify(manifestObj, null, "\t"), 'utf-8');
}
generateManifest();