transform-file.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.transformFileSync = transformFileSync;
  6. exports.transformFileAsync = transformFileAsync;
  7. exports.transformFile = void 0;
  8. function _fs() {
  9. const data = _interopRequireDefault(require("fs"));
  10. _fs = function () {
  11. return data;
  12. };
  13. return data;
  14. }
  15. var _config = _interopRequireDefault(require("./config"));
  16. var _transformation = require("./transformation");
  17. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  18. ({});
  19. const transformFile = function transformFile(filename, opts, callback) {
  20. let options;
  21. if (typeof opts === "function") {
  22. callback = opts;
  23. opts = undefined;
  24. }
  25. if (opts == null) {
  26. options = {
  27. filename
  28. };
  29. } else if (opts && typeof opts === "object") {
  30. options = Object.assign({}, opts, {
  31. filename
  32. });
  33. }
  34. process.nextTick(() => {
  35. let cfg;
  36. try {
  37. cfg = (0, _config.default)(options);
  38. if (cfg === null) return callback(null, null);
  39. } catch (err) {
  40. return callback(err);
  41. }
  42. const config = cfg;
  43. _fs().default.readFile(filename, "utf8", function (err, code) {
  44. if (err) return callback(err, null);
  45. (0, _transformation.runAsync)(config, code, null, callback);
  46. });
  47. });
  48. };
  49. exports.transformFile = transformFile;
  50. function transformFileSync(filename, opts) {
  51. let options;
  52. if (opts == null) {
  53. options = {
  54. filename
  55. };
  56. } else if (opts && typeof opts === "object") {
  57. options = Object.assign({}, opts, {
  58. filename
  59. });
  60. }
  61. const config = (0, _config.default)(options);
  62. if (config === null) return null;
  63. return (0, _transformation.runSync)(config, _fs().default.readFileSync(filename, "utf8"));
  64. }
  65. function transformFileAsync(filename, opts) {
  66. return new Promise((res, rej) => {
  67. transformFile(filename, opts, (err, result) => {
  68. if (err == null) res(result);else rej(err);
  69. });
  70. });
  71. }