-
Notifications
You must be signed in to change notification settings - Fork 17
/
stss.js
281 lines (254 loc) · 8.07 KB
/
stss.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
var fs = require('fs'),
path = require('path'),
async = require('async'),
EventEmitter = require('events').EventEmitter,
util = require('util'),
stss2scss = require('./lib/renderer/stss2scss'),
scss2css = require('./lib/renderer/scss2css'),
css2json = require('./lib/renderer/css2json'),
json2tss = require('./lib/renderer/json2tss');
/**
* @class STSS
* @extends events.EventEmitter
*
* Manages STSS to TSS conversion.
* @author Ronald Treur
*/
var STSS = function() {
// Apply parent constructor
EventEmitter.call(this);
};
// Extend EventEmitter
util.inherits(STSS, EventEmitter);
/**
* Convert the supplied SCSS structured markup into TSS.
*
* This function executes asynchronously.
*
* @private
* @param {String} scss SCSS structured markup
* @param {Object} options Dictionary containing instructions
* @param {Function} options.success Callback that will be called upon successful conversion
* @param {Function} [options.error] Callback that will be called if conversion fails
* @param {String} [options.shFile] JSON file that contains additional shorthand notation to use during conversion
* @param {Function} log Function that logs the current state of the process
*/
function process(stss, options, log) {
async.waterfall([
function(callback) {
var scss = stss2scss(stss, options);
if (scss instanceof Error) {
callback(scss);
return;
}
log('scss', scss);
callback(null, scss);
},
function(scss, callback) {
var css = scss2css(scss, options);
if (css instanceof Error) {
callback(css);
return;
}
if (css instanceof Buffer) {
css = css.toString();
}
log('css', css);
callback(null, css);
},
function(css, callback) {
var json = css2json(css, options);
if (json instanceof Error) {
callback(json);
return;
}
log('json', JSON.stringify(json, null, 2));
callback(null, json);
},
function(json, callback) {
var tss = json2tss(json, options);
if (tss instanceof Error) {
callback(tss);
return;
}
log('tss', tss);
callback(null, tss);
},
], function(err, tss) {
if (err) {
options.error && options.error(err);
} else {
options.success(tss);
}
});
}
/**
* Convert the supplied SCSS structured markup into TSS.
*
* This function executes synchronously.
*
* @private
* @param {String} scss SCSS structured markup
* @param {Object} options Dictionary containing instructions
* @param {Function} options.success Callback that will be called upon successful conversion
* @param {Function} [options.error] Callback that will be called if conversion fails
* @param {String} [options.shFile] JSON file that contains additional shorthand notation to use during conversion
* @param {Function} log Function that logs the current state of the process
* @return {String} TSS structured markup
* @throws {Error} If an error occured during any of the four rendering passes
*/
function processSync(stss, options, log) {
var scss = stss2scss(stss, options);
if (scss instanceof Error) { throw scss; }
log('scss', scss);
var css = scss2css(scss, options);
if (css instanceof Error) { throw css; }
if (css instanceof Buffer) { css = css.toString(); }
log('css', css);
var json = css2json(css, options);
if (json instanceof Error) { throw json; }
log('json', JSON.stringify(json, null, 2));
var tss = json2tss(json, options);
if (tss instanceof Error) { throw tss; }
log('tss', tss);
return tss;
}
function parseOptions(options) {
if (options.shFile) {
if (!fs.existsSync(options.shFile)) {
throw new Error('Shorthand file does not exist: '+ options.shFile);
} else {
options.shorthand = require(options.shFile);
}
}
return options;
}
/**
* Render STSS to TSS using the supplied instructions.
*
* Either options.data or options.file need to be provided. If both are supplied, data takes precedence and file
* will be ignored.
* Likewise, either options.outFile or options.success should be defined, or both.
*
* This function executes asynchronously.
*
* @param {Object} options Dictionary containing instructions
* @param {String} [options.data] STSS data, required if options.file is not passed
* @param {String} [options.file] STSS file that is to be converted
* @param {String} [options.outFile] File that is created/overwritten with the generated TSS output
* @param {Function} [options.success] Callback that will be called upon successful conversion
* @param {Function} [options.error] Callback that will be called if conversion fails
* @param {String} [options.shFile] JSON file that contains additional shorthand notation to use during conversion
*/
STSS.prototype.render = function(options) {
var success = options.success || function() {},
logProcess = this.logProcess.bind(this),
tss;
try {
options = parseOptions(options);
} catch (err) {
return options.error && options.error(err);
}
options.success = function(tss) {
if (options.outFile && tss) {
fs.writeFile(options.outFile, tss, function(err) {
if (err) { return options.error && options.error(err); }
success(options.outFile);
});
} else {
success(tss);
}
};
if (options.data) {
process(options.data, options, logProcess);
} else if (options.file) {
fs.exists(options.file, function() {
fs.readFile(options.file, {encoding: 'utf8'}, function(err, data) {
if (err) { return options.error && options.error(err); }
process(data, options, logProcess);
});
});
} else {
return options.error && options.error('No input file or data supplied');
}
};
/**
* Render STSS to TSS using the supplied instructions.
*
* Either options.data or options.file need to be provided. If both are supplied, data takes precedence and file
* will be ignored.
* Likewise, either options.outFile or options.success should be defined, or both.
*
* This function executes synchronously.
*
* @param {Object} options Dictionary containing instructions
* @param {String} [options.data] STSS data, required if options.file is not passed
* @param {String} [options.file] STSS file that is to be converted
* @param {String} [options.outFile] File that is created/overwritten with the generated TSS output
* @param {Function} [options.success] Callback that will be called upon successful conversion
* @param {Function} [options.error] Callback that will be called if conversion fails
* @param {String} [options.shFile] JSON file that contains additional shorthands to use during conversion
*/
STSS.prototype.renderSync = function(options) {
var success = options.success || function() {},
error = options.error || function() {},
logProcess = this.logProcess.bind(this),
data, tss;
try {
options = parseOptions(options);
} catch (err) {
return error(err);
}
options.success = function(tss) {
if (options.outFile && tss) {
try {
fs.writeFileSync(options.outFile, tss);
} catch (err) {
return error(err);
}
success(options.outFile);
} else {
success(tss);
}
};
if (options.data) {
try {
tss = processSync(options.data, options, logProcess);
} catch (err) {
return error(err);
}
options.success(tss);
} else if (options.file) {
// Save the basename for potential use in error messages
options.filename = path.basename(options.file);
if (fs.existsSync(options.file)) {
try {
data = fs.readFileSync(options.file, {encoding: 'utf8'});
} catch (err) {
return error(err);
}
try {
tss = processSync(data, options, logProcess);
} catch (err) {
return error(err);
}
options.success(tss);
} else {
return error('Input file does not exist: '+options.file);
}
} else {
return error('No input file or data supplied');
}
};
/**
* Log the conversion step that was just completed.
*
* @param {String} conversion Type of conversion just completed
* @param {String} output Output of the phase that was just finished
* @fires conversionStep
*/
STSS.prototype.logProcess = function(conversion, output) {
this.emit('conversionStep', conversion, output);
};
// Return the Singleton
module.exports = new STSS();