-
Notifications
You must be signed in to change notification settings - Fork 132
/
s2tQuicktypeUtil.js
79 lines (68 loc) · 2.77 KB
/
s2tQuicktypeUtil.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
/**
* SPDX-License-Identifier: Apache-2.0
* Copyright FINOS FDC3 contributors - see NOTICE file
*/
/** Utility for preparing arguments to quicktype, which workaround a specific
* quicktype bug in command line argument handling (where a directory is used
* as input the source language argument is ignored which causes our schemas
* to be interpreted as JSON input, rather than JSONSchema).
*
* Individual file arguments will be interpreted as 'additional' schema files
* that will be referenced from the other schemas and will not have top-level output
* schemas generated, while folders will be listed and the schema files they contain
* added as inputs and will have top-level types generated.
*
* */
const path = require('path');
const fs = require('fs');
const { forEachChild } = require('typescript');
const exec = require('child_process').exec;
const args = process.argv.slice(2);
const outputFile = args.pop();
const inputs = args;
const toProcess = [];
console.log('Inputs schema files: ' + inputs.join(' | '));
console.log('Output file argument: ' + outputFile);
let sources = '';
let additionalSchemaFiles = ''
//skip duplicate paths (we might want to specify some files to go first, and might duplicate them)
const allPaths = new Set();
const addAPath = (aPath,paths,sources,type) => {
if (!paths.has(aPath)) {
paths.add(aPath)
return sources + ` ${type} ${aPath}`;
} else {
console.log(`skipping duplicate path ${aPath}`);
return sources;
}
}
//process the content of folders to produce code for top-level types
let inputIndex = 0;
while (inputIndex < inputs.length) {
if (inputs[inputIndex].endsWith('.schema.json')) {
//add individual files with -S as additional schema files used in references (rather than ones that need a top-level output)
additionalSchemaFiles = addAPath(path.join(inputs[inputIndex]),allPaths,additionalSchemaFiles, "-S");
} else {
fs.readdirSync(inputs[inputIndex], { withFileTypes: true }).forEach(file => {
if (file.isDirectory()) {
inputs.push(path.join(inputs[inputIndex], file.name));
} else if (file.name.endsWith('.schema.json')) {
sources = addAPath(path.join(inputs[inputIndex], file.name),allPaths,sources, "--src");
}
});
}
inputIndex++;
}
// Normalise path to local quicktype executable.
//const quicktypeExec = "node " + ["..","quicktype","dist","index.js"].join(path.sep);
const quicktypeExec = ['..', 'node_modules', '.bin', 'quicktype'].join(path.sep);
const command = `${quicktypeExec} --prefer-const-values --prefer-unions -s schema -o ${outputFile} ${additionalSchemaFiles} ${sources}`;
console.log('command to run: ' + command);
exec(command, function(error, stdout, stderr) {
if (stdout) {
console.log(stdout);
}
if (stderr) {
console.log(stderr);
}
});