/
index.js
43 lines (34 loc) · 1.48 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
var shell = require('shelljs');
var watchr = require('watchr');
var less = require("less");
var fs = require('fs');
exports.init = function(config) {
if(!config) throw new Error("Please provide configuration.");
config.lesscPath = config.lesscPath || __dirname + "/node_modules/less/bin/lessc";
for(var i in config) {
if(i != "destination" && !fs.existsSync(config[i])) {
throw new Error("Path doesn't exist (" + config[i] + ")");
}
}
var watch = {
path: config.pathToWatch,
listener: function(eventName, filePath, fileCurrentStat, filePreviousStat) {
shell.exec(config.lesscPath + ' ' + config.fileToCompile + ' > ' + config.destination, function(code, output) {
if(code === 0) {
console.log('less compiled successfully');
} else {
console.log('less compilation failed. ' + output);
}
});
},
next: function(err, watcher){
if (err) throw err;
console.log('less-compiler watching started ...');
}
};
if(!config.pathToWatch) throw new Error("Missing config parameter 'pathToWatch'.");
if(!config.lesscPath) throw new Error("Missing config parameter 'lesscPath'.");
if(!config.fileToCompile) throw new Error("Missing config parameter 'fileToCompile'.");
if(!config.destination) throw new Error("Missing config parameter 'destination'.");
watchr.watch(watch);
};