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