Commit 7c354bf6 by Adeel Mujahid

Merge pull request #543 from am11/master

Binding: Refurbishes binding to use new API
parents e9b69ab6 187cfde2
...@@ -31,6 +31,8 @@ var cli = meow({ ...@@ -31,6 +31,8 @@ var cli = meow({
' --output-style CSS output style (nested|expanded|compact|compressed)', ' --output-style CSS output style (nested|expanded|compact|compressed)',
' --source-comments Include debug info in output', ' --source-comments Include debug info in output',
' --source-map Emit source map', ' --source-map Emit source map',
' --source-map-embed Embed sourceMappingUrl as data URI',
' --source-map-contents Embed include contents in map',
' --include-path Path to look for imported files', ' --include-path Path to look for imported files',
' --image-path Path to prepend when using the `image-url()` helper', ' --image-path Path to prepend when using the `image-url()` helper',
' --precision The amount of precision allowed in decimal numbers', ' --precision The amount of precision allowed in decimal numbers',
...@@ -43,6 +45,8 @@ var cli = meow({ ...@@ -43,6 +45,8 @@ var cli = meow({
'omit-source-map-url', 'omit-source-map-url',
'recursive', 'recursive',
'stdout', 'stdout',
'source-map-embed',
'source-map-contents',
'source-comments' 'source-comments'
], ],
string: [ string: [
...@@ -116,7 +120,17 @@ function getOptions(args, options) { ...@@ -116,7 +120,17 @@ function getOptions(args, options) {
var dir = options.output || process.cwd(); var dir = options.output || process.cwd();
options.src = args[0]; options.src = args[0];
options.dest = args[1] ? path.join(dir, args[1]) : null; options.dest = null;
if (args[1]) { // destination is available
// now first check if the destination is absolute path
// see http://stackoverflow.com/a/24225816/863980 for Marc Diethelm's comment
if (path.resolve(args[1]) === path.normalize(args[1]).replace(/(.+)([\/|\\])$/, '$1')) {
options.dest = args[1];
} else { // since dest path is relative, resolve it w.r.t input
options.dest = path.join(dir, args[1]);
}
}
if (!options.dest && !options.stdout) { if (!options.dest && !options.stdout) {
var ext = path.extname(options.src); var ext = path.extname(options.src);
......
...@@ -29,8 +29,10 @@ ...@@ -29,8 +29,10 @@
'src/libsass/remove_placeholders.cpp', 'src/libsass/remove_placeholders.cpp',
'src/libsass/sass.cpp', 'src/libsass/sass.cpp',
'src/libsass/sass2scss.cpp', 'src/libsass/sass2scss.cpp',
'src/libsass/sass_interface.cpp', 'src/libsass/sass_context.cpp',
'src/libsass/sass_functions.cpp',
'src/libsass/sass_util.cpp', 'src/libsass/sass_util.cpp',
'src/libsass/sass_values.cpp',
'src/libsass/source_map.cpp', 'src/libsass/source_map.cpp',
'src/libsass/to_c.cpp', 'src/libsass/to_c.cpp',
'src/libsass/to_string.cpp', 'src/libsass/to_string.cpp',
......
...@@ -36,11 +36,11 @@ function getOutFile(options) { ...@@ -36,11 +36,11 @@ function getOutFile(options) {
var outFile = options.outFile; var outFile = options.outFile;
if (!file || !outFile || typeof outFile !== 'string' || typeof file !== 'string') { if (!file || !outFile || typeof outFile !== 'string' || typeof file !== 'string') {
return false; return null;
} }
if (path.resolve(outFile) !== path.normalize(outFile).replace(new RegExp(path.sep + '$'), '')) { if (path.resolve(outFile) === path.normalize(outFile).replace(/(.+)([\/|\\])$/, '$1')) {
return false; return outFile;
} }
return path.resolve(path.dirname(file), outFile); return path.resolve(path.dirname(file), outFile);
...@@ -135,7 +135,7 @@ function getOptions(options) { ...@@ -135,7 +135,7 @@ function getOptions(options) {
options.data = options.data || null; options.data = options.data || null;
options.file = options.file || null; options.file = options.file || null;
options.imagePath = options.image_path || options.imagePath || ''; options.imagePath = options.image_path || options.imagePath || '';
options.outFile = getOutFile(options) || null; options.outFile = getOutFile(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);
...@@ -152,12 +152,20 @@ function getOptions(options) { ...@@ -152,12 +152,20 @@ function getOptions(options) {
var success = options.success; var success = options.success;
options.error = function(err, code) { options.error = function(err, code) {
try {
err = JSON.parse(err);
} catch (e) {
err = { message: err };
}
if (error) { if (error) {
error(err, code); error(err, code);
} }
}; };
options.success = function(css, sourceMap) { options.success = function(css, sourceMap) {
sourceMap = JSON.parse(sourceMap);
endStats(options, sourceMap); endStats(options, sourceMap);
if (success) { if (success) {
...@@ -248,7 +256,7 @@ module.exports.renderSync = function(options) { ...@@ -248,7 +256,7 @@ module.exports.renderSync = function(options) {
options = getOptions(options); options = getOptions(options);
output = options.file ? binding.renderFileSync(options) : binding.renderSync(options); output = options.file ? binding.renderFileSync(options) : binding.renderSync(options);
endStats(options, options.stats.sourceMap); endStats(options, JSON.parse(options.stats.sourceMap));
return output; return output;
}; };
...@@ -290,7 +298,7 @@ module.exports.renderFile = function(options) { ...@@ -290,7 +298,7 @@ module.exports.renderFile = function(options) {
var dir = path.dirname(outFile); var dir = path.dirname(outFile);
var sourceMapFile = path.resolve(dir, options.sourceMap); var sourceMapFile = path.resolve(dir, options.sourceMap);
fs.writeFile(sourceMapFile, sourceMap, function(err) { fs.writeFile(sourceMapFile, JSON.stringify(sourceMap), function(err) {
if (err) { if (err) {
return options.error(err); return options.error(err);
} }
......
...@@ -20,6 +20,8 @@ module.exports = function(options, emitter) { ...@@ -20,6 +20,8 @@ module.exports = function(options, emitter) {
outputStyle: options.outputStyle, outputStyle: options.outputStyle,
precision: options.precision, precision: options.precision,
sourceComments: options.sourceComments, sourceComments: options.sourceComments,
sourceMapEmbed: options.sourceMapEmbed,
sourceMapContents: options.sourceMapContents,
sourceMap: options.sourceMap sourceMap: options.sourceMap
}; };
...@@ -46,7 +48,7 @@ module.exports = function(options, emitter) { ...@@ -46,7 +48,7 @@ module.exports = function(options, emitter) {
fs.writeFile(options.dest, css, function(err) { fs.writeFile(options.dest, css, function(err) {
if (err) { if (err) {
return emitter.emit('error', chalk.red('Error: ' + 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));
...@@ -72,7 +74,7 @@ module.exports = function(options, emitter) { ...@@ -72,7 +74,7 @@ module.exports = function(options, emitter) {
}; };
renderOptions.error = function(error) { renderOptions.error = function(error) {
emitter.emit('error', chalk.red(error)); emitter.emit('error', chalk.red(JSON.stringify(error, null, 2)));
}; };
sass.render(renderOptions); sass.render(renderOptions);
......
#include "sass_context_wrapper.h" #include "sass_context_wrapper.h"
#include <nan.h>
#include <cstdlib>
extern "C" { extern "C" {
using namespace std; using namespace std;
void free_context(sass_context* ctx) { void compile_it(uv_work_t* req) {
delete[] ctx->source_string; sass_context_wrapper* ctx_w = static_cast<sass_context_wrapper*>(req->data);
delete[] ctx->options.include_paths;
delete[] ctx->options.image_path; if (ctx_w->dctx) {
sass_free_context(ctx); compile_data(ctx_w->dctx);
} else if (ctx_w->fctx) {
compile_file(ctx_w->fctx);
}
}
void compile_data(struct Sass_Data_Context* dctx) {
sass_compile_data_context(dctx);
} }
void free_file_context(sass_file_context* fctx) { void compile_file(struct Sass_File_Context* fctx) {
delete[] fctx->input_path; sass_compile_file_context(fctx);
delete[] fctx->options.include_paths;
delete[] fctx->options.image_path;
sass_free_file_context(fctx);
} }
sass_context_wrapper* sass_new_context_wrapper() { sass_context_wrapper* sass_make_context_wrapper() {
return (sass_context_wrapper*) calloc(1, sizeof(sass_context_wrapper)); return (sass_context_wrapper*) calloc(1, sizeof(sass_context_wrapper));
} }
void sass_free_context_wrapper(sass_context_wrapper* ctx_w) { void sass_free_context_wrapper(sass_context_wrapper* ctx_w) {
if (ctx_w->ctx) { if (ctx_w->dctx) {
free_context(ctx_w->ctx); sass_delete_data_context(ctx_w->dctx);
} else if (ctx_w->fctx) { } else if (ctx_w->fctx) {
free_file_context(ctx_w->fctx); sass_delete_file_context(ctx_w->fctx);
} }
NanDisposePersistent(ctx_w->stats); NanDisposePersistent(ctx_w->stats);
......
#include "libsass/sass_interface.h"
#include <nan.h> #include <nan.h>
#include "libsass/sass_context.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
...@@ -7,20 +7,21 @@ extern "C" { ...@@ -7,20 +7,21 @@ extern "C" {
using namespace v8; using namespace v8;
void free_context(sass_context* ctx); void compile_data(struct Sass_Data_Context* dctx);
void free_file_context(sass_file_context* fctx); void compile_file(struct Sass_File_Context* fctx);
void compile_it(uv_work_t* req);
struct sass_context_wrapper { struct sass_context_wrapper {
sass_context* ctx; Sass_Data_Context* dctx;
sass_file_context* fctx; Sass_File_Context* fctx;
Persistent<Object> stats; Persistent<Object> stats;
uv_work_t request; uv_work_t request;
NanCallback* callback; NanCallback* callback;
NanCallback* errorCallback; NanCallback* errorCallback;
}; };
struct sass_context_wrapper* sass_new_context_wrapper(void); struct sass_context_wrapper* sass_make_context_wrapper(void);
void sass_free_context_wrapper(struct sass_context_wrapper* ctx); void sass_free_context_wrapper(struct sass_context_wrapper* ctx_w);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -187,8 +187,7 @@ describe('api', function() { ...@@ -187,8 +187,7 @@ describe('api', function() {
var stats = {}; var stats = {};
var expected = [ var expected = [
fixture('include-files/bar.scss').replace(/\\/g, '/'), fixture('include-files/bar.scss').replace(/\\/g, '/'),
fixture('include-files/foo.scss').replace(/\\/g, '/'), fixture('include-files/foo.scss').replace(/\\/g, '/')
'stdin'
]; ];
sass.render({ sass.render({
...@@ -422,8 +421,7 @@ describe('api', function() { ...@@ -422,8 +421,7 @@ describe('api', function() {
stats: stats, stats: stats,
sourceMap: true, sourceMap: true,
success: function() { success: function() {
var map = JSON.parse(stats.sourceMap); assert.equal(stats.sourceMap.sources[0], 'index.scss');
assert.equal(map.sources[0], 'index.scss');
done(); done();
} }
}); });
...@@ -517,8 +515,7 @@ describe('api', function() { ...@@ -517,8 +515,7 @@ describe('api', function() {
sourceMap: true sourceMap: true
}); });
var map = JSON.parse(stats.sourceMap); assert.equal(stats.sourceMap.sources[0], 'index.scss');
assert.equal(map.sources[0], 'index.scss');
done(); done();
}); });
}); });
......
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