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 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. const path = require('path');
  3. const js = require('default-require-extensions/js');
  4. module.exports = appendTransform;
  5. let count = 0;
  6. // eslint-disable-next-line node/no-deprecated-api
  7. function appendTransform(transform, ext = '.js', extensions = require.extensions) {
  8. // Generate a unique key for this transform
  9. const key = path.join(__dirname, count.toString());
  10. count++;
  11. let forwardGet;
  12. let forwardSet;
  13. const descriptor = Object.getOwnPropertyDescriptor(extensions, ext) || {value: js, configurable: true};
  14. if (
  15. ((descriptor.get || descriptor.set) && !(descriptor.get && descriptor.set)) ||
  16. !descriptor.configurable
  17. ) {
  18. throw new Error('Somebody did bad things to require.extensions["' + ext + '"]');
  19. }
  20. if (descriptor.get) {
  21. // Wrap a previous append-transform install and pass through to the getter/setter pair it created
  22. forwardGet = function () {
  23. return descriptor.get();
  24. };
  25. forwardSet = function (val) {
  26. descriptor.set(val);
  27. return forwardGet();
  28. };
  29. } else {
  30. forwardGet = function () {
  31. return descriptor.value;
  32. };
  33. forwardSet = function (val) {
  34. descriptor.value = val;
  35. return val;
  36. };
  37. }
  38. function wrapCustomHook(hook) {
  39. return function (module, filename) {
  40. // We wrap every added extension, but we only apply the transform to the one on top of the stack
  41. if (!module[key]) {
  42. module[key] = true;
  43. const originalCompile = module._compile;
  44. // eslint-disable-next-line func-name-matching, func-names
  45. module._compile = function replacementCompile(code, filename) {
  46. module._compile = originalCompile;
  47. code = transform(code, filename);
  48. module._compile(code, filename);
  49. };
  50. }
  51. hook(module, filename);
  52. };
  53. }
  54. // Wrap the original
  55. forwardSet(wrapCustomHook(forwardGet()));
  56. const hooks = [forwardGet()];
  57. function setCurrentHook(hook) {
  58. const restoreIndex = hooks.indexOf(hook);
  59. if (restoreIndex === -1) {
  60. hooks.push(forwardSet(wrapCustomHook(hook)));
  61. } else {
  62. // We have already seen this hook, and it is being reverted (proxyquire, etc) - don't wrap again.
  63. hooks.splice(restoreIndex + 1, hooks.length);
  64. forwardSet(hook);
  65. }
  66. }
  67. Object.defineProperty(extensions, ext, {
  68. configurable: true,
  69. enumerable: true,
  70. get: forwardGet,
  71. set: setCurrentHook
  72. });
  73. }