Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension option #6

Merged
merged 7 commits into from Jul 21, 2012
17 changes: 10 additions & 7 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 = '';


var dest = path.join(destPath, destPath = destPath ? destPath : path.dirname(src);
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 All @@ -46,10 +48,11 @@ module.exports = function(grunt) {


try { try {
js = coffee.compile(grunt.file.read(src), options); js = coffee.compile(grunt.file.read(src), options);
grunt.file.write(dest, js);
} catch (e) { } catch (e) {
grunt.log.error("Error in " + src + ":\n" + e); grunt.log.error("Error in " + src + ":\n" + e);
return false;
} }
if (this.errorCount) { return false; }
grunt.file.write(dest, js);
}); });

}; };
62 changes: 46 additions & 16 deletions test/coffee_test.js
Expand Up @@ -2,6 +2,8 @@ var grunt = require('grunt'),
fs = require('fs'), fs = require('fs'),
path = require('path'); path = require('path');


fs.existsSync = fs.existsSync ? fs.existsSync : path.existsSync;

/* /*
======== A Handy Little Nodeunit Reference ======== ======== A Handy Little Nodeunit Reference ========
https://github.com/caolan/nodeunit https://github.com/caolan/nodeunit
Expand All @@ -22,34 +24,62 @@ var grunt = require('grunt'),
test.ifError(value) test.ifError(value)
*/ */


var src = 'test/fixtures/hello_world.coffee';
var destFolder = 'tmp/js';
var dest1 = 'test/fixtures/hello_world.js';
var dest2 = 'test/fixtures/hello_world.coffee.js';

exports['coffee'] = { exports['coffee'] = {
setUp: function(done) { setUp: function(done) {
// setup here done();
fs.exists('tmp/coffee', function(exists) { },
if (exists) {
fs.rmdir('tmp/coffee', done); tearDown: function(done) {
} else { if (fs.existsSync(destFolder)) {
done(); if ( fs.existsSync(destFolder + '/hello_world.js') ) {
fs.unlinkSync(destFolder + '/hello_world.js');
} }
}); fs.rmdirSync(destFolder);
}

if (fs.existsSync(dest1)) {
fs.unlinkSync(dest1);
}
if (fs.existsSync(dest2)) {
fs.unlinkSync(dest2);
}
done();
}, },

'helper': function(test) { 'helper': function(test) {
test.expect(2); test.expect(2);
var files = [
'test/fixtures/hello_world.coffee' grunt.helper('coffee', [src], destFolder);
]; test.equal(grunt.file.read(destFolder + '/hello_world.js'),
var dest = 'tmp/js';
// tests here
grunt.helper('coffee', files, dest);
test.equal(grunt.file.read(dest + '/hello_world.js'),
'\nconsole.log("Hello CoffeeScript!");\n', '\nconsole.log("Hello CoffeeScript!");\n',
'it should compile the coffee'); 'it should compile the coffee');


grunt.helper('coffee', files, dest, { bare:false }); grunt.helper('coffee', [src], destFolder, { bare:false });
test.equal(grunt.file.read(dest + '/hello_world.js'), test.equal(grunt.file.read(destFolder + '/hello_world.js'),
'(function() {\n\n console.log("Hello CoffeeScript!");\n\n}).call(this);\n', '(function() {\n\n console.log("Hello CoffeeScript!");\n\n}).call(this);\n',
'it should compile the coffee'); 'it should compile the coffee');


test.done(); test.done();
},

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

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