-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
99 lines (85 loc) · 2.2 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
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
97
98
99
var through = require("through2"),
gutil = require("gulp-util"),
_ = require('lodash');
/**
* module.exports
* @param {object} param http://nevir.github.io/groc/cli.html#cli-options
* @returns {*}
*/
module.exports = function (param) {
"use strict";
var filesToGroc = [];
/**
* groc
* @param file
* @param enc
* @param callback
* @returns {*}
*/
function groc(file, enc, callback) {
/*jshint validthis:true*/
// Do nothing if no contents
if (file.isNull()) {
this.push(file);
return callback();
}
if (file.isStream()) {
this.emit("error",
new gutil.PluginError("gulp-groc", "Stream content is not supported"));
return callback();
}
// check if file.contents is a `Buffer`
if (file.isBuffer()) {
filesToGroc.push(file.path);
this.push(file);
callback();
}
}
/**
* merge Args
* @param files
* @param parameter
* @returns {*}
*/
function mergeArgs (files, parameter) {
return _.flatten(files.concat(parseOption(parameter)));
}
/**
* parseOption
* @param options
* @returns {Array}
*/
function parseOption (options) {
var args = [];
_.each(options, function(value, key) {
// Convert the key to a switch
var sw = (key.length > 1 ? '--' : '-') + key;
// Add the switch and its value
// If the value is an array, add all array elements to the array.
if(!_.isArray(value)) {
value = [value];
}
_.each(value, function(value) {
args.push([sw, value.toString()]);
});
});
return args;
}
/**
* end stream
*/
function endStream () {
if (filesToGroc) {
var groc = require('groc').CLI;
groc(mergeArgs(filesToGroc, param), function(err) {
if(err) {
this.emit('error', new gutil.PluginError('gulp-<%= pluginName %>', err));
}
this.emit('end');
}.bind(this));
} else {
this.emit('end');
}
}
return through.obj(groc, endStream);
};