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.

node-loaders.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* eslint-disable no-console */
  2. 'use strict';
  3. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
  4. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  5. var fs = require('fs');
  6. var path = require('path');
  7. var Loader = require('./loader');
  8. var _require = require('./precompiled-loader.js'),
  9. PrecompiledLoader = _require.PrecompiledLoader;
  10. var chokidar;
  11. var FileSystemLoader = /*#__PURE__*/function (_Loader) {
  12. _inheritsLoose(FileSystemLoader, _Loader);
  13. function FileSystemLoader(searchPaths, opts) {
  14. var _this;
  15. _this = _Loader.call(this) || this;
  16. if (typeof opts === 'boolean') {
  17. console.log('[nunjucks] Warning: you passed a boolean as the second ' + 'argument to FileSystemLoader, but it now takes an options ' + 'object. See http://mozilla.github.io/nunjucks/api.html#filesystemloader');
  18. }
  19. opts = opts || {};
  20. _this.pathsToNames = {};
  21. _this.noCache = !!opts.noCache;
  22. if (searchPaths) {
  23. searchPaths = Array.isArray(searchPaths) ? searchPaths : [searchPaths]; // For windows, convert to forward slashes
  24. _this.searchPaths = searchPaths.map(path.normalize);
  25. } else {
  26. _this.searchPaths = ['.'];
  27. }
  28. if (opts.watch) {
  29. // Watch all the templates in the paths and fire an event when
  30. // they change
  31. try {
  32. chokidar = require('chokidar'); // eslint-disable-line global-require
  33. } catch (e) {
  34. throw new Error('watch requires chokidar to be installed');
  35. }
  36. var paths = _this.searchPaths.filter(fs.existsSync);
  37. var watcher = chokidar.watch(paths);
  38. watcher.on('all', function (event, fullname) {
  39. fullname = path.resolve(fullname);
  40. if (event === 'change' && fullname in _this.pathsToNames) {
  41. _this.emit('update', _this.pathsToNames[fullname], fullname);
  42. }
  43. });
  44. watcher.on('error', function (error) {
  45. console.log('Watcher error: ' + error);
  46. });
  47. }
  48. return _this;
  49. }
  50. var _proto = FileSystemLoader.prototype;
  51. _proto.getSource = function getSource(name) {
  52. var fullpath = null;
  53. var paths = this.searchPaths;
  54. for (var i = 0; i < paths.length; i++) {
  55. var basePath = path.resolve(paths[i]);
  56. var p = path.resolve(paths[i], name); // Only allow the current directory and anything
  57. // underneath it to be searched
  58. if (p.indexOf(basePath) === 0 && fs.existsSync(p)) {
  59. fullpath = p;
  60. break;
  61. }
  62. }
  63. if (!fullpath) {
  64. return null;
  65. }
  66. this.pathsToNames[fullpath] = name;
  67. var source = {
  68. src: fs.readFileSync(fullpath, 'utf-8'),
  69. path: fullpath,
  70. noCache: this.noCache
  71. };
  72. this.emit('load', name, source);
  73. return source;
  74. };
  75. return FileSystemLoader;
  76. }(Loader);
  77. var NodeResolveLoader = /*#__PURE__*/function (_Loader2) {
  78. _inheritsLoose(NodeResolveLoader, _Loader2);
  79. function NodeResolveLoader(opts) {
  80. var _this2;
  81. _this2 = _Loader2.call(this) || this;
  82. opts = opts || {};
  83. _this2.pathsToNames = {};
  84. _this2.noCache = !!opts.noCache;
  85. if (opts.watch) {
  86. try {
  87. chokidar = require('chokidar'); // eslint-disable-line global-require
  88. } catch (e) {
  89. throw new Error('watch requires chokidar to be installed');
  90. }
  91. _this2.watcher = chokidar.watch();
  92. _this2.watcher.on('change', function (fullname) {
  93. _this2.emit('update', _this2.pathsToNames[fullname], fullname);
  94. });
  95. _this2.watcher.on('error', function (error) {
  96. console.log('Watcher error: ' + error);
  97. });
  98. _this2.on('load', function (name, source) {
  99. _this2.watcher.add(source.path);
  100. });
  101. }
  102. return _this2;
  103. }
  104. var _proto2 = NodeResolveLoader.prototype;
  105. _proto2.getSource = function getSource(name) {
  106. // Don't allow file-system traversal
  107. if (/^\.?\.?(\/|\\)/.test(name)) {
  108. return null;
  109. }
  110. if (/^[A-Z]:/.test(name)) {
  111. return null;
  112. }
  113. var fullpath;
  114. try {
  115. fullpath = require.resolve(name);
  116. } catch (e) {
  117. return null;
  118. }
  119. this.pathsToNames[fullpath] = name;
  120. var source = {
  121. src: fs.readFileSync(fullpath, 'utf-8'),
  122. path: fullpath,
  123. noCache: this.noCache
  124. };
  125. this.emit('load', name, source);
  126. return source;
  127. };
  128. return NodeResolveLoader;
  129. }(Loader);
  130. module.exports = {
  131. FileSystemLoader: FileSystemLoader,
  132. PrecompiledLoader: PrecompiledLoader,
  133. NodeResolveLoader: NodeResolveLoader
  134. };