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.

FSEventsWatcher.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. 'use strict';
  2. function _events() {
  3. const data = require('events');
  4. _events = function () {
  5. return data;
  6. };
  7. return data;
  8. }
  9. function path() {
  10. const data = _interopRequireWildcard(require('path'));
  11. path = function () {
  12. return data;
  13. };
  14. return data;
  15. }
  16. function _anymatch() {
  17. const data = _interopRequireDefault(require('anymatch'));
  18. _anymatch = function () {
  19. return data;
  20. };
  21. return data;
  22. }
  23. function fs() {
  24. const data = _interopRequireWildcard(require('graceful-fs'));
  25. fs = function () {
  26. return data;
  27. };
  28. return data;
  29. }
  30. function _micromatch() {
  31. const data = _interopRequireDefault(require('micromatch'));
  32. _micromatch = function () {
  33. return data;
  34. };
  35. return data;
  36. }
  37. function _walker() {
  38. const data = _interopRequireDefault(require('walker'));
  39. _walker = function () {
  40. return data;
  41. };
  42. return data;
  43. }
  44. function _interopRequireDefault(obj) {
  45. return obj && obj.__esModule ? obj : {default: obj};
  46. }
  47. function _getRequireWildcardCache(nodeInterop) {
  48. if (typeof WeakMap !== 'function') return null;
  49. var cacheBabelInterop = new WeakMap();
  50. var cacheNodeInterop = new WeakMap();
  51. return (_getRequireWildcardCache = function (nodeInterop) {
  52. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  53. })(nodeInterop);
  54. }
  55. function _interopRequireWildcard(obj, nodeInterop) {
  56. if (!nodeInterop && obj && obj.__esModule) {
  57. return obj;
  58. }
  59. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  60. return {default: obj};
  61. }
  62. var cache = _getRequireWildcardCache(nodeInterop);
  63. if (cache && cache.has(obj)) {
  64. return cache.get(obj);
  65. }
  66. var newObj = {};
  67. var hasPropertyDescriptor =
  68. Object.defineProperty && Object.getOwnPropertyDescriptor;
  69. for (var key in obj) {
  70. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  71. var desc = hasPropertyDescriptor
  72. ? Object.getOwnPropertyDescriptor(obj, key)
  73. : null;
  74. if (desc && (desc.get || desc.set)) {
  75. Object.defineProperty(newObj, key, desc);
  76. } else {
  77. newObj[key] = obj[key];
  78. }
  79. }
  80. }
  81. newObj.default = obj;
  82. if (cache) {
  83. cache.set(obj, newObj);
  84. }
  85. return newObj;
  86. }
  87. function _defineProperty(obj, key, value) {
  88. if (key in obj) {
  89. Object.defineProperty(obj, key, {
  90. value: value,
  91. enumerable: true,
  92. configurable: true,
  93. writable: true
  94. });
  95. } else {
  96. obj[key] = value;
  97. }
  98. return obj;
  99. }
  100. // eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
  101. // @ts-ignore: this is for CI which runs linux and might not have this
  102. let fsevents = null;
  103. try {
  104. fsevents = require('fsevents');
  105. } catch {
  106. // Optional dependency, only supported on Darwin.
  107. }
  108. const CHANGE_EVENT = 'change';
  109. const DELETE_EVENT = 'delete';
  110. const ADD_EVENT = 'add';
  111. const ALL_EVENT = 'all';
  112. /**
  113. * Export `FSEventsWatcher` class.
  114. * Watches `dir`.
  115. */
  116. class FSEventsWatcher extends _events().EventEmitter {
  117. static isSupported() {
  118. return fsevents !== null;
  119. }
  120. static normalizeProxy(callback) {
  121. return (filepath, stats) => callback(path().normalize(filepath), stats);
  122. }
  123. static recReaddir(
  124. dir,
  125. dirCallback,
  126. fileCallback,
  127. endCallback,
  128. errorCallback,
  129. ignored
  130. ) {
  131. (0, _walker().default)(dir)
  132. .filterDir(
  133. currentDir => !ignored || !(0, _anymatch().default)(ignored, currentDir)
  134. )
  135. .on('dir', FSEventsWatcher.normalizeProxy(dirCallback))
  136. .on('file', FSEventsWatcher.normalizeProxy(fileCallback))
  137. .on('error', errorCallback)
  138. .on('end', () => {
  139. endCallback();
  140. });
  141. }
  142. constructor(dir, opts) {
  143. if (!fsevents) {
  144. throw new Error(
  145. '`fsevents` unavailable (this watcher can only be used on Darwin)'
  146. );
  147. }
  148. super();
  149. _defineProperty(this, 'root', void 0);
  150. _defineProperty(this, 'ignored', void 0);
  151. _defineProperty(this, 'glob', void 0);
  152. _defineProperty(this, 'dot', void 0);
  153. _defineProperty(this, 'hasIgnore', void 0);
  154. _defineProperty(this, 'doIgnore', void 0);
  155. _defineProperty(this, 'fsEventsWatchStopper', void 0);
  156. _defineProperty(this, '_tracked', void 0);
  157. this.dot = opts.dot || false;
  158. this.ignored = opts.ignored;
  159. this.glob = Array.isArray(opts.glob) ? opts.glob : [opts.glob];
  160. this.hasIgnore =
  161. Boolean(opts.ignored) && !(Array.isArray(opts) && opts.length > 0);
  162. this.doIgnore = opts.ignored
  163. ? (0, _anymatch().default)(opts.ignored)
  164. : () => false;
  165. this.root = path().resolve(dir);
  166. this.fsEventsWatchStopper = fsevents.watch(
  167. this.root,
  168. this.handleEvent.bind(this)
  169. );
  170. this._tracked = new Set();
  171. FSEventsWatcher.recReaddir(
  172. this.root,
  173. filepath => {
  174. this._tracked.add(filepath);
  175. },
  176. filepath => {
  177. this._tracked.add(filepath);
  178. },
  179. this.emit.bind(this, 'ready'),
  180. this.emit.bind(this, 'error'),
  181. this.ignored
  182. );
  183. }
  184. /**
  185. * End watching.
  186. */
  187. async close(callback) {
  188. await this.fsEventsWatchStopper();
  189. this.removeAllListeners();
  190. if (typeof callback === 'function') {
  191. process.nextTick(callback.bind(null, null, true));
  192. }
  193. }
  194. isFileIncluded(relativePath) {
  195. if (this.doIgnore(relativePath)) {
  196. return false;
  197. }
  198. return this.glob.length
  199. ? (0, _micromatch().default)([relativePath], this.glob, {
  200. dot: this.dot
  201. }).length > 0
  202. : this.dot ||
  203. (0, _micromatch().default)([relativePath], '**/*').length > 0;
  204. }
  205. handleEvent(filepath) {
  206. const relativePath = path().relative(this.root, filepath);
  207. if (!this.isFileIncluded(relativePath)) {
  208. return;
  209. }
  210. fs().lstat(filepath, (error, stat) => {
  211. if (error && error.code !== 'ENOENT') {
  212. this.emit('error', error);
  213. return;
  214. }
  215. if (error) {
  216. // Ignore files that aren't tracked and don't exist.
  217. if (!this._tracked.has(filepath)) {
  218. return;
  219. }
  220. this._emit(DELETE_EVENT, relativePath);
  221. this._tracked.delete(filepath);
  222. return;
  223. }
  224. if (this._tracked.has(filepath)) {
  225. this._emit(CHANGE_EVENT, relativePath, stat);
  226. } else {
  227. this._tracked.add(filepath);
  228. this._emit(ADD_EVENT, relativePath, stat);
  229. }
  230. });
  231. }
  232. /**
  233. * Emit events.
  234. */
  235. _emit(type, file, stat) {
  236. this.emit(type, file, this.root, stat);
  237. this.emit(ALL_EVENT, type, file, this.root, stat);
  238. }
  239. }
  240. module.exports = FSEventsWatcher;