Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Extension option #6

Merged
merged 7 commits into from

2 participants

This page is out of date. Refresh to see the latest.
Showing with 56 additions and 23 deletions.
  1. +10 −7 tasks/coffee.js
  2. +46 −16 test/coffee_test.js
View
17 tasks/coffee.js
@@ -18,9 +18,10 @@ module.exports = function(grunt) {
grunt.registerMultiTask('coffee', 'Compile CoffeeScript files', function() {
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.helper('coffee', filepath, dest, options);
+ grunt.helper('coffee', filepath, dest, options, extension);
});
if (grunt.task.current.errorCount) {
@@ -32,12 +33,13 @@ module.exports = function(grunt) {
// HELPERS
// ==========================================================================
- grunt.registerHelper('coffee', function(src, destPath, options) {
+ grunt.registerHelper('coffee', function(src, destPath, options, extension) {
var coffee = require('coffee-script'),
js = '';
- var dest = path.join(destPath,
- path.basename(src, '.coffee') + '.js');
+ destPath = destPath ? destPath : path.dirname(src);
+ extension = extension ? extension : '.js';
+ var dest = path.join(destPath, path.basename(src, '.coffee') + extension);
options = options || {};
if( options.bare !== false ) {
@@ -46,10 +48,11 @@ module.exports = function(grunt) {
try {
js = coffee.compile(grunt.file.read(src), options);
+ grunt.file.write(dest, js);
} catch (e) {
grunt.log.error("Error in " + src + ":\n" + e);
+ return false;
}
- if (this.errorCount) { return false; }
- grunt.file.write(dest, js);
});
+
};
View
62 test/coffee_test.js
@@ -2,6 +2,8 @@ var grunt = require('grunt'),
fs = require('fs'),
path = require('path');
+fs.existsSync = fs.existsSync ? fs.existsSync : path.existsSync;
+
/*
======== A Handy Little Nodeunit Reference ========
https://github.com/caolan/nodeunit
@@ -22,34 +24,62 @@ var grunt = require('grunt'),
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'] = {
setUp: function(done) {
- // setup here
- fs.exists('tmp/coffee', function(exists) {
- if (exists) {
- fs.rmdir('tmp/coffee', done);
- } else {
- done();
+ done();
+ },
+
+ tearDown: function(done) {
+ if (fs.existsSync(destFolder)) {
+ 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) {
test.expect(2);
- var files = [
- 'test/fixtures/hello_world.coffee'
- ];
- var dest = 'tmp/js';
- // tests here
- grunt.helper('coffee', files, dest);
- test.equal(grunt.file.read(dest + '/hello_world.js'),
+
+ grunt.helper('coffee', [src], destFolder);
+ test.equal(grunt.file.read(destFolder + '/hello_world.js'),
'\nconsole.log("Hello CoffeeScript!");\n',
'it should compile the coffee');
- grunt.helper('coffee', files, dest, { bare:false });
- test.equal(grunt.file.read(dest + '/hello_world.js'),
+ grunt.helper('coffee', [src], destFolder, { bare:false });
+ test.equal(grunt.file.read(destFolder + '/hello_world.js'),
'(function() {\n\n console.log("Hello CoffeeScript!");\n\n}).call(this);\n',
'it should compile the coffee');
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();
}
};
Something went wrong with that request. Please try again.