Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

API: Simplified code usage (#547) #577

Merged
merged 16 commits into from
Dec 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion bin/node-sass
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ var Emitter = require('events').EventEmitter,
meow = require('meow'),
replaceExt = require('replace-ext'),
stdin = require('get-stdin'),
render = require('../lib/render');
render = require('../lib/render'),
fs = require('fs');

/**
* Initialize CLI
Expand All @@ -14,6 +15,8 @@ var Emitter = require('events').EventEmitter,
var cli = meow({
pkg: '../package.json',
help: [
require('../lib/').info(),
'',
'Usage',
' node-sass [options] <input.scss> [output.css]',
' cat <input.scss> | node-sass > output.css',
Expand All @@ -37,6 +40,7 @@ var cli = meow({
' --image-path Path to prepend when using the `image-url()` helper',
' --precision The amount of precision allowed in decimal numbers',
' --stdout Print the resulting CSS to stdout',
' --importer Path to custom importer',
' --help Print usage info'
].join('\n')
}, {
Expand Down Expand Up @@ -105,6 +109,10 @@ function getEmitter() {
console.log(data);
});

emitter.on('done', function(){
process.exit(0);
});

return emitter;
}

Expand Down Expand Up @@ -202,6 +210,15 @@ function run(options, emitter) {
}
}

if (options.importer) {
if (fs.existsSync(options.importer)) {
options.importer = require(options.importer);
} else {
console.error('Could not locate importer.');
process.exit(1);
}
}

if (options.watch) {
watch(options, emitter);
} else {
Expand Down Expand Up @@ -240,6 +257,7 @@ if (options.src) {
} else if (!process.stdin.isTTY) {
stdin(function(data) {
options.data = data;
options.stdin = true;
run(options, emitter);
});
}
Expand Down
160 changes: 40 additions & 120 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -65,17 +65,14 @@ function getStats(options) {
/**
* End stats
*
* @param {Object} options
* @param {Object} stats
* @param {Object} sourceMap
* @api private
*/

function endStats(options, sourceMap) {
var stats = options.stats || {};

function endStats(stats) {
stats.end = Date.now();
stats.duration = stats.end - stats.start;
stats.sourceMap = sourceMap;

return stats;
}
Expand Down Expand Up @@ -139,15 +136,12 @@ 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);

var error = options.error;
var success = options.success;
var importer = options.importer;
Expand All @@ -159,29 +153,41 @@ function getOptions(options) {
err = { message: err };
}

err.code = code;

if (error) {
error(err, code);
error(err);
}
};

options.success = function(css, sourceMap) {
sourceMap = JSON.parse(sourceMap);
options.success = function() {
options.result.sourceMap = JSON.parse(options.result.sourceMap);

endStats(options, sourceMap);
var stats = endStats(options.result.stats);

if (success) {
success(css, sourceMap);
success({
css: options.result.css,
map: options.result.sourceMap,
stats: stats
});
}
};

if (importer) {
options.importer = function(file, prev, key) {
importer(file, prev, function(data) {
var done = function(data) {
binding.importedCallback({
index: key,
objectLiteral: data
});
});
};

var result = importer(file, prev, done);

if (result) {
done(result);
}
};
}

Expand All @@ -191,6 +197,10 @@ function getOptions(options) {
delete options.source_comments;
delete options.sourceComments;

options.result = {
stats: getStats(options)
};

return options;
}

Expand All @@ -200,41 +210,6 @@ function getOptions(options) {

var binding = require(getBinding());

/**
* Render (deprecated)
*
* @param {String} css
* @param {Function} cb
* @param {Object} options
* @api private
*/

function deprecatedRender(css, cb, options) {
options = getOptions(options);
options.data = css;
options.error = cb;
options.success = function(css) {
cb(null, css);
};

binding.render(options);
}

/**
* Render sync (deprecated)
*
* @param {String} css
* @param {Object} options
* @api private
*/

function deprecatedRenderSync(css, options) {
options = getOptions(options);
options.data = css;

return binding.renderSync(options);
}

/**
* Render
*
Expand All @@ -243,12 +218,9 @@ function deprecatedRenderSync(css, options) {
*/

module.exports.render = function(options) {
if (typeof arguments[0] === 'string') {
return deprecatedRender.apply(this, arguments);
}

options = getOptions(options);
options.file ? binding.renderFile(options) : binding.render(options);

options.data ? binding.render(options) : binding.renderFile(options);
};

/**
Expand All @@ -259,79 +231,27 @@ module.exports.render = function(options) {
*/

module.exports.renderSync = function(options) {
if (typeof arguments[0] === 'string') {
return deprecatedRenderSync.apply(this, arguments);
}

var output;

options = getOptions(options);
output = options.file ? binding.renderFileSync(options) : binding.renderSync(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
*/
var result = options.data ? binding.renderSync(options) : binding.renderFileSync(options);

module.exports.renderFile = function(options) {
options = options || {};
if(result) {
options.result.stats = endStats(options.result.stats);

var outFile = options.outFile;
var success = options.success;

if (options.sourceMap === true) {
options.sourceMap = outFile + '.map';
return options.result;
}

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);
});
});
};

module.exports.render(options);
};

/**
* Middleware
* API Info
*
* @api public
*/

module.exports.middleware = function() {
return new Error([
'The middleware has been moved to',
'https://github.com/sass/node-sass-middleware'
].join(' '));
module.exports.info = function() {
var package = require('../package.json');

return [
'node-sass version: ' + package.version,
'libsass version: ' + package.libsass
].join('\n');
};
19 changes: 10 additions & 9 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ module.exports = function(options, emitter) {
sourceComments: options.sourceComments,
sourceMapEmbed: options.sourceMapEmbed,
sourceMapContents: options.sourceMapContents,
sourceMap: options.sourceMap
sourceMap: options.sourceMap,
importer: options.importer
};

if (options.src) {
Expand All @@ -31,46 +32,46 @@ 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) {
emitter.emit('done');
}
};

if (options.stdout || (!options.dest && !process.stdout.isTTY)) {
emitter.emit('log', css);
if (options.stdout || (!options.dest && !process.stdout.isTTY) || options.stdin) {
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) {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "node-sass",
"version": "1.2.3",
"version": "2.0.0-beta",
"libsass": "3.1.0-beta",
"description": "Wrapper around libsass",
"license": "MIT",
"homepage": "https://github.com/sass/node-sass",
Expand Down Expand Up @@ -50,7 +51,7 @@
"mkdirp": "^0.5.0",
"mocha": "^2.0.1",
"nan": "^1.3.0",
"object-assign": "^1.0.0",
"object-assign": "^2.0.0",
"replace-ext": "0.0.1",
"request": "^2.48.0",
"shelljs": "^0.3.0"
Expand Down
Loading