forked from badsyntax/sass-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (65 loc) · 1.63 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Prepare the command arguments.
* @param {object} options - The options passed to the compile method
* @return {array}
*/
function getArgs(options) {
if (!options.data && !options.filepath) {
throw new Error('Please either specify a filepath or data string to compile');
}
var args = [];
if (options.filepath) {
args.push(options.filepath);
}
if (options.type || options.data) {
args.push('--' + (options.type || 'scss'));
}
if (options.compass) {
args.push('--compass');
}
if (options.style) {
args.push('--style', options.style);
}
if (options.precision) {
args.push('--precision', options.precision);
}
if (options.loadPath) {
args.push('--load-path', options.loadPath);
}
return args;
}
/**
* Compiles the sass, either from a filepath or from a data string
* @param {object} options - The compile options
*/
function compile(options) {
var cp = require('child_process').spawn('sass', getArgs(options));
if( options.callback ){
var stdoutBuffer = '';
}
cp.stdout.setEncoding('utf8');
cp.stdout.on('data', function (data) {
if (options.callback) {
stdoutBuffer += data;
}
});
cp.stdout.on('close', function () {
if (options.callback) {
options.callback(null, stdoutBuffer);
}
});
cp.stderr.setEncoding('utf8');
cp.stderr.on('data', function (data) {
if (options.callback) {
options.callback(new Error(new Buffer(data).toString('utf8')), null);
}
});
if (options.data) {
cp.stdin.setEncoding('utf8');
cp.stdin.write(options.data);
cp.stdin.end();
}
}
module.exports = {
compile: compile
};