-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
33 lines (29 loc) · 1.53 KB
/
index.js
File metadata and controls
33 lines (29 loc) · 1.53 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
"use strict"
var gutil = require("gulp-util");
var through = require("through2");
/** @summary The provided callback function will be called for each file that passes through.
* @param {function} fn - A callback function that will be provided with one parameter, the (Vinyl) file object that is being processed. The properties from this
* file object can be modified in the callback function, or a completely new (Vinyl) file can be returned. (In the former case nothing needs to be returned.)
*/
module.exports = function (fn) {
if ( !fn || typeof fn !== "function" ) {
throw new gutil.PluginError("gulp-filechange", "Need a callback function.");
}
// Callback signature: callback(err, chunk)
return through.obj(function (file, encoding, callback) {
if ( file.isNull() ) {
return callback(null, file);
}
if ( file.isStream() ) {
return callback(new gutil.PluginError("gulp-filechange", "Streaming not supported."));
}
var newfile = fn(file);
// Update the file if a new one was returned; otherwise just use the original file object (whose properties nonetheless might have been updated by the callback function).
// Note: Older version of Gulp (< v4.x) use an old version of Vinyl that doesn't yet support Vinyl.isVinyl(), so we manually check if it has all of the Vinyl properties.
if ( newfile && (newfile._isVinyl || (newfile.history && newfile.cwd && newfile.base && newfile.stat !== undefined && newfile.contents !== undefined)) ) {
file = newfile;
}
// Send the file back to the stream.
callback(null, file);
});
}