Skip to content

Commit

Permalink
Merge pull request #6 from petebacondarwin/extension-option
Browse files Browse the repository at this point in the history
Extension option
  • Loading branch information
avalade committed Jul 21, 2012
2 parents b90ac32 + 341f486 commit 7d2c6ee
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
10 changes: 6 additions & 4 deletions tasks/coffee.js
Expand Up @@ -18,9 +18,10 @@ module.exports = function(grunt) {


grunt.registerMultiTask('coffee', 'Compile CoffeeScript files', function() { grunt.registerMultiTask('coffee', 'Compile CoffeeScript files', function() {
var dest = this.file.dest, var dest = this.file.dest,
options = this.data.options; options = this.data.options,
extension = this.data.extension;
grunt.file.expandFiles(this.file.src).forEach(function(filepath) { grunt.file.expandFiles(this.file.src).forEach(function(filepath) {
grunt.helper('coffee', filepath, dest, options); grunt.helper('coffee', filepath, dest, options, extension);
}); });


if (grunt.task.current.errorCount) { if (grunt.task.current.errorCount) {
Expand All @@ -32,12 +33,13 @@ module.exports = function(grunt) {
// HELPERS // HELPERS
// ========================================================================== // ==========================================================================


grunt.registerHelper('coffee', function(src, destPath, options) { grunt.registerHelper('coffee', function(src, destPath, options, extension) {
var coffee = require('coffee-script'), var coffee = require('coffee-script'),
js = ''; js = '';


destPath = destPath ? destPath : path.dirname(src); destPath = destPath ? destPath : path.dirname(src);
var dest = path.join(destPath, path.basename(src, '.coffee') + '.js'); extension = extension ? extension : '.js';
var dest = path.join(destPath, path.basename(src, '.coffee') + extension);


options = options || {}; options = options || {};
if( options.bare !== false ) { if( options.bare !== false ) {
Expand Down
26 changes: 18 additions & 8 deletions test/coffee_test.js
Expand Up @@ -26,10 +26,8 @@ fs.existsSync = fs.existsSync ? fs.existsSync : path.existsSync;


var src = 'test/fixtures/hello_world.coffee'; var src = 'test/fixtures/hello_world.coffee';
var destFolder = 'tmp/js'; var destFolder = 'tmp/js';
var relativeDest = function(src) { var dest1 = 'test/fixtures/hello_world.js';
var out = path.resolve(path.dirname(src),path.basename(src, '.coffee') + '.js'); var dest2 = 'test/fixtures/hello_world.coffee.js';
return out;
};


exports['coffee'] = { exports['coffee'] = {
setUp: function(done) { setUp: function(done) {
Expand All @@ -43,8 +41,12 @@ exports['coffee'] = {
} }
fs.rmdirSync(destFolder); fs.rmdirSync(destFolder);
} }
if (fs.existsSync(relativeDest(src))) {
fs.unlinkSync(relativeDest(src)); if (fs.existsSync(dest1)) {
fs.unlinkSync(dest1);
}
if (fs.existsSync(dest2)) {
fs.unlinkSync(dest2);
} }
done(); done();
}, },
Expand All @@ -66,10 +68,18 @@ exports['coffee'] = {
}, },


'helper-nodest': function(test) { 'helper-nodest': function(test) {
grunt.helper('coffee', [src], null); test.expect(1);
test.equal(grunt.file.read(relativeDest(src)), grunt.helper('coffee', [src]);
test.equal(grunt.file.read(dest1),
'\nconsole.log("Hello CoffeeScript!");\n', '\nconsole.log("Hello CoffeeScript!");\n',
'it should compile the coffee'); 'it should compile the coffee');
test.done(); test.done();
},

'helper-extension': function(test) {
test.expect(1);
grunt.helper('coffee', [src], null, {}, '.coffee.js');
test.ok(fs.existsSync(dest2));
test.done();
} }
}; };

0 comments on commit 7d2c6ee

Please sign in to comment.