Debian Codegraph @

node/@parcel/watcher/wrapper.js

raw
1 const path = require('path');
2 const picomatch = require('picomatch');
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 (value instanceof RegExp) {
13 if (value.flags !== '') {
14 throw new Error(
15 `RegExp ignore patterns must not have flags (got /${value.source}/${value.flags}). Flags are not supported by the native matcher.`,
16 );
17 }
18 if (!opts.ignoreGlobs) {
19 opts.ignoreGlobs = [];
20 }
21 // The native backend uses std::regex_match (full-string match), but
22 // callers expect JS .test() semantics (substring search). Wrapping the
23 // source in ^[\\s\\S]*(?:…)[\\s\\S]*$ achieves that. The (?:…) group
24 // isolates the source so leading/trailing | in the source stays contained.
25 opts.ignoreGlobs.push(`^[\\s\\S]*(?:${value.source})[\\s\\S]*$`);
26 } else if (isGlob(value)) {
27 if (!opts.ignoreGlobs) {
28 opts.ignoreGlobs = [];
29 }
30
31 const regex = picomatch.makeRe(value, {
32 // We set `dot: true` to workaround an issue with the
33 // regular expression on Linux where the resulting
34 // negative lookahead `(?!(\\/|^)` was never matching
35 // in some cases. See also https://bit.ly/3UZlQDm
36 dot: true,
37 windows: process.platform === 'win32',
38 });
39 opts.ignoreGlobs.push(regex.source);
40 } else {
41 if (!opts.ignorePaths) {
42 opts.ignorePaths = [];
43 }
44
45 opts.ignorePaths.push(path.resolve(dir, value));
46 }
47 }
48 }
49
50 return opts;
51 }
52
53 exports.createWrapper = (binding) => {
54 return {
55 writeSnapshot(dir, snapshot, opts) {
56 return binding.writeSnapshot(
57 path.resolve(dir),
58 path.resolve(snapshot),
59 normalizeOptions(dir, opts),
60 );
61 },
62 getEventsSince(dir, snapshot, opts) {
63 return binding.getEventsSince(
64 path.resolve(dir),
65 path.resolve(snapshot),
66 normalizeOptions(dir, opts),
67 );
68 },
69 async subscribe(dir, fn, opts) {
70 dir = path.resolve(dir);
71 opts = normalizeOptions(dir, opts);
72 await binding.subscribe(dir, fn, opts);
73
74 return {
75 unsubscribe() {
76 return binding.unsubscribe(dir, fn, opts);
77 },
78 };
79 },
80 unsubscribe(dir, fn, opts) {
81 return binding.unsubscribe(
82 path.resolve(dir),
83 fn,
84 normalizeOptions(dir, opts),
85 );
86 },
87 };
88 };