forked from NativeScript/nativescript-dev-sass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.js
77 lines (65 loc) · 2.38 KB
/
converter.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
exports.convert = convert;
var spawn = require('child_process').spawn;
var fs = require('fs');
var path = require('path');
function convert(logger, projectDir, options) {
return new Promise(function (resolve, reject) {
options = options || {};
var peerSassPath = path.join(__dirname, '../../node-sass');
var sassPath = path.join(peerSassPath, 'bin/node-sass');
var appDir = path.join(projectDir, "app");
if (fs.existsSync(sassPath)) {
try {
logger.info('Found peer node-sass');
} catch (err) { }
} else {
throw Error('node-sass installation local to project was not found. Install by executing `npm install node-sass`.');
}
var nodeArgs = [sassPath, appDir, '--output', appDir, '--output-style', 'compressed'];
if (options.watch) {
nodeArgs.push('-r', '--watch');
}
logger.trace(process.execPath, nodeArgs.join(' '));
var sass = spawn(process.execPath, nodeArgs);
var isResolved = false;
var watchResolveTimeout;
sass.stdout.on('data', function (data) {
var stringData = data.toString();
logger.info(stringData);
});
sass.stderr.on('data', function (err) {
var message = '';
var stringData = err.toString();
try {
var parsed = JSON.parse(stringData);
message = parsed.formatted || parsed.message || stringData;
} catch (e) {
message = err.toString();
}
logger.info(message);
});
sass.on('error', function (err) {
logger.info(err.message);
if (!isResolved) {
isResolved = true;
reject(err);
}
});
// TODO: Consider using close event instead of exit
sass.on('exit', function (code, signal) {
if (!isResolved) {
isResolved = true;
if (code === 0) {
resolve();
} else {
reject(Error('SASS compiler failed with exit code ' + code));
}
}
});
// SASS does not recompile on watch, so directly resolve.
if (options.watch && !isResolved) {
isResolved = true;
resolve();
}
});
}