importer.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  14. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  15. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  16. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  17. return c > 3 && r && Object.defineProperty(target, key, r), r;
  18. };
  19. var __read = (this && this.__read) || function (o, n) {
  20. var m = typeof Symbol === "function" && o[Symbol.iterator];
  21. if (!m) return o;
  22. var i = m.call(o), r, ar = [], e;
  23. try {
  24. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  25. }
  26. catch (error) { e = { error: error }; }
  27. finally {
  28. try {
  29. if (r && !r.done && (m = i["return"])) m.call(i);
  30. }
  31. finally { if (e) throw e.error; }
  32. }
  33. return ar;
  34. };
  35. var __spread = (this && this.__spread) || function () {
  36. for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
  37. return ar;
  38. };
  39. Object.defineProperty(exports, "__esModule", { value: true });
  40. var logger_1 = require("./logger");
  41. var memoize_1 = require("./memoize");
  42. var messages_1 = require("./messages");
  43. var version_checkers_1 = require("./version-checkers");
  44. var logger = logger_1.rootLogger.child({ namespace: 'Importer' });
  45. var passThru = function (action) { return function (input) {
  46. action();
  47. return input;
  48. }; };
  49. var Importer = (function () {
  50. function Importer(_patches) {
  51. if (_patches === void 0) { _patches = {}; }
  52. this._patches = _patches;
  53. }
  54. Object.defineProperty(Importer, "instance", {
  55. get: function () {
  56. logger.debug('creating Importer singleton');
  57. return new Importer({
  58. '@babel/core': [passThru(version_checkers_1.VersionCheckers.babelCore.warn)],
  59. 'babel-jest': [passThru(version_checkers_1.VersionCheckers.babelJest.warn)],
  60. typescript: [passThru(version_checkers_1.VersionCheckers.typescript.warn)],
  61. jest: [passThru(version_checkers_1.VersionCheckers.jest.warn)],
  62. });
  63. },
  64. enumerable: true,
  65. configurable: true
  66. });
  67. Importer.prototype.babelJest = function (why) {
  68. return this._import(why, 'babel-jest');
  69. };
  70. Importer.prototype.babelCore = function (why) {
  71. return this._import(why, '@babel/core');
  72. };
  73. Importer.prototype.typescript = function (why, which) {
  74. return this._import(why, which);
  75. };
  76. Importer.prototype.tryThese = function (moduleName) {
  77. var fallbacks = [];
  78. for (var _i = 1; _i < arguments.length; _i++) {
  79. fallbacks[_i - 1] = arguments[_i];
  80. }
  81. var name;
  82. var loaded;
  83. var tries = __spread([moduleName], fallbacks);
  84. while ((name = tries.shift()) !== undefined) {
  85. var req = requireWrapper(name);
  86. var contextReq = __assign({}, req);
  87. delete contextReq.exports;
  88. if (req.exists) {
  89. loaded = req;
  90. if (loaded.error) {
  91. logger.error({ requireResult: contextReq }, "failed loading module '" + name + "'", loaded.error.message);
  92. }
  93. else {
  94. logger.debug({ requireResult: contextReq }, 'loaded module', name);
  95. loaded.exports = this._patch(name, loaded.exports);
  96. }
  97. break;
  98. }
  99. else {
  100. logger.debug({ requireResult: contextReq }, "module '" + name + "' not found");
  101. continue;
  102. }
  103. }
  104. return loaded;
  105. };
  106. Importer.prototype.tryTheseOr = function (moduleNames, missingResult, allowLoadError) {
  107. if (allowLoadError === void 0) { allowLoadError = false; }
  108. var args = Array.isArray(moduleNames) ? moduleNames : [moduleNames];
  109. var result = this.tryThese.apply(this, __spread(args));
  110. if (!result)
  111. return missingResult;
  112. if (!result.error)
  113. return result.exports;
  114. if (allowLoadError)
  115. return missingResult;
  116. throw result.error;
  117. };
  118. Importer.prototype._patch = function (name, unpatched) {
  119. if (name in this._patches) {
  120. logger.debug('patching', name);
  121. return this._patches[name].reduce(function (mod, patcher) { return patcher(mod); }, unpatched);
  122. }
  123. return unpatched;
  124. };
  125. Importer.prototype._import = function (why, moduleName, _a) {
  126. var _b = _a === void 0 ? {} : _a, _c = _b.alternatives, alternatives = _c === void 0 ? [] : _c, _d = _b.installTip, installTip = _d === void 0 ? moduleName : _d;
  127. var res = this.tryThese.apply(this, __spread([moduleName], alternatives));
  128. if (res && res.exists) {
  129. if (!res.error)
  130. return res.exports;
  131. throw new Error(messages_1.interpolate(messages_1.Errors.LoadingModuleFailed, { module: res.given, error: res.error.message }));
  132. }
  133. var msg = alternatives.length ? messages_1.Errors.UnableToLoadAnyModule : messages_1.Errors.UnableToLoadOneModule;
  134. var loadModule = __spread([moduleName], alternatives).map(function (m) { return "\"" + m + "\""; }).join(', ');
  135. if (typeof installTip === 'string') {
  136. installTip = [{ module: installTip, label: "install \"" + installTip + "\"" }];
  137. }
  138. var fix = installTip
  139. .map(function (tip) {
  140. return " " + (installTip.length === 1 ? '↳' : '•') + " " + messages_1.interpolate(messages_1.Helps.FixMissingModule, tip);
  141. })
  142. .join('\n');
  143. throw new Error(messages_1.interpolate(msg, {
  144. module: loadModule,
  145. reason: why,
  146. fix: fix,
  147. }));
  148. };
  149. __decorate([
  150. memoize_1.Memoize(function () {
  151. var args = [];
  152. for (var _i = 0; _i < arguments.length; _i++) {
  153. args[_i] = arguments[_i];
  154. }
  155. return args.join(':');
  156. })
  157. ], Importer.prototype, "tryThese", null);
  158. __decorate([
  159. memoize_1.Memoize(function (name) { return name; })
  160. ], Importer.prototype, "_patch", null);
  161. __decorate([
  162. memoize_1.Memoize()
  163. ], Importer, "instance", null);
  164. return Importer;
  165. }());
  166. exports.Importer = Importer;
  167. exports.importer = Importer.instance;
  168. function requireWrapper(moduleName) {
  169. var path;
  170. var exists = false;
  171. try {
  172. path = resolveModule(moduleName);
  173. exists = true;
  174. }
  175. catch (error) {
  176. return { error: error, exists: exists, given: moduleName };
  177. }
  178. var result = { exists: exists, path: path, given: moduleName };
  179. try {
  180. result.exports = requireModule(moduleName);
  181. }
  182. catch (error) {
  183. result.error = error;
  184. }
  185. return result;
  186. }
  187. var requireModule = function (mod) { return require(mod); };
  188. var resolveModule = function (mod) { return require.resolve(mod); };
  189. function __requireModule(localRequire, localResolve) {
  190. requireModule = localRequire;
  191. resolveModule = localResolve;
  192. }
  193. exports.__requireModule = __requireModule;