wrapper.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const path = require('path');
  2. const picomatch = require('picomatch');
  3. const isGlob = require('is-glob');
  4. function normalizeOptions(dir, opts = {}) {
  5. const { ignore, ...rest } = opts;
  6. if (Array.isArray(ignore)) {
  7. opts = { ...rest };
  8. for (const value of ignore) {
  9. if (isGlob(value)) {
  10. if (!opts.ignoreGlobs) {
  11. opts.ignoreGlobs = [];
  12. }
  13. const regex = picomatch.makeRe(value, {
  14. // We set `dot: true` to workaround an issue with the
  15. // regular expression on Linux where the resulting
  16. // negative lookahead `(?!(\\/|^)` was never matching
  17. // in some cases. See also https://bit.ly/3UZlQDm
  18. dot: true,
  19. windows: process.platform === 'win32',
  20. });
  21. opts.ignoreGlobs.push(regex.source);
  22. } else {
  23. if (!opts.ignorePaths) {
  24. opts.ignorePaths = [];
  25. }
  26. opts.ignorePaths.push(path.resolve(dir, value));
  27. }
  28. }
  29. }
  30. return opts;
  31. }
  32. exports.createWrapper = (binding) => {
  33. return {
  34. writeSnapshot(dir, snapshot, opts) {
  35. return binding.writeSnapshot(
  36. path.resolve(dir),
  37. path.resolve(snapshot),
  38. normalizeOptions(dir, opts),
  39. );
  40. },
  41. getEventsSince(dir, snapshot, opts) {
  42. return binding.getEventsSince(
  43. path.resolve(dir),
  44. path.resolve(snapshot),
  45. normalizeOptions(dir, opts),
  46. );
  47. },
  48. async subscribe(dir, fn, opts) {
  49. dir = path.resolve(dir);
  50. opts = normalizeOptions(dir, opts);
  51. await binding.subscribe(dir, fn, opts);
  52. return {
  53. unsubscribe() {
  54. return binding.unsubscribe(dir, fn, opts);
  55. },
  56. };
  57. },
  58. unsubscribe(dir, fn, opts) {
  59. return binding.unsubscribe(
  60. path.resolve(dir),
  61. fn,
  62. normalizeOptions(dir, opts),
  63. );
  64. }
  65. };
  66. };