-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathconverter.js
94 lines (81 loc) · 3.18 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
exports.convert = convert;
exports.getSassProcess = getSassProcess;
var spawn = require('child_process').spawn;
var fs = require('fs');
var path = require('path');
var currentSassProcess = null;
function convert(logger, projectDir, appDir, options) {
return new Promise(function (resolve, reject) {
options = options || {};
var sassPath = require.resolve('node-sass/bin/node-sass');
var importerPath = path.join(__dirname, "importer.js");
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`.');
}
// Node SASS Command Line Args (https://github.com/sass/node-sass#command-line-interface)
// --ouput : Output directory
// --output-style : CSS output style (nested | expanded | compact | compresed)
// -q : Supress log output except on error
// --follow : Follow symlinked directories
// -r : Recursively watch directories or files
// --watch : Watch a directory or file
var nodeArgs = [sassPath, appDir, '--output', appDir, '--output-style', 'compressed', '-q', '--follow', '--importer', importerPath];
if (options.watch) {
nodeArgs.push('-r', '--watch');
}
logger.trace(process.execPath, nodeArgs.join(' '));
var env = Object.create( process.env );
env.PROJECT_DIR = projectDir;
env.APP_DIR = appDir;
currentSassProcess = spawn(process.execPath, nodeArgs, { env: env });
var isResolved = false;
var watchResolveTimeout;
currentSassProcess.stdout.on('data', function (data) {
var stringData = data.toString();
logger.info(stringData);
});
currentSassProcess.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) {
renderMsg = true;
message = err.toString();
}
logger.info(message);
});
currentSassProcess.on('error', function (err) {
logger.info(err.message);
if (!isResolved) {
isResolved = true;
reject(err);
}
});
// TODO: Consider using close event instead of exit
currentSassProcess.on('exit', function (code, signal) {
currentSassProcess = null;
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();
}
});
}
function getSassProcess() {
return currentSassProcess;
}