forked from mvhenten/gulp-reduce-file
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (25 loc) · 1.09 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
'use strict';
var through = require('through'),
gutil = require('gulp-util'),
PluginError = gutil.PluginError,
File = gutil.File;
module.exports = function reduce(filename, fn, done, memo) {
if (!filename) throw new PluginError('gulp-reduce-file', 'Missing target filenam for gulp-reduce-file');
if (!fn instanceof Function) throw new PluginError('gulp-reduce-file', 'Missing iterator callback for gulp-reduce-file');
if (!done instanceof Function) throw new PluginError('gulp-reduce-file', 'Missing done callback for gulp-reduce-file');
memo = memo || '';
function iterate(file) {
memo = fn.apply(this, [file, memo]);
}
function end() {
var content = done.apply(this, [memo]),
target = new File();
if (typeof content === 'string') content = new Buffer(content);
if (!(content instanceof Buffer)) content = new Buffer(JSON.stringify(content, null, 2));
target.path = filename;
target.contents = content;
this.emit('data', target);
this.emit('end');
}
return through(iterate, end);
};