forked from appleboy/gulp-compass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (47 loc) · 1.46 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
'use strict';
var fs = require('fs'),
compass = require('./lib/compass'),
through = require('through2'),
gutil = require('gulp-util'),
path = require('path');
// Consts
var PLUGIN_NAME = 'gulp-compass';
module.exports = function(opt) {
var compile = function(file, enc, cb) {
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
return cb();
}
if (path.basename(file.path)[0] === '_') {
return cb();
}
compass(file.path, opt, function(code, stdout, stderr, path) {
if (code === 127) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'You need to have Ruby and Compass installed ' +
'and in your system PATH for this task to work. '));
return cb();
}
// support error callback
if (code !== 0) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, stdout || 'Compass failed'));
return cb();
}
// excute callback
var pathToCss = gutil.replaceExtension(path, '.css'),
contents = fs.readFileSync(pathToCss);
// Fix garbled output.
if (!(contents instanceof Buffer)) {
contents = new Buffer(contents);
}
file.path = gutil.replaceExtension(file.path, '.css');
file.contents = contents;
this.push(file);
cb();
}.bind(this));
};
return through.obj(compile);
};