wrapper.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const path = require('path');
  2. const micromatch = require('micromatch');
  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 = micromatch.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. // C++ does not support lookbehind regex patterns, they
  20. // were only added later to JavaScript engines
  21. // (https://bit.ly/3V7S6UL)
  22. lookbehinds: false
  23. });
  24. opts.ignoreGlobs.push(regex.source);
  25. } else {
  26. if (!opts.ignorePaths) {
  27. opts.ignorePaths = [];
  28. }
  29. opts.ignorePaths.push(path.resolve(dir, value));
  30. }
  31. }
  32. }
  33. return opts;
  34. }
  35. exports.createWrapper = (binding) => {
  36. return {
  37. writeSnapshot(dir, snapshot, opts) {
  38. return binding.writeSnapshot(
  39. path.resolve(dir),
  40. path.resolve(snapshot),
  41. normalizeOptions(dir, opts),
  42. );
  43. },
  44. getEventsSince(dir, snapshot, opts) {
  45. return binding.getEventsSince(
  46. path.resolve(dir),
  47. path.resolve(snapshot),
  48. normalizeOptions(dir, opts),
  49. );
  50. },
  51. async subscribe(dir, fn, opts) {
  52. dir = path.resolve(dir);
  53. opts = normalizeOptions(dir, opts);
  54. await binding.subscribe(dir, fn, opts);
  55. return {
  56. unsubscribe() {
  57. return binding.unsubscribe(dir, fn, opts);
  58. },
  59. };
  60. },
  61. unsubscribe(dir, fn, opts) {
  62. return binding.unsubscribe(
  63. path.resolve(dir),
  64. fn,
  65. normalizeOptions(dir, opts),
  66. );
  67. }
  68. };
  69. };