-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
75 lines (72 loc) · 2.36 KB
/
Copy pathapp.js
File metadata and controls
75 lines (72 loc) · 2.36 KB
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
#!/usr/bin/env node
var watch = require('watch');
var argv = require('optimist').argv;
var fs = require('fs');
var less = require('less');
var path = require('path');
var output = argv.o;
var file = argv.f;
var folder = argv.d;
//
if(argv.h) {
console.log('Watch for a entire directory and only compile a specific file');
console.log('')
console.log('Usage: watchless [Options]');
console.log('');
console.log(' --- Options ---');
console.log("");
console.log(" '-o' the output folder");
console.log(" '-d' the directory to watch");
console.log(" '-f' the file to compile when there is a change in the directory");
console.log(" '-h' show the help");
console.log();
console.log("All the options are required (for now) later i will make some error checking");
console.log();
console.log(" --- Example ---");
console.log();
console.log(" watchless -o cssfolder -d lessfolder -f lessfolder/file.less");
console.log();
console.log();
process.exit();
}
console.log('Waiting for changes in folder '+folder);
var compile = function(f, stat, prev) {
fs.readFile(file, function(err, content){
if(err){
console.log("an error happend while trying to read the file "+file);
console.log(err);
process.exit();
} else {
var parser = new (less.Parser)({
paths:[path.dirname(file)],
filename:path.basename(file)
});
parser.parse(content.toString(), function (err, tree){
if(err) {
console.log("an error happend while trying to compile the less file");
console.log(err);
} else {
fileToWrite = path.basename(file, '.less');
fileToWrite+= ".css";
outputFile = path.join(output, fileToWrite);
var css = tree.toCSS({compress:true});
fs.writeFile(outputFile, css, function (err){
if(err) {
trow(err);
console.log("an error happend while trying to save the file to the output "+ output);
console.log(err);
} else {
console.log(new Date().toLocaleTimeString() + ' compiled file '+file +' on '+outputFile);
}
});
}
});
}
});
}
compile();
watch.createMonitor(argv.d, function(monitor){
monitor.on("created", compile);
monitor.on("changed", compile);
monitor.on("removed", compile);
});