Commit 974f93e7 by Adeel

Importer: Adds support for CLI usage.

parent 52554b83
...@@ -5,7 +5,8 @@ var Emitter = require('events').EventEmitter, ...@@ -5,7 +5,8 @@ var Emitter = require('events').EventEmitter,
meow = require('meow'), meow = require('meow'),
replaceExt = require('replace-ext'), replaceExt = require('replace-ext'),
stdin = require('get-stdin'), stdin = require('get-stdin'),
render = require('../lib/render'); render = require('../lib/render'),
fs = require('fs');
/** /**
* Initialize CLI * Initialize CLI
...@@ -39,6 +40,7 @@ var cli = meow({ ...@@ -39,6 +40,7 @@ var cli = meow({
' --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',
' --stdout Print the resulting CSS to stdout', ' --stdout Print the resulting CSS to stdout',
' --importer Path to custom importer',
' --help Print usage info' ' --help Print usage info'
].join('\n') ].join('\n')
}, { }, {
...@@ -107,6 +109,10 @@ function getEmitter() { ...@@ -107,6 +109,10 @@ function getEmitter() {
console.log(data); console.log(data);
}); });
emitter.on('done', function(){
process.exit(0);
});
return emitter; return emitter;
} }
...@@ -204,6 +210,15 @@ function run(options, emitter) { ...@@ -204,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) { if (options.watch) {
watch(options, emitter); watch(options, emitter);
} else { } else {
......
...@@ -22,7 +22,8 @@ module.exports = function(options, emitter) { ...@@ -22,7 +22,8 @@ module.exports = function(options, emitter) {
sourceComments: options.sourceComments, sourceComments: options.sourceComments,
sourceMapEmbed: options.sourceMapEmbed, sourceMapEmbed: options.sourceMapEmbed,
sourceMapContents: options.sourceMapContents, sourceMapContents: options.sourceMapContents,
sourceMap: options.sourceMap sourceMap: options.sourceMap,
importer: options.importer
}; };
if (options.src) { if (options.src) {
......
...@@ -223,4 +223,106 @@ describe('cli', function() { ...@@ -223,4 +223,106 @@ describe('cli', function() {
}); });
}); });
}); });
describe('importer', function() {
var dest = fixture('include-files/index.css');
var src = fixture('include-files/index.scss');
var expected = read(fixture('include-files/expected-importer.css'), 'utf8').trim().replace(/\r\n/g, '\n');
it('should override imports and fire callback with file and contents', function(done) {
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--importer', fixture('extras/my_custom_importer_file_and_data_cb.js')
]);
bin.on('close', function () {
assert.equal(read(dest, 'utf8').trim(), expected);
fs.unlinkSync(dest);
done();
});
});
it('should override imports and fire callback with file', function(done) {
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--importer', fixture('extras/my_custom_importer_file_cb.js')
]);
bin.on('close', function () {
if (fs.existsSync(dest)) {
assert.equal(read(dest, 'utf8').trim(), '');
fs.unlinkSync(dest);
}
done();
});
});
it('should override imports and fire callback with data', function(done) {
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--importer', fixture('extras/my_custom_importer_data_cb.js')
]);
bin.on('close', function () {
assert.equal(read(dest, 'utf8').trim(), expected);
fs.unlinkSync(dest);
done();
});
});
it('should override imports and return file and contents', function(done) {
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--importer', fixture('extras/my_custom_importer_file_and_data.js')
]);
bin.on('close', function () {
assert.equal(read(dest, 'utf8').trim(), expected);
fs.unlinkSync(dest);
done();
});
});
it('should override imports and return file', function(done) {
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--importer', fixture('extras/my_custom_importer_file.js')
]);
bin.on('close', function () {
if (fs.existsSync(dest)) {
assert.equal(read(dest, 'utf8').trim(), '');
fs.unlinkSync(dest);
}
done();
});
});
it('should override imports and return data', function(done) {
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--importer', fixture('extras/my_custom_importer_data.js')
]);
bin.on('close', function () {
assert.equal(read(dest, 'utf8').trim(), expected);
fs.unlinkSync(dest);
done();
});
});
it('should return error on for invalid importer file path', function(done) {
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--importer', fixture('non/existing/path')
]);
bin.on('close', function (code) {
assert(code !== 0);
done();
});
});
});
}); });
module.exports = function() {
return {
contents: 'div {color: yellow;}'
};
};
module.exports = function(file, prev, done) {
done({
contents: 'div {color: yellow;}'
});
};
var path = require('path');
module.exports = function(file) {console.log('>>>>>>>>>>');console.log(path.resolve(path.join(process.cwd(), 'test/fixtures/include-files/', file + (path.extname(file) ? '' : '.scss'))));
return {
file: path.resolve(path.join(process.cwd(), 'test/fixtures/include-files/', file + (path.extname(file) ? '' : '.scss')))
};
};
module.exports = function() {
return {
file: '/some/random/path/file.scss',
contents: 'div {color: yellow;}'
};
};
module.exports = function(file, prev, done) {
done({
file: '/some/random/path/file.scss',
contents: 'div {color: yellow;}'
});
};
var path = require('path');
module.exports = function(file, /* jshint unused:false */ prev, done) {
done({
file: path.resolve(path.join(process.cwd(), 'test/fixtures/include-files/', file + (path.extname(file) ? '' : '.scss')))
});
};
div {
color: yellow; }
div {
color: yellow; }
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