-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathci-vfs-patch.js
More file actions
57 lines (52 loc) · 1.44 KB
/
ci-vfs-patch.js
File metadata and controls
57 lines (52 loc) · 1.44 KB
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
/**
* CI shim for gulp/vinyl-fs to tolerate empty/invalid globs.
* Preload with: NODE_OPTIONS="--require ./build/ci-vfs-patch.cjs"
*/
'use strict';
/** @returns {import('stream').Readable} */
function emptyStream() {
const { Readable } = require('stream');
return Readable.from([]);
}
/**
* Sanitize vinyl-fs glob argument.
* @param {any} globs
* @returns {string|string[]|null}
*/
function sanitize(globs) {
if (Array.isArray(globs)) {
const cleaned = globs.filter(g => typeof g === 'string' && g.trim().length > 0);
return cleaned.length ? cleaned : null;
}
if (typeof globs === 'string') {
const s = globs.trim();
return s.length ? s : null;
}
return null;
}
/**
* Patch a src function (vinyl-fs/lib/src).
* @param {(globs:any, options?:any)=>any} realSrc
*/
function makePatchedSrc(realSrc) {
return function patchedSrc(globs, options) {
const safe = sanitize(globs);
if (!safe) return emptyStream();
return realSrc.call(this, safe, options);
};
}
try {
// Patch top-level vinyl-fs.src
const vfs = require('vinyl-fs');
if (vfs && typeof vfs.src === 'function') {
vfs.src = makePatchedSrc(vfs.src);
}
} catch { /* ignore */ }
try {
// Patch direct import path used by gulp
const srcPath = require.resolve('vinyl-fs/lib/src/index.js');
const realSrc = require(srcPath);
if (typeof realSrc === 'function' && require.cache[srcPath]) {
require.cache[srcPath].exports = makePatchedSrc(realSrc);
}
} catch { /* ignore */ }