Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.addHook = addHook;
  6. var _module = _interopRequireDefault(require("module"));
  7. var _path = _interopRequireDefault(require("path"));
  8. var _nodeModulesRegexp = _interopRequireDefault(require("node-modules-regexp"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. /* (c) 2015 Ari Porad (@ariporad) <http://ariporad.com>. License: ariporad.mit-license.org */
  11. // Guard against poorly mocked module constructors.
  12. const Module = module.constructor.length > 1 ? module.constructor : _module.default;
  13. const HOOK_RETURNED_NOTHING_ERROR_MESSAGE = '[Pirates] A hook returned a non-string, or nothing at all! This is a' + ' violation of intergalactic law!\n' + '--------------------\n' + 'If you have no idea what this means or what Pirates is, let me explain: ' + 'Pirates is a module that makes is easy to implement require hooks. One of' + " the require hooks you're using uses it. One of these require hooks" + " didn't return anything from it's handler, so we don't know what to" + ' do. You might want to debug this.';
  14. function shouldCompile(filename, exts, matcher, ignoreNodeModules) {
  15. if (typeof filename !== 'string') {
  16. return false;
  17. }
  18. if (exts.indexOf(_path.default.extname(filename)) === -1) {
  19. return false;
  20. }
  21. const resolvedFilename = _path.default.resolve(filename);
  22. if (ignoreNodeModules && _nodeModulesRegexp.default.test(resolvedFilename)) {
  23. return false;
  24. }
  25. if (matcher && typeof matcher === 'function') {
  26. return !!matcher(resolvedFilename);
  27. }
  28. return true;
  29. }
  30. /**
  31. * Add a require hook.
  32. *
  33. * @param {Function} hook - The hook. Accepts the code of the module and the filename. Required.
  34. * @param {Object} [opts] - Options
  35. * @param {String[]} [opts.exts=['.js']] - The extensions to hook. Should start with '.' (ex. ['.js']).
  36. * @param {Function(path)} [opts.matcher] - A matcher function, will be called with path to a file. Should return truthy if the file should be hooked, falsy otherwise.
  37. * @param {Boolean} [opts.ignoreNodeModules=true] - Auto-ignore node_modules. Independent of any matcher.
  38. * @returns {Function} revert - Reverts the hooks.
  39. */
  40. function addHook(hook, opts = {}) {
  41. let reverted = false;
  42. const loaders = [];
  43. const oldLoaders = [];
  44. let exts; // We need to do this to fix #15. Basically, if you use a non-standard extension (ie. .jsx), then
  45. // We modify the .js loader, then use the modified .js loader for as the base for .jsx.
  46. // This prevents that.
  47. const originalJSLoader = Module._extensions['.js'];
  48. const matcher = opts.matcher || null;
  49. const ignoreNodeModules = opts.ignoreNodeModules !== false;
  50. exts = opts.extensions || opts.exts || opts.extension || opts.ext || ['.js'];
  51. if (!Array.isArray(exts)) {
  52. exts = [exts];
  53. }
  54. exts.forEach(ext => {
  55. if (typeof ext !== 'string') {
  56. throw new TypeError(`Invalid Extension: ${ext}`);
  57. }
  58. const oldLoader = Module._extensions[ext] || originalJSLoader;
  59. oldLoaders[ext] = oldLoader;
  60. loaders[ext] = Module._extensions[ext] = function newLoader(mod, filename) {
  61. let compile;
  62. if (!reverted) {
  63. if (shouldCompile(filename, exts, matcher, ignoreNodeModules)) {
  64. compile = mod._compile;
  65. mod._compile = function _compile(code) {
  66. // reset the compile immediately as otherwise we end up having the
  67. // compile function being changed even though this loader might be reverted
  68. // Not reverting it here leads to long useless compile chains when doing
  69. // addHook -> revert -> addHook -> revert -> ...
  70. // The compile function is also anyway created new when the loader is called a second time.
  71. mod._compile = compile;
  72. const newCode = hook(code, filename);
  73. if (typeof newCode !== 'string') {
  74. throw new Error(HOOK_RETURNED_NOTHING_ERROR_MESSAGE);
  75. }
  76. return mod._compile(newCode, filename);
  77. };
  78. }
  79. }
  80. oldLoader(mod, filename);
  81. };
  82. });
  83. return function revert() {
  84. if (reverted) return;
  85. reverted = true;
  86. exts.forEach(ext => {
  87. // if the current loader for the extension is our loader then unregister it and set the oldLoader again
  88. // if not we can not do anything as we cannot remove a loader from within the loader-chain
  89. if (Module._extensions[ext] === loaders[ext]) {
  90. Module._extensions[ext] = oldLoaders[ext];
  91. }
  92. });
  93. };
  94. }