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 "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