Skip to content

Commit

Permalink
Added option to specify the extension of the generated file.
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Jul 20, 2012
1 parent 9b89279 commit 9d63a16
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
5 changes: 3 additions & 2 deletions tasks/coffee.js
Expand Up @@ -32,12 +32,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 9d63a16

Please sign in to comment.