api.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.bundleSwift = exports.toSwift = exports.bundleKotlin = exports.toKotlin = exports.parse = void 0;
  4. const path_1 = require("path");
  5. const utils_1 = require("./utils");
  6. const bindingsOverride = process.env['UTS_BINARY_PATH'];
  7. const bindings = !!bindingsOverride
  8. ? require((0, path_1.resolve)(bindingsOverride))
  9. : require('./binding').default;
  10. function resolveOptions(options) {
  11. const { input, output } = options;
  12. if (!input?.root) {
  13. return;
  14. }
  15. if (!input?.filename) {
  16. return;
  17. }
  18. if (!output?.outDir) {
  19. return;
  20. }
  21. if (output.sourceMap === true) {
  22. output.sourceMap = output.outDir;
  23. }
  24. else if (output.sourceMap === false ||
  25. typeof output.sourceMap === 'undefined') {
  26. output.sourceMap = '';
  27. }
  28. if (!output.imports) {
  29. // TODO
  30. output.imports = [];
  31. }
  32. input.root = (0, utils_1.normalizePath)(input.root);
  33. input.filename = (0, utils_1.normalizePath)(input.filename);
  34. output.outDir = (0, utils_1.normalizePath)(output.outDir);
  35. output.sourceMap = (0, utils_1.normalizePath)(output.sourceMap);
  36. output.logFilename = !!output.logFilename;
  37. output.noColor = !!output.noColor;
  38. return options;
  39. }
  40. function parse(source, options = {}) {
  41. options.noColor = !!options.noColor;
  42. return bindings
  43. .parse(source, toBuffer(options))
  44. .then((res) => JSON.parse(res));
  45. }
  46. exports.parse = parse;
  47. function toKotlin(options) {
  48. const kotlinOptions = resolveOptions(options);
  49. if (!kotlinOptions) {
  50. return Promise.resolve({});
  51. }
  52. return bindings
  53. .toKotlin(toBuffer(kotlinOptions))
  54. .then((res) => JSON.parse(res));
  55. }
  56. exports.toKotlin = toKotlin;
  57. function bundleKotlin(options) {
  58. const bundleOptions = resolveOptions(options);
  59. if (!bundleOptions) {
  60. return Promise.resolve({});
  61. }
  62. return bindings
  63. .bundleKotlin(toBuffer(bundleOptions))
  64. .then((res) => JSON.parse(res))
  65. .catch((error) => {
  66. return { error };
  67. });
  68. }
  69. exports.bundleKotlin = bundleKotlin;
  70. function toSwift(options) {
  71. const swiftOptions = resolveOptions(options);
  72. if (!swiftOptions) {
  73. return Promise.resolve({});
  74. }
  75. return bindings
  76. .toSwift(toBuffer(swiftOptions))
  77. .then((res) => JSON.parse(res))
  78. .catch((error) => {
  79. return { error };
  80. });
  81. }
  82. exports.toSwift = toSwift;
  83. function bundleSwift(options) {
  84. const bundleOptions = resolveOptions(options);
  85. if (!bundleOptions) {
  86. return Promise.resolve({});
  87. }
  88. return bindings
  89. .bundleSwift(toBuffer(bundleOptions))
  90. .then((res) => JSON.parse(res));
  91. }
  92. exports.bundleSwift = bundleSwift;
  93. function toBuffer(t) {
  94. // @ts-ignore 'Buffer' only refers to a type, but is being used as a value here
  95. return Buffer.from(JSON.stringify(t));
  96. }