Files
2024-02-15 17:01:50 +00:00

102 lines
2.6 KiB
TypeScript

/* global process */
/* eslint-env node */
/* eslint import/no-unresolved:0 */
import fs from 'node:fs';
import path from 'node:path';
import yaml from 'js-yaml';
import JsonRefs from 'json-refs';
function dictToString(dict) {
const res = [];
for (const [k, v] of Object.entries(dict)) {
res.push(`${k}: ${v}`);
}
return res.join('\n');
}
function bundle(originalFile) {
const root = yaml.safeLoad(fs.readFileSync(originalFile, 'utf8'));
const options = {
filter: ['relative', 'remote', 'local'],
resolveCirculars: true,
location: originalFile,
loaderOptions: {
processContent: function (res, callback) {
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;
let targetDirValue;
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);