-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcompiler.js
100 lines (79 loc) · 2.47 KB
/
compiler.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
95
96
97
98
99
100
exports.compile = compile;
var fs = require("fs");
var path = require('path');
var spawn = require("child_process").spawn;
var LogProvider = require('./log-provider');
var currentSassProcess = null;
function compile(data) {
if (currentSassProcess) {
return Promise.resolve();
}
return new Promise((res, rej) => {
var projectDir = data.projectDir,
appDir = data.appDir;
var logger = new LogProvider(data.logger);
var sassPath = require.resolve('node-sass/bin/node-sass');
if (fs.existsSync(sassPath)) {
logger.info("Found peer node-sass");
} else {
throw new Error('node-sass installation local to project was not found. Install by executing `npm install node-sass`.');
}
var isResolved = false;
var resolve = () => {
if (isResolved) {
return;
}
isResolved = true;
res(currentSassProcess);
}
var reject = err => {
if (isResolved) {
return;
}
isResolved = true;
err.errorAsWarning = true;
err.stopExecution = false;
rej(err);
}
// 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', path.join(__dirname, "importer.js")];
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 });
currentSassProcess.stdout.on('data', data => {
var stringData = data.toString();
logger.info(stringData);
});
currentSassProcess.stderr.on('data', error => {
var message = '';
var stringData = error.toString();
try {
var parsed = JSON.parse(stringData);
message = parsed.formatted || parsed.message || stringData;
} catch (e) {
message = error.toString();
}
logger.info(message);
});
currentSassProcess.on('error', error => {
logger.info(err.message);
reject(error);
});
currentSassProcess.on('exit', (code, signal) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`SASS compiler failed with exit code ${code}`));
}
currentSassProcess = null;
});
});
}