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.

common.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // vendored from https://github.com/amasad/sane/blob/64ff3a870c42e84f744086884bf55a4f9c22d376/src/common.js
  2. 'use strict';
  3. const platform = require('os').platform();
  4. const path = require('path');
  5. const anymatch = require('anymatch');
  6. const micromatch = require('micromatch');
  7. const walker = require('walker');
  8. /**
  9. * Constants
  10. */
  11. exports.DEFAULT_DELAY = 100;
  12. exports.CHANGE_EVENT = 'change';
  13. exports.DELETE_EVENT = 'delete';
  14. exports.ADD_EVENT = 'add';
  15. exports.ALL_EVENT = 'all';
  16. /**
  17. * Assigns options to the watcher.
  18. *
  19. * @param {NodeWatcher|PollWatcher|WatchmanWatcher} watcher
  20. * @param {?object} opts
  21. * @return {boolean}
  22. * @public
  23. */
  24. exports.assignOptions = function (watcher, opts) {
  25. opts = opts || {};
  26. watcher.globs = opts.glob || [];
  27. watcher.dot = opts.dot || false;
  28. watcher.ignored = opts.ignored || false;
  29. if (!Array.isArray(watcher.globs)) {
  30. watcher.globs = [watcher.globs];
  31. }
  32. watcher.hasIgnore =
  33. Boolean(opts.ignored) && !(Array.isArray(opts) && opts.length > 0);
  34. watcher.doIgnore = opts.ignored ? anymatch(opts.ignored) : () => false;
  35. if (opts.watchman && opts.watchmanPath) {
  36. watcher.watchmanPath = opts.watchmanPath;
  37. }
  38. return opts;
  39. };
  40. /**
  41. * Checks a file relative path against the globs array.
  42. *
  43. * @param {array} globs
  44. * @param {string} relativePath
  45. * @return {boolean}
  46. * @public
  47. */
  48. exports.isFileIncluded = function (globs, dot, doIgnore, relativePath) {
  49. if (doIgnore(relativePath)) {
  50. return false;
  51. }
  52. return globs.length
  53. ? micromatch.some(relativePath, globs, {
  54. dot
  55. })
  56. : dot || micromatch.some(relativePath, '**/*');
  57. };
  58. /**
  59. * Traverse a directory recursively calling `callback` on every directory.
  60. *
  61. * @param {string} dir
  62. * @param {function} dirCallback
  63. * @param {function} fileCallback
  64. * @param {function} endCallback
  65. * @param {*} ignored
  66. * @public
  67. */
  68. exports.recReaddir = function (
  69. dir,
  70. dirCallback,
  71. fileCallback,
  72. endCallback,
  73. errorCallback,
  74. ignored
  75. ) {
  76. walker(dir)
  77. .filterDir(currentDir => !anymatch(ignored, currentDir))
  78. .on('dir', normalizeProxy(dirCallback))
  79. .on('file', normalizeProxy(fileCallback))
  80. .on('error', errorCallback)
  81. .on('end', () => {
  82. if (platform === 'win32') {
  83. setTimeout(endCallback, 1000);
  84. } else {
  85. endCallback();
  86. }
  87. });
  88. };
  89. /**
  90. * Returns a callback that when called will normalize a path and call the
  91. * original callback
  92. *
  93. * @param {function} callback
  94. * @return {function}
  95. * @private
  96. */
  97. function normalizeProxy(callback) {
  98. return (filepath, stats) => callback(path.normalize(filepath), stats);
  99. }