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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. const path = require('path');
  3. const fs = require('fs');
  4. const {promisify} = require('util');
  5. const pLocate = require('p-locate');
  6. const fsStat = promisify(fs.stat);
  7. const fsLStat = promisify(fs.lstat);
  8. const typeMappings = {
  9. directory: 'isDirectory',
  10. file: 'isFile'
  11. };
  12. function checkType({type}) {
  13. if (type in typeMappings) {
  14. return;
  15. }
  16. throw new Error(`Invalid type specified: ${type}`);
  17. }
  18. const matchType = (type, stat) => type === undefined || stat[typeMappings[type]]();
  19. module.exports = async (paths, options) => {
  20. options = {
  21. cwd: process.cwd(),
  22. type: 'file',
  23. allowSymlinks: true,
  24. ...options
  25. };
  26. checkType(options);
  27. const statFn = options.allowSymlinks ? fsStat : fsLStat;
  28. return pLocate(paths, async path_ => {
  29. try {
  30. const stat = await statFn(path.resolve(options.cwd, path_));
  31. return matchType(options.type, stat);
  32. } catch (_) {
  33. return false;
  34. }
  35. }, options);
  36. };
  37. module.exports.sync = (paths, options) => {
  38. options = {
  39. cwd: process.cwd(),
  40. allowSymlinks: true,
  41. type: 'file',
  42. ...options
  43. };
  44. checkType(options);
  45. const statFn = options.allowSymlinks ? fs.statSync : fs.lstatSync;
  46. for (const path_ of paths) {
  47. try {
  48. const stat = statFn(path.resolve(options.cwd, path_));
  49. if (matchType(options.type, stat)) {
  50. return path_;
  51. }
  52. } catch (_) {
  53. }
  54. }
  55. };