index.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* global process */
  2. /* eslint-env node */
  3. import fs from 'fs';
  4. import path from 'path';
  5. import yaml from 'js-yaml';
  6. import JsonRefs from 'json-refs';
  7. function dictToString(dict) {
  8. const res = [];
  9. for (const [k, v] of Object.entries(dict)) {
  10. res.push(`${k}: ${v}`);
  11. }
  12. return res.join('\n');
  13. }
  14. function bundle(originalFile) {
  15. const root = yaml.safeLoad(fs.readFileSync(originalFile, 'utf8'));
  16. const options = {
  17. filter: ['relative', 'remote', 'local'],
  18. resolveCirculars: true,
  19. location: originalFile,
  20. loaderOptions: {
  21. processContent: function (res, callback) {
  22. callback(undefined, yaml.safeLoad(res.text));
  23. },
  24. },
  25. };
  26. JsonRefs.clearCache();
  27. return JsonRefs.resolveRefs(root, options).then(
  28. function (results) {
  29. const resErrors = {};
  30. for (const [k, v] of Object.entries(results.refs)) {
  31. if (
  32. 'missing' in v &&
  33. v.missing === true &&
  34. (v.type === 'relative' || v.type === 'remote')
  35. ) {
  36. resErrors[k] = v.error;
  37. }
  38. }
  39. if (Object.keys(resErrors).length > 0) {
  40. return Promise.reject(new Error(dictToString(resErrors)));
  41. }
  42. return results.resolved;
  43. },
  44. function (e) {
  45. const error = {};
  46. Object.getOwnPropertyNames(e).forEach(function (key) {
  47. error[key] = e[key];
  48. });
  49. return Promise.reject(new Error(dictToString(error)));
  50. }
  51. );
  52. }
  53. function build(originalFile, _, bundleTo) {
  54. bundle(originalFile).then(
  55. function (bundled) {
  56. const bundleString = JSON.stringify(bundled, null, 2);
  57. if (typeof bundleTo === 'string') {
  58. fs.writeFile(bundleTo, bundleString, function (err) {
  59. if (err) {
  60. // biome-ignore lint/suspicious/noConsoleLog: Disable console
  61. console.log(err);
  62. return;
  63. }
  64. // biome-ignore lint/suspicious/noConsoleLog: Disable console
  65. console.log('Saved bundle file at ' + bundleTo);
  66. });
  67. }
  68. },
  69. function (err) {
  70. // biome-ignore lint/suspicious/noConsoleLog: Disable console
  71. console.log(err);
  72. }
  73. );
  74. }
  75. let originalFile;
  76. let targetDirValue;
  77. const argv = process.argv.slice(2);
  78. originalFile = argv[0];
  79. const derefedFile = argv[1] || 'tests/apidocs/openapi-derefed.json';
  80. try {
  81. if (!path.isAbsolute(originalFile)) {
  82. originalFile = path.resolve(process.cwd(), originalFile);
  83. }
  84. targetDirValue = path.dirname(originalFile);
  85. } catch (err) {
  86. // biome-ignore lint/suspicious/noConsoleLog: Disable console
  87. console.error(`Failed to resolve path to [targetDir].`);
  88. process.exit(1);
  89. }
  90. build(originalFile, targetDirValue, derefedFile);