util.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.addSourceMappingUrl = addSourceMappingUrl;
  6. exports.alphasort = alphasort;
  7. exports.chmod = chmod;
  8. exports.compile = compile;
  9. exports.debounce = debounce;
  10. exports.deleteDir = deleteDir;
  11. exports.hasDataSourcemap = hasDataSourcemap;
  12. exports.isCompilableExtension = isCompilableExtension;
  13. exports.readdir = readdir;
  14. exports.readdirForCompilable = readdirForCompilable;
  15. exports.transformRepl = transformRepl;
  16. exports.withExtension = withExtension;
  17. function _fsReaddirRecursive() {
  18. const data = require("fs-readdir-recursive");
  19. _fsReaddirRecursive = function () {
  20. return data;
  21. };
  22. return data;
  23. }
  24. function babel() {
  25. const data = require("@babel/core");
  26. babel = function () {
  27. return data;
  28. };
  29. return data;
  30. }
  31. function _path() {
  32. const data = require("path");
  33. _path = function () {
  34. return data;
  35. };
  36. return data;
  37. }
  38. function _fs() {
  39. const data = require("fs");
  40. _fs = function () {
  41. return data;
  42. };
  43. return data;
  44. }
  45. var watcher = require("./watcher.js");
  46. function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
  47. function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
  48. function chmod(src, dest) {
  49. try {
  50. _fs().chmodSync(dest, _fs().statSync(src).mode);
  51. } catch (_) {
  52. console.warn(`Cannot change permissions of ${dest}`);
  53. }
  54. }
  55. function alphasort(a, b) {
  56. return a.localeCompare(b, "en");
  57. }
  58. function readdir(dirname, includeDotfiles, filter) {
  59. {
  60. return _fsReaddirRecursive()("", (filename, index, currentDirectory) => {
  61. const stat = _fs().statSync(_path().join(currentDirectory, filename));
  62. if (stat.isDirectory()) return true;
  63. return (includeDotfiles || filename[0] !== ".") && (!filter || filter(filename));
  64. }, [], dirname);
  65. }
  66. }
  67. function readdirForCompilable(dirname, includeDotfiles, altExts) {
  68. return readdir(dirname, includeDotfiles, function (filename) {
  69. return isCompilableExtension(filename, altExts);
  70. });
  71. }
  72. function isCompilableExtension(filename, altExts) {
  73. const exts = altExts || babel().DEFAULT_EXTENSIONS;
  74. const ext = _path().extname(filename);
  75. return exts.includes(ext);
  76. }
  77. function addSourceMappingUrl(code, loc) {
  78. return code + "\n//# sourceMappingURL=" + _path().basename(loc);
  79. }
  80. function hasDataSourcemap(code) {
  81. const pos = code.lastIndexOf("\n", code.length - 2);
  82. return pos !== -1 && code.lastIndexOf("//# sourceMappingURL") < pos;
  83. }
  84. const CALLER = {
  85. name: "@babel/cli",
  86. supportsStaticESM: false,
  87. supportsDynamicImport: false,
  88. supportsExportNamespaceFrom: false
  89. };
  90. function transformRepl(filename, code, opts) {
  91. opts = Object.assign({}, opts, {
  92. sourceMaps: opts.sourceMaps === "inline" ? true : opts.sourceMaps,
  93. caller: CALLER,
  94. filename
  95. });
  96. return new Promise((resolve, reject) => {
  97. babel().transform(code, opts, (err, result) => {
  98. if (err) reject(err);else resolve(result);
  99. });
  100. });
  101. }
  102. function compile(_x, _x2) {
  103. return _compile.apply(this, arguments);
  104. }
  105. function _compile() {
  106. _compile = _asyncToGenerator(function* (filename, opts) {
  107. opts = Object.assign({}, opts, {
  108. caller: CALLER
  109. });
  110. const result = yield new Promise((resolve, reject) => {
  111. babel().transformFile(filename, opts, (err, result) => {
  112. if (err) reject(err);else resolve(result);
  113. });
  114. });
  115. if (result) {
  116. {
  117. if (!result.externalDependencies) return result;
  118. }
  119. watcher.updateExternalDependencies(filename, result.externalDependencies);
  120. }
  121. return result;
  122. });
  123. return _compile.apply(this, arguments);
  124. }
  125. function deleteDir(path) {
  126. (_fs().rmSync || function d(p) {
  127. if (_fs().existsSync(p)) {
  128. _fs().readdirSync(p).forEach(function (f) {
  129. const c = p + "/" + f;
  130. if (_fs().lstatSync(c).isDirectory()) {
  131. d(c);
  132. } else {
  133. _fs().unlinkSync(c);
  134. }
  135. });
  136. _fs().rmdirSync(p);
  137. }
  138. })(path, {
  139. force: true,
  140. recursive: true
  141. });
  142. }
  143. process.on("uncaughtException", function (err) {
  144. console.error(err);
  145. process.exitCode = 1;
  146. });
  147. function withExtension(filename, ext = ".js") {
  148. const newBasename = _path().basename(filename, _path().extname(filename)) + ext;
  149. return _path().join(_path().dirname(filename), newBasename);
  150. }
  151. function debounce(fn, time) {
  152. let timer;
  153. function debounced() {
  154. clearTimeout(timer);
  155. timer = setTimeout(fn, time);
  156. }
  157. debounced.flush = () => {
  158. clearTimeout(timer);
  159. fn();
  160. };
  161. return debounced;
  162. }
  163. //# sourceMappingURL=util.js.map