-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.js
184 lines (150 loc) · 4.56 KB
/
handlers.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
ForkHandle = function(pkg) {
var path = require('path'),
gutil = require('gulp-util'),
c = gutil.colors,
notification = require('gulp-notify'),
through = require('through2');
/* notify */
notify = {
opts: function(message, success, opts) {
opts = (opts ? opts : {});
notification.logLevel(0);
return {
title: 'Task ' + (success !== false ? 'complete' : 'failed'),
subtitle: pkg.description,
message: message,
onLast: opts.onLast ? opts.onLast : true,
logLevel: 0,
icon: path.resolve(__dirname, 'assets', 'fork.png')
};
},
show: function(message, success, opts) {
return notification(this.opts(message, success, opts));
},
error: function(err, message) {
if(!message) message = '<%= error.message %>';
return notification.onError(notify.opts(message, false, {}))(err);
}
},
/* SCSS lint handler */
lint = {
error: function(file) {
var errorCount = file.scsslint.errorCount,
plural = errorCount === 1 ? '' : 's',
error,
files = [];
file.scsslint.results.forEach(function(result) {
var base = path.basename(file.path);
files.indexOf(base) < 0 && files.push(base);
generic.log(
':' + c.red(result.line) + ':' + c.red(result.column) + ' - ' + c.yellow(result.linter) + ': ' + result.reason,
{type: 'bad', space: false, pipe: false, file: file}
);
});
if(files) {
gutil.beep();
error = new gutil.PluginError('scss-lint', 'SCSS lint failed for ' + files.join(', '));
notify.error(error);
return error;
}
}
},
/* everything else handler */
generic = {
error: function(err) {
var alert = {};
if(err.fileName) err.file = err.fileName; // standardise
if(err.file) {
alert = {
message: c.cyan(path.basename(err.file)) + ':' + c.red(err.lineNumber) + ' ' + err.message,
notify: path.basename(err.file) + ':<%= error.lineNumber %> <%= error.message %>'
};
} else {
alert = {
message: err.message,
notify: '<%= error.message %>'
};
}
generic.log(alert.message, {type: 'bad', space: false, pipe: false});
gutil.beep();
notify.error(err, alert.notify);
this.emit('end');
},
/*
Global logger, appends filename and tick/cross when applicable.
msg: <string> The message to show
opt: <object> Options:
- type: good|bad Shows either tick or cross
- pipe: <boolean> Set to false when calling out of pipe
- color: <string> The color of the filename shown - Any colors supported in chalk.
- space: <boolean> Append space to end of filename
- file: <object> Directly supply a file object (to fetch name)
mod: <function> Modifier function, exposes the file, message and options vars before logging:
.pipe(handle.generic.log('Message here', false, function(file, msg, opt) {
if(file.relative.match(/whatever/)) {
msg += ' whatever';
opt.type = 'bad';
opt.color = 'green';
}
return arguments; // always return arguments
}))
*/
log: function(msg, opt, mod) {
if(!opt) opt = {};
var defaults = {
type: 'good',
pipe: true,
color: null,
space: true,
file: false
};
Object.keys(defaults).forEach(function(key) {
if(!(key in opt)) opt[key] = defaults[key];
});
var log = function(file, enc, cb) {
var mods = mod ? mod(file, msg, opt) : false;
if(mods) {
file = mods[0]; msg = mods[1]; opt = mods[2];
}
if(opt.type == 'bad') opt.color = 'cyan';
var start = (opt.type == 'good' ? c.green('✔') : c.red('✘')) + ' ',
filename = file ? path.basename(file.relative) + (opt.space ? ' ' : '') : '';
if(opt.color in c) filename = c[opt.color](filename);
gutil.log(start + filename + msg);
file && opt.pipe && this.push(file);
cb && cb();
};
return opt.pipe ? through.obj(log) : log.apply(this, [opt.file]);
}
},
/* small plugin to minify CSS, without the bells & whistles */
minify = function() {
return through.obj(function(file, enc, cb) {
if(file.isNull() || file.isStream()) return cb();
if(file.isBuffer()) {
var min = file.clone()
contents = min.contents.toString(),
layers = [
[/\/\*(?:(?!\*\/)[\s\S])*\*\/|[\r\n\t]+/g, ''],
[/ {2,}/g, ''],
[/ ([{:}]) /g, '$1'],
[/([;,]) /g, '$1'],
[/ !/g, '!']
];
for(var i = 0; i < layers.length; i++) {
contents = contents.replace(layers[i][0], layers[i][1]);
}
min.contents = new Buffer(contents);
this.push(min);
return cb();
}
});
};
return {
notify: notify,
lint: lint,
generic: generic,
minify: minify
};
};
module.exports = ForkHandle;