-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.ts
99 lines (90 loc) · 2.67 KB
/
index.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
import yaml from 'js-yaml';
import JsonRefs from 'json-refs';
import fs from 'node:fs';
import path from 'node:path';
function dictToString(dict) {
const res: string[] = [];
for (const [k, v] of Object.entries(dict)) {
res.push(`${k}: ${v}`);
}
return res.join('\n');
}
function bundle(originalFile) {
// @ts-expect-error: Types do not match the version of js-yaml installed
const root = yaml.safeLoad(fs.readFileSync(originalFile, 'utf8'));
const options = {
filter: ['relative', 'remote', 'local'],
resolveCirculars: true,
location: originalFile,
loaderOptions: {
processContent: function (res, callback) {
// @ts-expect-error: Types do not match the version of js-yaml installed
callback(undefined, yaml.safeLoad(res.text));
},
},
};
JsonRefs.clearCache();
return JsonRefs.resolveRefs(root, options).then(
function (results) {
const resErrors = {};
for (const [k, v] of Object.entries(results.refs)) {
if (
'missing' in v &&
v.missing === true &&
(v.type === 'relative' || v.type === 'remote')
) {
resErrors[k] = v.error;
}
}
if (Object.keys(resErrors).length > 0) {
return Promise.reject(new Error(dictToString(resErrors)));
}
return results.resolved;
},
function (e) {
const error = {};
Object.getOwnPropertyNames(e).forEach(function (key) {
error[key] = e[key];
});
return Promise.reject(new Error(dictToString(error)));
}
);
}
function build(originalFile, _, bundleTo) {
bundle(originalFile).then(
function (bundled) {
const bundleString = JSON.stringify(bundled, null, 2);
if (typeof bundleTo === 'string') {
fs.writeFile(bundleTo, bundleString, function (err) {
if (err) {
// eslint-disable-next-line no-console
console.log(err);
return;
}
// eslint-disable-next-line no-console
console.log('Saved bundle file at ' + bundleTo);
});
}
},
function (err) {
// eslint-disable-next-line no-console
console.log(err);
}
);
}
let originalFile: any;
let targetDirValue: any;
const argv = process.argv.slice(2);
originalFile = argv[0];
const derefedFile = argv[1] || 'tests/apidocs/openapi-derefed.json';
try {
if (!path.isAbsolute(originalFile)) {
originalFile = path.resolve(process.cwd(), originalFile);
}
targetDirValue = path.dirname(originalFile);
} catch (err) {
// eslint-disable-next-line no-console
console.error(`Failed to resolve path to [targetDir].`);
process.exit(1);
}
build(originalFile, targetDirValue, derefedFile);