-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathindex.js
59 lines (47 loc) · 1.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import process from 'node:process';
import path from 'node:path';
import PluginError from 'plugin-error';
import multimatch from 'multimatch';
import streamfilter from 'streamfilter';
import toAbsoluteGlob from 'to-absolute-glob';
import slash from 'slash';
export default function plugin(pattern, options = {}) {
pattern = typeof pattern === 'string' ? [pattern] : pattern;
if (!Array.isArray(pattern) && typeof pattern !== 'function') {
throw new PluginError('gulp-filter', '`pattern` should be a string, array, or function');
}
// TODO: Use `readableStream.filter()` when targeting Node.js 18.
return streamfilter((file, encoding, callback) => {
let match;
if (typeof pattern === 'function') {
match = pattern(file);
} else {
const base = path.dirname(file.path);
let patterns = pattern.map(pattern => {
// Filename only matching glob, prepend full path.
if (!pattern.includes('/')) {
if (pattern[0] === '!') {
return '!' + path.resolve(base, pattern.slice(1));
}
return path.resolve(base, pattern);
}
pattern = toAbsoluteGlob(pattern, {cwd: file.cwd, root: options.root});
// Calling `path.resolve` after `toAbsoluteGlob` is required for removing `..` from path.
// This is useful for `../A/B` cases.
if (pattern[0] === '!') {
return '!' + path.resolve(pattern.slice(1));
}
return path.resolve(pattern);
});
if (process.platform === 'win32') {
patterns = patterns.map(pattern => slash(pattern));
}
match = multimatch(path.resolve(file.cwd, file.path), patterns, options).length > 0;
}
callback(!match);
}, {
objectMode: true,
passthrough: options.passthrough !== false,
restore: options.restore,
});
}