Commit 0a4970e4 by Adeel

API: Encapsulate output in a single object.

parent 70ce07ba
...@@ -54,7 +54,7 @@ function getOutFile(options) { ...@@ -54,7 +54,7 @@ function getOutFile(options) {
*/ */
function getStats(options) { function getStats(options) {
var stats = options.stats; var stats = {};
stats.entry = options.file || 'data'; stats.entry = options.file || 'data';
stats.start = Date.now(); stats.start = Date.now();
...@@ -70,12 +70,11 @@ function getStats(options) { ...@@ -70,12 +70,11 @@ function getStats(options) {
* @api private * @api private
*/ */
function endStats(options, sourceMap) { function endStats(options) {
var stats = options.stats || {}; var stats = options.stats || {};
stats.end = Date.now(); stats.end = Date.now();
stats.duration = stats.end - stats.start; stats.duration = stats.end - stats.start;
stats.sourceMap = sourceMap;
return stats; return stats;
} }
...@@ -139,14 +138,13 @@ function getOptions(options) { ...@@ -139,14 +138,13 @@ function getOptions(options) {
options.paths = (options.include_paths || options.includePaths || []).join(path.delimiter); options.paths = (options.include_paths || options.includePaths || []).join(path.delimiter);
options.precision = parseInt(options.precision) || 5; options.precision = parseInt(options.precision) || 5;
options.sourceMap = getSourceMap(options); options.sourceMap = getSourceMap(options);
options.stats = options.stats || {};
options.style = getStyle(options) || 0; options.style = getStyle(options) || 0;
if (options.imagePath && typeof options.imagePath !== 'string') { if (options.imagePath && typeof options.imagePath !== 'string') {
throw new Error('`imagePath` needs to be a string'); throw new Error('`imagePath` needs to be a string');
} }
getStats(options); options.stats = getStats(options);
var error = options.error; var error = options.error;
var success = options.success; var success = options.success;
...@@ -159,8 +157,10 @@ function getOptions(options) { ...@@ -159,8 +157,10 @@ function getOptions(options) {
err = { message: err }; err = { message: err };
} }
err.code = code;
if (error) { if (error) {
error(err, code); error(err);
} }
}; };
...@@ -170,7 +170,11 @@ function getOptions(options) { ...@@ -170,7 +170,11 @@ function getOptions(options) {
endStats(options, sourceMap); endStats(options, sourceMap);
if (success) { if (success) {
success(css, sourceMap); success({
css: css,
map: sourceMap,
stats: options.stats
});
} }
}; };
...@@ -225,77 +229,16 @@ module.exports.renderSync = function(options) { ...@@ -225,77 +229,16 @@ module.exports.renderSync = function(options) {
options = getOptions(options); options = getOptions(options);
output = options.data ? binding.renderSync(options) : binding.renderFileSync(options); output = options.data ? binding.renderSync(options) : binding.renderFileSync(options);
endStats(options, JSON.parse(options.stats.sourceMap)); endStats(options);
return output;
};
/**
* Render file
*
* `options.sourceMap` can be used to specify that the source map should be saved:
*
* - If falsy the source map will not be saved
* - If `options.sourceMap === true` the source map will be saved to the
* standard location of `options.file + '.map'`
* - Else `options.sourceMap` specifies the path (relative to the `outFile`)
* where the source map should be saved
*
* @param {Object} options
* @api public
*/
module.exports.renderFile = function(options) {
options = options || {};
var outFile = options.outFile;
var success = options.success;
if (options.sourceMap === true) {
options.sourceMap = outFile + '.map';
}
options.success = function(css, sourceMap) {
fs.writeFile(outFile, css, function(err) {
if (err) {
return options.error(err);
}
if (!options.sourceMap) {
return success(outFile);
}
var dir = path.dirname(outFile);
var sourceMapFile = path.resolve(dir, options.sourceMap);
fs.writeFile(sourceMapFile, JSON.stringify(sourceMap), function(err) {
if (err) {
return options.error(err);
}
success(outFile, sourceMapFile); var result = {
}); css: output,
}); map: options.stats.sourceMap
}; };
module.exports.render(options); delete options.stats.sourceMap;
};
/**
* Middleware
*
* @api public
*/
module.exports.middleware = function() { result.stats = options.stats;
return new Error([
'The middleware has been moved to',
'https://github.com/sass/node-sass-middleware'
].join(' '));
endStats(options, options.stats.sourceMap); return result;
return {
css: output,
map: options.stats.sourceMap
};
}; };
...@@ -31,7 +31,7 @@ module.exports = function(options, emitter) { ...@@ -31,7 +31,7 @@ module.exports = function(options, emitter) {
renderOptions.data = options.data; renderOptions.data = options.data;
} }
renderOptions.success = function(css, sourceMap) { renderOptions.success = function(result) {
var todo = 1; var todo = 1;
var done = function() { var done = function() {
if (--todo <= 0) { if (--todo <= 0) {
...@@ -40,37 +40,37 @@ module.exports = function(options, emitter) { ...@@ -40,37 +40,37 @@ module.exports = function(options, emitter) {
}; };
if (options.stdout || (!options.dest && !process.stdout.isTTY) || options.stdin) { if (options.stdout || (!options.dest && !process.stdout.isTTY) || options.stdin) {
emitter.emit('log', css); emitter.emit('log', result.css);
return done(); return done();
} }
emitter.emit('warn', chalk.green('Rendering Complete, saving .css file...')); emitter.emit('warn', chalk.green('Rendering Complete, saving .css file...'));
fs.writeFile(options.dest, css, function(err) { fs.writeFile(options.dest, result.css, function(err) {
if (err) { if (err) {
return emitter.emit('error', chalk.red(err)); return emitter.emit('error', chalk.red(err));
} }
emitter.emit('warn', chalk.green('Wrote CSS to ' + options.dest)); emitter.emit('warn', chalk.green('Wrote CSS to ' + options.dest));
emitter.emit('write', err, options.dest, css); emitter.emit('write', err, options.dest, result.css);
done(); done();
}); });
if (options.sourceMap) { if (options.sourceMap) {
todo++; todo++;
fs.writeFile(options.sourceMap, sourceMap, function(err) { fs.writeFile(options.sourceMap, result.map, function(err) {
if (err) { if (err) {
return emitter.emit('error', chalk.red('Error' + err)); return emitter.emit('error', chalk.red('Error' + err));
} }
emitter.emit('warn', chalk.green('Wrote Source Map to ' + options.sourceMap)); emitter.emit('warn', chalk.green('Wrote Source Map to ' + options.sourceMap));
emitter.emit('write-source-map', err, options.sourceMap, sourceMap); emitter.emit('write-source-map', err, options.sourceMap, result.sourceMap);
done(); done();
}); });
} }
emitter.emit('render', css); emitter.emit('render', result.css);
}; };
renderOptions.error = function(error) { renderOptions.error = function(error) {
......
...@@ -40,8 +40,8 @@ describe('spec', function () { ...@@ -40,8 +40,8 @@ describe('spec', function () {
sass.render({ sass.render({
file: t.src, file: t.src,
includePaths: t.paths, includePaths: t.paths,
success: function(css) { success: function(result) {
assert.equal(util.normalize(css), expected); assert.equal(util.normalize(result.css), expected);
done(); done();
}, },
error: function(err) { error: function(err) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment