Commit 0a4970e4 by Adeel

API: Encapsulate output in a single object.

parent 70ce07ba
......@@ -54,7 +54,7 @@ function getOutFile(options) {
*/
function getStats(options) {
var stats = options.stats;
var stats = {};
stats.entry = options.file || 'data';
stats.start = Date.now();
......@@ -70,12 +70,11 @@ function getStats(options) {
* @api private
*/
function endStats(options, sourceMap) {
function endStats(options) {
var stats = options.stats || {};
stats.end = Date.now();
stats.duration = stats.end - stats.start;
stats.sourceMap = sourceMap;
return stats;
}
......@@ -139,14 +138,13 @@ function getOptions(options) {
options.paths = (options.include_paths || options.includePaths || []).join(path.delimiter);
options.precision = parseInt(options.precision) || 5;
options.sourceMap = getSourceMap(options);
options.stats = options.stats || {};
options.style = getStyle(options) || 0;
if (options.imagePath && typeof options.imagePath !== 'string') {
throw new Error('`imagePath` needs to be a string');
}
getStats(options);
options.stats = getStats(options);
var error = options.error;
var success = options.success;
......@@ -159,8 +157,10 @@ function getOptions(options) {
err = { message: err };
}
err.code = code;
if (error) {
error(err, code);
error(err);
}
};
......@@ -170,7 +170,11 @@ function getOptions(options) {
endStats(options, sourceMap);
if (success) {
success(css, sourceMap);
success({
css: css,
map: sourceMap,
stats: options.stats
});
}
};
......@@ -225,77 +229,16 @@ module.exports.renderSync = function(options) {
options = getOptions(options);
output = options.data ? binding.renderSync(options) : binding.renderFileSync(options);
endStats(options, JSON.parse(options.stats.sourceMap));
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);
}
endStats(options);
success(outFile, sourceMapFile);
});
});
var result = {
css: output,
map: options.stats.sourceMap
};
module.exports.render(options);
};
/**
* Middleware
*
* @api public
*/
delete options.stats.sourceMap;
module.exports.middleware = function() {
return new Error([
'The middleware has been moved to',
'https://github.com/sass/node-sass-middleware'
].join(' '));
result.stats = options.stats;
endStats(options, options.stats.sourceMap);
return {
css: output,
map: options.stats.sourceMap
};
return result;
};
......@@ -31,7 +31,7 @@ module.exports = function(options, emitter) {
renderOptions.data = options.data;
}
renderOptions.success = function(css, sourceMap) {
renderOptions.success = function(result) {
var todo = 1;
var done = function() {
if (--todo <= 0) {
......@@ -40,37 +40,37 @@ module.exports = function(options, emitter) {
};
if (options.stdout || (!options.dest && !process.stdout.isTTY) || options.stdin) {
emitter.emit('log', css);
emitter.emit('log', result.css);
return done();
}
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) {
return emitter.emit('error', chalk.red(err));
}
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();
});
if (options.sourceMap) {
todo++;
fs.writeFile(options.sourceMap, sourceMap, function(err) {
fs.writeFile(options.sourceMap, result.map, function(err) {
if (err) {
return emitter.emit('error', chalk.red('Error' + err));
}
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();
});
}
emitter.emit('render', css);
emitter.emit('render', result.css);
};
renderOptions.error = function(error) {
......
......@@ -40,8 +40,8 @@ describe('spec', function () {
sass.render({
file: t.src,
includePaths: t.paths,
success: function(css) {
assert.equal(util.normalize(css), expected);
success: function(result) {
assert.equal(util.normalize(result.css), expected);
done();
},
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