util.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.chmod = chmod;
  6. exports.readdir = readdir;
  7. exports.readdirForCompilable = readdirForCompilable;
  8. exports.isCompilableExtension = isCompilableExtension;
  9. exports.addSourceMappingUrl = addSourceMappingUrl;
  10. exports.transform = transform;
  11. exports.compile = compile;
  12. exports.deleteDir = deleteDir;
  13. exports.requireChokidar = requireChokidar;
  14. exports.adjustRelative = adjustRelative;
  15. function _fsReaddirRecursive() {
  16. const data = _interopRequireDefault(require("fs-readdir-recursive"));
  17. _fsReaddirRecursive = function () {
  18. return data;
  19. };
  20. return data;
  21. }
  22. function babel() {
  23. const data = _interopRequireWildcard(require("@babel/core"));
  24. babel = function () {
  25. return data;
  26. };
  27. return data;
  28. }
  29. function _includes() {
  30. const data = _interopRequireDefault(require("lodash/includes"));
  31. _includes = function () {
  32. return data;
  33. };
  34. return data;
  35. }
  36. function _path() {
  37. const data = _interopRequireDefault(require("path"));
  38. _path = function () {
  39. return data;
  40. };
  41. return data;
  42. }
  43. function _fs() {
  44. const data = _interopRequireDefault(require("fs"));
  45. _fs = function () {
  46. return data;
  47. };
  48. return data;
  49. }
  50. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  51. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  52. function chmod(src, dest) {
  53. _fs().default.chmodSync(dest, _fs().default.statSync(src).mode);
  54. }
  55. function readdir(dirname, includeDotfiles, filter) {
  56. return (0, _fsReaddirRecursive().default)(dirname, (filename, _index, currentDirectory) => {
  57. const stat = _fs().default.statSync(_path().default.join(currentDirectory, filename));
  58. if (stat.isDirectory()) return true;
  59. return (includeDotfiles || filename[0] !== ".") && (!filter || filter(filename));
  60. });
  61. }
  62. function readdirForCompilable(dirname, includeDotfiles, altExts) {
  63. return readdir(dirname, includeDotfiles, function (filename) {
  64. return isCompilableExtension(filename, altExts);
  65. });
  66. }
  67. function isCompilableExtension(filename, altExts) {
  68. const exts = altExts || babel().DEFAULT_EXTENSIONS;
  69. const ext = _path().default.extname(filename);
  70. return (0, _includes().default)(exts, ext);
  71. }
  72. function addSourceMappingUrl(code, loc) {
  73. return code + "\n//# sourceMappingURL=" + _path().default.basename(loc);
  74. }
  75. const CALLER = {
  76. name: "@babel/cli"
  77. };
  78. function transform(filename, code, opts) {
  79. opts = Object.assign({}, opts, {
  80. caller: CALLER,
  81. filename
  82. });
  83. return new Promise((resolve, reject) => {
  84. babel().transform(code, opts, (err, result) => {
  85. if (err) reject(err);else resolve(result);
  86. });
  87. });
  88. }
  89. function compile(filename, opts) {
  90. opts = Object.assign({}, opts, {
  91. caller: CALLER
  92. });
  93. return new Promise((resolve, reject) => {
  94. babel().transformFile(filename, opts, (err, result) => {
  95. if (err) reject(err);else resolve(result);
  96. });
  97. });
  98. }
  99. function deleteDir(path) {
  100. if (_fs().default.existsSync(path)) {
  101. _fs().default.readdirSync(path).forEach(function (file) {
  102. const curPath = path + "/" + file;
  103. if (_fs().default.lstatSync(curPath).isDirectory()) {
  104. deleteDir(curPath);
  105. } else {
  106. _fs().default.unlinkSync(curPath);
  107. }
  108. });
  109. _fs().default.rmdirSync(path);
  110. }
  111. }
  112. process.on("uncaughtException", function (err) {
  113. console.error(err);
  114. process.exit(1);
  115. });
  116. function requireChokidar() {
  117. try {
  118. return require("chokidar");
  119. } catch (err) {
  120. console.error("The optional dependency chokidar failed to install and is required for " + "--watch. Chokidar is likely not supported on your platform.");
  121. throw err;
  122. }
  123. }
  124. function adjustRelative(relative, keepFileExtension) {
  125. if (keepFileExtension) {
  126. return relative;
  127. }
  128. return relative.replace(/\.(\w*?)$/, "") + ".js";
  129. }