/
sassquatch.js
86 lines (66 loc) · 1.79 KB
/
sassquatch.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
#!/usr/bin/env node
var program = require('commander'),
scss = require('node-sass'),
fs = require('fs'),
watchPath = './',
outputPath = './';
program
.version('0.0.1')
.option('-v, --verbose', 'Verbose output')
.option('-o, --output [path]', 'Output Path [default ./]')
.option('-w, --watch [path]', 'Watch Path [default ./]')
.parse(process.argv);
if(program.watch){
watchPath = program.watch;
}
if(program.output){
outputPath = program.output;
}
function hasError(error){
if(error){
console.log(error);
return true;
}
}
function renderSass(callback){
return function(filename, sass){
log('Rendering Sass from ' + filename);
scss.render(sass.toString(), function(error, css){
if(!hasError(error)){
callback(filename, css);
}
});
};
}
function readSass(filename, callback){
log('Reading Sass from ' + filename);
fs.readFile(watchPath + filename, function(error, scssFile){
if(!hasError(error)){
callback(filename, scssFile.toString());
}
});
}
function writeCss(filename, css){
filename = filename.split('.').slice(0,-1).join('.') + '.css';
log('Writing Css to ' + filename);
fs.writeFile(outputPath + filename, css, function(error){
hasError(error);
});
}
function log(message){
if(program.verbose){
console.log(message);
}
}
log('Watching ' + watchPath + ' for changes.');
log('Output path is ' + outputPath);
fs.watch(watchPath, function(eventType, filename){
var ext;
if(eventType !== 'change'){
return;
}
ext = filename.split('.').pop();
if(ext === 'scss' || ext === 'sass'){
readSass(filename, renderSass(writeCss));
}
});