123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- var fs = require('fs'),
- path = require('path');
- require('./extensions');
- /**
- * Get binding
- *
- * @api private
- */
- function getBinding() {
- var candidates = [
- path.join(__dirname, '..', 'build', 'Debug', 'binding.node'),
- path.join(__dirname, '..', 'build', 'Release', 'binding.node'),
- path.join(__dirname, '..', 'vendor', process.sassBinaryName, 'binding.node')
- ];
- var candidate = candidates.filter(fs.existsSync).shift();
- if (!candidate) {
- throw new Error('`libsass` bindings not found. Try reinstalling `node-sass`?');
- }
- return candidate;
- }
- /**
- * Get outfile
- *
- * @param {Object} options
- * @api private
- */
- function getOutFile(options) {
- var file = options.file;
- var outFile = options.outFile;
- if (!outFile || typeof outFile !== 'string' || (!options.data && !file)) {
- return null;
- }
- if (path.resolve(outFile) === path.normalize(outFile).replace(/(.+)([\/|\\])$/, '$1')) {
- return outFile;
- }
- return path.resolve(path.dirname(file), outFile);
- }
- /**
- * Get stats
- *
- * @param {Object} options
- * @api private
- */
- function getStats(options) {
- var stats = {};
- stats.entry = options.file || 'data';
- stats.start = Date.now();
- return stats;
- }
- /**
- * End stats
- *
- * @param {Object} stats
- * @param {Object} sourceMap
- * @api private
- */
- function endStats(stats) {
- stats.end = Date.now();
- stats.duration = stats.end - stats.start;
- return stats;
- }
- /**
- * Get style
- *
- * @param {Object} options
- * @api private
- */
- function getStyle(options) {
- var style = options.output_style || options.outputStyle;
- var styles = {
- nested: 0,
- expanded: 1,
- compact: 2,
- compressed: 3
- };
- return styles[style];
- }
- /**
- * Get source map
- *
- * @param {Object} options
- * @api private
- */
- function getSourceMap(options) {
- var file = options.file;
- var outFile = options.outFile;
- var sourceMap = options.sourceMap;
- if (sourceMap) {
- if (typeof sourceMap !== 'string') {
- sourceMap = outFile ? outFile + '.map' : '';
- } else if (outFile) {
- sourceMap = path.resolve(path.dirname(file), sourceMap);
- }
- }
- return sourceMap;
- }
- /**
- * Get options
- *
- * @param {Object} options
- * @api private
- */
- function getOptions(options) {
- options = options || {};
- options.comments = options.source_comments || options.sourceComments || false;
- options.data = options.data || null;
- options.file = options.file || null;
- options.imagePath = options.image_path || options.imagePath || '';
- options.outFile = getOutFile(options);
- options.paths = (options.include_paths || options.includePaths || []).join(path.delimiter);
- options.precision = parseInt(options.precision) || 5;
- options.sourceMap = getSourceMap(options);
- options.style = getStyle(options) || 0;
- if (options.imagePath && typeof options.imagePath !== 'string') {
- throw new Error('`imagePath` needs to be a string');
- }
- var error = options.error;
- var success = options.success;
- options.error = function(err, code) {
- err = JSON.parse(err);
- err.code = code;
- if (error) {
- error(err);
- }
- };
- options.success = function() {
- var result = options.result;
- var stats = endStats(result.stats);
- if (success) {
- success({
- css: result.css,
- map: result.map,
- stats: stats
- });
- }
- };
- delete options.image_path;
- delete options.include_paths;
- delete options.includePaths;
- delete options.source_comments;
- delete options.sourceComments;
- options.result = {
- stats: getStats(options)
- };
- return options;
- }
- /**
- * Require binding
- */
- var binding = require(getBinding());
- /**
- * Render
- *
- * @param {Object} options
- * @api public
- */
- module.exports.render = function(options) {
- options = getOptions(options);
- var importer = options.importer;
- if (importer) {
- options.importer = function(file, prev, key) {
- function done(data) {
- console.log(data); // ugly hack
- binding.importedCallback({
- index: key,
- objectLiteral: data
- });
- }
- var result = importer(file, prev, done);
- if (result) {
- done(result);
- }
- };
- }
- options.data ? binding.render(options) : binding.renderFile(options);
- };
- /**
- * Render sync
- *
- * @param {Object} options
- * @api public
- */
- module.exports.renderSync = function(options) {
- options = getOptions(options);
- var importer = options.importer;
- if (importer) {
- options.importer = function(file, prev) {
- return { objectLiteral: importer(file, prev) };
- };
- }
- var status = options.data ? binding.renderSync(options) : binding.renderFileSync(options);
- var result = options.result;
- if(status) {
- result.stats = endStats(result.stats);
- return result;
- }
- };
- /**
- * API Info
- *
- * @api public
- */
- module.exports.info = function() {
- var package = require('../package.json');
- return [
- 'node-sass version: ' + package.version,
- 'libsass version: ' + package.libsass
- ].join('\n');
- };
|