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({
' --output-style CSS output style (nested|expanded|compact|compressed)',
' --source-comments Include debug info in output',
' --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',
' --image-path Path to prepend when using the `image-url()` helper',
' --precision The amount of precision allowed in decimal numbers',
......@@ -43,6 +45,8 @@ var cli = meow({
'omit-source-map-url',
'recursive',
'stdout',
'source-map-embed',
'source-map-contents',
'source-comments'
],
string: [
......@@ -116,7 +120,17 @@ function getOptions(args, options) {
var dir = options.output || process.cwd();
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) {
var ext = path.extname(options.src);
......
......@@ -29,8 +29,10 @@
'src/libsass/remove_placeholders.cpp',
'src/libsass/sass.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_values.cpp',
'src/libsass/source_map.cpp',
'src/libsass/to_c.cpp',
'src/libsass/to_string.cpp',
......
......@@ -36,11 +36,11 @@ function getOutFile(options) {
var outFile = options.outFile;
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 + '$'), '')) {
return false;
if (path.resolve(outFile) === path.normalize(outFile).replace(/(.+)([\/|\\])$/, '$1')) {
return outFile;
}
return path.resolve(path.dirname(file), outFile);
......@@ -135,7 +135,7 @@ function getOptions(options) {
options.data = options.data || null;
options.file = options.file || null;
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.precision = parseInt(options.precision) || 5;
options.sourceMap = getSourceMap(options);
......@@ -152,12 +152,20 @@ function getOptions(options) {
var success = options.success;
options.error = function(err, code) {
try {
err = JSON.parse(err);
} catch (e) {
err = { message: err };
}
if (error) {
error(err, code);
}
};
options.success = function(css, sourceMap) {
sourceMap = JSON.parse(sourceMap);
endStats(options, sourceMap);
if (success) {
......@@ -248,7 +256,7 @@ module.exports.renderSync = function(options) {
options = getOptions(options);
output = options.file ? binding.renderFileSync(options) : binding.renderSync(options);
endStats(options, options.stats.sourceMap);
endStats(options, JSON.parse(options.stats.sourceMap));
return output;
};
......@@ -290,7 +298,7 @@ module.exports.renderFile = function(options) {
var dir = path.dirname(outFile);
var sourceMapFile = path.resolve(dir, options.sourceMap);
fs.writeFile(sourceMapFile, sourceMap, function(err) {
fs.writeFile(sourceMapFile, JSON.stringify(sourceMap), function(err) {
if (err) {
return options.error(err);
}
......
......@@ -20,6 +20,8 @@ module.exports = function(options, emitter) {
outputStyle: options.outputStyle,
precision: options.precision,
sourceComments: options.sourceComments,
sourceMapEmbed: options.sourceMapEmbed,
sourceMapContents: options.sourceMapContents,
sourceMap: options.sourceMap
};
......@@ -46,7 +48,7 @@ module.exports = function(options, emitter) {
fs.writeFile(options.dest, css, function(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));
......@@ -72,7 +74,7 @@ module.exports = function(options, emitter) {
};
renderOptions.error = function(error) {
emitter.emit('error', chalk.red(error));
emitter.emit('error', chalk.red(JSON.stringify(error, null, 2)));
};
sass.render(renderOptions);
......
#include <nan.h>
#include <string>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include "sass_context_wrapper.h"
using namespace v8;
using namespace std;
void WorkOnContext(uv_work_t* req) {
sass_context_wrapper* ctx_w = static_cast<sass_context_wrapper*>(req->data);
if (ctx_w->ctx) {
sass_context* ctx = static_cast<sass_context*>(ctx_w->ctx);
sass_compile(ctx);
} else if (ctx_w->fctx) {
sass_file_context* ctx = static_cast<sass_file_context*>(ctx_w->fctx);
sass_compile_file(ctx);
}
}
char* CreateString(Local<Value> value) {
if(value->IsNull() || !value->IsString()) {
return const_cast<char*>(""); // return empty string.
......@@ -30,10 +12,7 @@ char* CreateString(Local<Value> value) {
return str;
}
void ExtractOptions(Local<Value> optionsValue, void* cptr, sass_context_wrapper* ctx_w, bool isFile) {
bool source_comments;
Local<Object> options = optionsValue->ToObject();
void ExtractOptions(Local<Object> options, void* cptr, sass_context_wrapper* ctx_w, bool isFile) {
if (ctx_w) {
NanAssignPersistent(ctx_w->stats, options->Get(NanNew("stats"))->ToObject());
......@@ -41,66 +20,62 @@ void ExtractOptions(Local<Value> optionsValue, void* cptr, sass_context_wrapper*
Local<Function> callback = Local<Function>::Cast(options->Get(NanNew("success")));
Local<Function> errorCallback = Local<Function>::Cast(options->Get(NanNew("error")));
if (isFile) {
ctx_w->fctx = (sass_file_context*) cptr;
ctx_w->fctx = (struct Sass_File_Context*) cptr;
} else {
ctx_w->ctx = (sass_context*) cptr;
ctx_w->dctx = (struct Sass_Data_Context*) cptr;
}
ctx_w->request.data = ctx_w;
ctx_w->callback = new NanCallback(callback);
ctx_w->errorCallback = new NanCallback(errorCallback);
}
struct Sass_Context* ctx;
if (isFile) {
sass_file_context* ctx = (sass_file_context*) cptr;
ctx->input_path = CreateString(options->Get(NanNew("file")));
ctx->output_path = CreateString(options->Get(NanNew("outFile")));
ctx->options.image_path = CreateString(options->Get(NanNew("imagePath")));
ctx->options.output_style = options->Get(NanNew("style"))->Int32Value();
ctx->options.source_comments = source_comments = options->Get(NanNew("comments"))->BooleanValue();
ctx->options.omit_source_map_url = options->Get(NanNew("omitSourceMapUrl"))->BooleanValue();
ctx->options.source_map_file = CreateString(options->Get(NanNew("sourceMap")));
ctx->options.include_paths = CreateString(options->Get(NanNew("paths")));
ctx->options.precision = options->Get(NanNew("precision"))->Int32Value();
ctx = sass_file_context_get_context((struct Sass_File_Context*) cptr);
} else {
sass_context* ctx = (sass_context*) cptr;
ctx->source_string = CreateString(options->Get(NanNew("data")));
ctx->output_path = CreateString(options->Get(NanNew("outFile")));
ctx->options.is_indented_syntax_src = options->Get(NanNew("indentedSyntax"))->BooleanValue();
ctx->options.image_path = CreateString(options->Get(NanNew("imagePath")));
ctx->options.output_style = options->Get(NanNew("style"))->Int32Value();
ctx->options.source_comments = source_comments = options->Get(NanNew("comments"))->BooleanValue();
ctx->options.omit_source_map_url = options->Get(NanNew("omitSourceMapUrl"))->BooleanValue();
ctx->options.source_map_file = CreateString(options->Get(NanNew("sourceMap")));
ctx->options.include_paths = CreateString(options->Get(NanNew("paths")));
ctx->options.precision = options->Get(NanNew("precision"))->Int32Value();
ctx = sass_data_context_get_context((struct Sass_Data_Context*) cptr);
}
struct Sass_Options* sass_options = sass_context_get_options(ctx);
sass_option_set_output_path(sass_options, CreateString(options->Get(NanNew("outFile"))));
sass_option_set_image_path(sass_options, CreateString(options->Get(NanNew("imagePath"))));
sass_option_set_output_style(sass_options, (Sass_Output_Style)options->Get(NanNew("style"))->Int32Value());
sass_option_set_is_indented_syntax_src(sass_options, options->Get(NanNew("indentedSyntax"))->BooleanValue());
sass_option_set_source_comments(sass_options, options->Get(NanNew("comments"))->BooleanValue());
sass_option_set_omit_source_map_url(sass_options, options->Get(NanNew("omitSourceMapUrl"))->BooleanValue());
sass_option_set_source_map_embed(sass_options, options->Get(NanNew("sourceMapEmbed"))->BooleanValue());
sass_option_set_source_map_contents(sass_options, options->Get(NanNew("sourceMapContents"))->BooleanValue());
sass_option_set_source_map_file(sass_options, CreateString(options->Get(NanNew("sourceMap"))));
sass_option_set_include_path(sass_options, CreateString(options->Get(NanNew("paths"))));
sass_option_set_precision(sass_options, options->Get(NanNew("precision"))->Int32Value());
}
template<typename Ctx>
void FillStatsObj(Handle<Object> stats, Ctx ctx) {
int i;
Handle<Array> arr;
void FillStatsObj(Handle<Object> stats, Sass_Context* ctx) {
char** included_files = sass_context_get_included_files(ctx);
Handle<Array> arr = NanNew<Array>();
arr = NanNew<Array>(ctx->num_included_files);
for (i = 0; i < ctx->num_included_files; i++) {
arr->Set(i, NanNew<String>(ctx->included_files[i]));
if(included_files) {
for (int i = 0; included_files[i] != nullptr; ++i) {
arr->Set(i, NanNew<String>(included_files[i]));
}
}
(*stats)->Set(NanNew("includedFiles"), arr);
}
void FillStatsObj(Handle<Object> stats, sass_file_context* ctx) {
Handle<Value> source_map;
FillStatsObj<sass_file_context*>(stats, ctx);
if (ctx->error_status) {
if (sass_context_get_error_status(ctx)) {
return;
}
if (ctx->source_map_string) {
source_map = NanNew<String>(ctx->source_map_string);
if (sass_context_get_source_map_string(ctx)) {
source_map = NanNew<String>(sass_context_get_source_map_string(ctx));
} else {
source_map = NanNull();
source_map = NanNew<String>("{}");
}
(*stats)->Set(NanNew("sourceMap"), source_map);
}
......@@ -109,17 +84,22 @@ void MakeCallback(uv_work_t* req) {
TryCatch try_catch;
sass_context_wrapper* ctx_w = static_cast<sass_context_wrapper*>(req->data);
int error_status = ctx_w->ctx ? ctx_w->ctx->error_status : ctx_w->fctx->error_status;
int error_status;
struct Sass_Context* ctx;
if (ctx_w->ctx) {
FillStatsObj(NanNew(ctx_w->stats), ctx_w->ctx);
if (ctx_w->dctx) {
ctx = sass_data_context_get_context(ctx_w->dctx);
FillStatsObj(NanNew(ctx_w->stats), ctx);
error_status = sass_context_get_error_status(ctx);
} else {
FillStatsObj(NanNew(ctx_w->stats), ctx_w->fctx);
ctx = sass_file_context_get_context(ctx_w->fctx);
FillStatsObj(NanNew(ctx_w->stats), ctx);
error_status = sass_context_get_error_status(ctx);
}
if (error_status == 0) {
// if no error, do callback(null, result)
char* val = ctx_w->ctx ? ctx_w->ctx->output_string : ctx_w->fctx->output_string;
const char* val = sass_context_get_output_string(ctx);
Local<Value> argv[] = {
NanNew<String>(val),
NanNew(ctx_w->stats)->Get(NanNew("sourceMap"))
......@@ -127,7 +107,7 @@ void MakeCallback(uv_work_t* req) {
ctx_w->callback->Call(2, argv);
} else {
// if error, do callback(error)
char* err = ctx_w->ctx ? ctx_w->ctx->error_message : ctx_w->fctx->error_message;
const char* err = sass_context_get_error_json(ctx);
Local<Value> argv[] = {
NanNew<String>(err),
NanNew<Integer>(error_status)
......@@ -137,18 +117,24 @@ void MakeCallback(uv_work_t* req) {
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
sass_free_context_wrapper(ctx_w);
}
NAN_METHOD(Render) {
NanScope();
sass_context* ctx = sass_new_context();
sass_context_wrapper* ctx_w = sass_new_context_wrapper();
ctx_w->ctx = ctx;
ExtractOptions(args[0], ctx, ctx_w, false);
Local<Object> options = args[0]->ToObject();
char* source_string = CreateString(options->Get(NanNew("data")));
struct Sass_Data_Context* dctx = sass_make_data_context(source_string);
sass_context_wrapper* ctx_w = sass_make_context_wrapper();
ctx_w->dctx = dctx;
ExtractOptions(options, dctx, ctx_w, false);
int status = uv_queue_work(uv_default_loop(), &ctx_w->request, compile_it, (uv_after_work_cb)MakeCallback);
int status = uv_queue_work(uv_default_loop(), &ctx_w->request, WorkOnContext, (uv_after_work_cb)MakeCallback);
assert(status == 0);
NanReturnUndefined();
......@@ -156,58 +142,75 @@ NAN_METHOD(Render) {
NAN_METHOD(RenderSync) {
NanScope();
Handle<Object> options = args[0]->ToObject();
sass_context* ctx = sass_new_context();
ExtractOptions(args[0], ctx, NULL, false);
sass_compile(ctx);
Local<Object> options = args[0]->ToObject();
char* source_string = CreateString(options->Get(NanNew("data")));
struct Sass_Data_Context* dctx = sass_make_data_context(source_string);
struct Sass_Context* ctx = sass_data_context_get_context(dctx);
ExtractOptions(options, dctx, NULL, false);
compile_data(dctx);
FillStatsObj(options->Get(NanNew("stats"))->ToObject(), ctx);
if (ctx->error_status == 0) {
Local<String> output = NanNew<String>(ctx->output_string);
free_context(ctx);
if (sass_context_get_error_status(ctx) == 0) {
Local<String> output = NanNew<String>(sass_context_get_output_string(ctx));
sass_delete_data_context(dctx);
NanReturnValue(output);
}
Local<String> error = NanNew<String>(ctx->error_message);
free_context(ctx);
Local<String> error = NanNew<String>(sass_context_get_error_json(ctx));
sass_delete_data_context(dctx);
NanThrowError(error);
NanReturnUndefined();
}
NAN_METHOD(RenderFile) {
NanScope();
sass_file_context* fctx = sass_new_file_context();
sass_context_wrapper* ctx_w = sass_new_context_wrapper();
Local<Object> options = args[0]->ToObject();
char* input_path = CreateString(options->Get(NanNew("file")));
struct Sass_File_Context* fctx = sass_make_file_context(input_path);
sass_context_wrapper* ctx_w = sass_make_context_wrapper();
ctx_w->fctx = fctx;
ExtractOptions(args[0], fctx, ctx_w, true);
ExtractOptions(options, fctx, ctx_w, true);
int status = uv_queue_work(uv_default_loop(), &ctx_w->request, compile_it, (uv_after_work_cb)MakeCallback);
int status = uv_queue_work(uv_default_loop(), &ctx_w->request, WorkOnContext, (uv_after_work_cb)MakeCallback);
assert(status == 0);
free(input_path);
NanReturnUndefined();
}
NAN_METHOD(RenderFileSync) {
NanScope();
sass_file_context* ctx = sass_new_file_context();
ExtractOptions(args[0], ctx, NULL, true);
Handle<Object> options = args[0]->ToObject();
sass_compile_file(ctx);
Local<Object> options = args[0]->ToObject();
char* input_path = CreateString(options->Get(NanNew("file")));
struct Sass_File_Context* fctx = sass_make_file_context(input_path);
struct Sass_Context* ctx = sass_file_context_get_context(fctx);
ExtractOptions(options, fctx, NULL, true);
compile_file(fctx);
FillStatsObj(options->Get(NanNew("stats"))->ToObject(), ctx);
free(input_path);
if (ctx->error_status == 0) {
Local<String> output = NanNew<String>(ctx->output_string);
free_file_context(ctx);
if (sass_context_get_error_status(ctx) == 0) {
Local<String> output = NanNew<String>(sass_context_get_output_string(ctx));
sass_delete_file_context(fctx);
NanReturnValue(output);
}
Local<String> error = NanNew<String>(ctx->error_message);
free_file_context(ctx);
Local<String> error = NanNew<String>(sass_context_get_error_json(ctx));
sass_delete_file_context(fctx);
NanThrowError(error);
NanReturnUndefined();
}
......
#include "sass_context_wrapper.h"
#include <nan.h>
#include <cstdlib>
extern "C" {
using namespace std;
void free_context(sass_context* ctx) {
delete[] ctx->source_string;
delete[] ctx->options.include_paths;
delete[] ctx->options.image_path;
sass_free_context(ctx);
void compile_it(uv_work_t* req) {
sass_context_wrapper* ctx_w = static_cast<sass_context_wrapper*>(req->data);
if (ctx_w->dctx) {
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) {
delete[] fctx->input_path;
delete[] fctx->options.include_paths;
delete[] fctx->options.image_path;
sass_free_file_context(fctx);
void compile_file(struct Sass_File_Context* fctx) {
sass_compile_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));
}
void sass_free_context_wrapper(sass_context_wrapper* ctx_w) {
if (ctx_w->ctx) {
free_context(ctx_w->ctx);
if (ctx_w->dctx) {
sass_delete_data_context(ctx_w->dctx);
} else if (ctx_w->fctx) {
free_file_context(ctx_w->fctx);
sass_delete_file_context(ctx_w->fctx);
}
NanDisposePersistent(ctx_w->stats);
......
#include "libsass/sass_interface.h"
#include <nan.h>
#include "libsass/sass_context.h"
#ifdef __cplusplus
extern "C" {
......@@ -7,20 +7,21 @@ extern "C" {
using namespace v8;
void free_context(sass_context* ctx);
void free_file_context(sass_file_context* fctx);
void compile_data(struct Sass_Data_Context* dctx);
void compile_file(struct Sass_File_Context* fctx);
void compile_it(uv_work_t* req);
struct sass_context_wrapper {
sass_context* ctx;
sass_file_context* fctx;
Sass_Data_Context* dctx;
Sass_File_Context* fctx;
Persistent<Object> stats;
uv_work_t request;
NanCallback* callback;
NanCallback* errorCallback;
};
struct sass_context_wrapper* sass_new_context_wrapper(void);
void sass_free_context_wrapper(struct sass_context_wrapper* ctx);
struct sass_context_wrapper* sass_make_context_wrapper(void);
void sass_free_context_wrapper(struct sass_context_wrapper* ctx_w);
#ifdef __cplusplus
}
......
......@@ -187,8 +187,7 @@ describe('api', function() {
var stats = {};
var expected = [
fixture('include-files/bar.scss').replace(/\\/g, '/'),
fixture('include-files/foo.scss').replace(/\\/g, '/'),
'stdin'
fixture('include-files/foo.scss').replace(/\\/g, '/')
];
sass.render({
......@@ -422,8 +421,7 @@ describe('api', function() {
stats: stats,
sourceMap: true,
success: function() {
var map = JSON.parse(stats.sourceMap);
assert.equal(map.sources[0], 'index.scss');
assert.equal(stats.sourceMap.sources[0], 'index.scss');
done();
}
});
......@@ -517,8 +515,7 @@ describe('api', function() {
sourceMap: true
});
var map = JSON.parse(stats.sourceMap);
assert.equal(map.sources[0], 'index.scss');
assert.equal(stats.sourceMap.sources[0], 'index.scss');
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