-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
96 lines (68 loc) · 2.31 KB
/
index.js
File metadata and controls
96 lines (68 loc) · 2.31 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';
var inherits = require('util').inherits;
var Transform = require('stream').Transform;
var PluginError = require('gulp-util').PluginError;
function gulpTransform(transformFn, options) {
if (isNone(transformFn)) {
throwPluginError('transformFn must be defined');
} else if (typeof transformFn !== 'function') {
throwPluginError('transformFn must be a function');
} else if (!isNone(options) && !/^(function|object)$/.test(typeof options)) {
throwPluginError('options must be an object if defined');
} else {
return new GulpTransformStream(transformFn, options);
}
}
function GulpTransformStream(transformFn, options) {
Transform.call(this, {objectMode: true});
this.transformFn = transformFn;
this.options = options || {};
}
inherits(GulpTransformStream, Transform);
GulpTransformStream.prototype._transform = function(file, encoding, next) {
if (file.isBuffer()) {
file.contents = transformContents(
this.transformFn, file.contents, file, this.options);
}
if (file.isStream()) {
file.contents = file.contents.pipe(
new FileStream(this.transformFn, file, this.options));
}
next(null, file);
};
function FileStream(transformFn, file, options) {
Transform.call(this);
this.transformFn = transformFn;
this.file = file;
this.options = options;
this.data = [];
}
inherits(FileStream, Transform);
FileStream.prototype._transform = function(chunk, encoding, next) {
this.data.push(chunk);
next();
};
FileStream.prototype._flush = function(done) {
var contents = Buffer.concat(this.data);
this.push(transformContents(
this.transformFn, contents, this.file, this.options));
done();
};
function transformContents(transformFn, contents, file, options) {
contents = options.encoding ? contents.toString(options.encoding) : contents;
contents = transformFn.call(options.thisArg, contents, file);
if (Buffer.isBuffer(contents)) {
return contents;
} else if (typeof contents === 'string' || contents instanceof String) {
return new Buffer(contents);
} else {
throwPluginError('transformFn must return a string or a Buffer');
}
}
function throwPluginError(message) {
throw new PluginError('gulp-transform', message);
}
function isNone(value) {
return value === undefined || value === null;
}
module.exports = gulpTransform;