-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbstudio-sass.js
More file actions
executable file
·152 lines (114 loc) · 3.19 KB
/
bstudio-sass.js
File metadata and controls
executable file
·152 lines (114 loc) · 3.19 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env node
let sass = require('node-sass');
let path = require('path');
let readline = require('readline');
if (process.stdin.isTTY) {
console.log('bstudio-sass is Bootstrap Studio\'s sidekick for compiling SASS files.');
console.log('To link this utility to Bootstrap Studio, please use the following path:');
var cmd = process.argv[1];
if (process.platform == 'win32') {
cmd = cmd.replace('\\node_modules\\bstudio-sass\\bstudio-sass.js', '\\bstudio-sass');
}
console.log(cmd);
process.exit();
}
let supportedVersions = {
inputMessage: 1,
outputMessage: 1
};
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
process.chdir('/');
rl.on('line', function(data){
let message = {};
try {
message = JSON.parse(data);
if (!message.version || message.version > supportedVersions.inputMessage) {
throw new Error('This message format is not supported. Please update bstudio-sass.');
}
if (!message.files || typeof message.files != 'object') {
throw new Error('Missing or invalid file entry.');
}
// Compile all regular sass files
let result = {};
for (let filePath in message.files) {
if (/^_/.test(path.basename(filePath))) {
// SASS partial. Skip compilation.
continue;
}
let render = sass.renderSync({
file: filePath,
data: message.files[filePath] || ' ',
outputStyle: 'expanded',
importer: function(url, prev) {
if (/^https?:\/\//.test(url)) {
// Leave URLs as is
return null;
}
// Compensating for libsass' insistence on prepending
// paths with the current working directory. Also,
// normalizing Windows path separators.
let resolvedPath = path.resolve(path.dirname(prev), url).replace(process.cwd(), '/').replace(/\\/g, '/');
if (path.extname(resolvedPath) != 'scss') {
resolvedPath += '.scss';
}
let underScorePath = path.dirname(resolvedPath) + '/_' + path.basename(resolvedPath);
underScorePath = underScorePath.replace(/\/+/g, '/');
if (underScorePath in message.files) {
resolvedPath = underScorePath;
}
if (!(resolvedPath in message.files)) {
return new Error('File to import not found: ' + resolvedPath.slice(1));
}
return {
file: resolvedPath,
contents: message.files[resolvedPath]
};
}
});
result[filePath] = render.css.toString();
}
output({
version: supportedVersions.outputMessage,
jobID: message.jobID,
result: result
});
}
catch (e){
if (e.formatted) {
errorAndExit(
'SASS Compilation Error',
e.message,
message.jobID,
{
formatted: e.formatted,
file: e.file ?
e.file.replace(process.cwd(), '/').replace(/\\/g, '/').slice(1):
null,
line: e.line,
column: e.column
}
);
}
errorAndExit('Invalid message format', e.message, message.jobID);
}
});
function output(message) {
console.log(JSON.stringify(message));
}
function errorAndExit(short, long, jobID, data = {}) {
outputAndExit({
status: 'error',
short: short,
long: long || '',
jobID: jobID,
data: data
});
}
function outputAndExit(message) {
console.log(JSON.stringify(message));
process.exit();
}