Skip to content

Commit

Permalink
Merge pull request #5 from petebacondarwin/master
Browse files Browse the repository at this point in the history
Relative compilation
  • Loading branch information
avalade committed Jul 21, 2012
2 parents 123414e + 9b89279 commit b90ac32
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
9 changes: 5 additions & 4 deletions tasks/coffee.js
Expand Up @@ -36,8 +36,8 @@ module.exports = function(grunt) {
var coffee = require('coffee-script'),
js = '';

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

options = options || {};
if( options.bare !== false ) {
Expand All @@ -46,10 +46,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);
});

};
52 changes: 36 additions & 16 deletions test/coffee_test.js
Expand Up @@ -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
Expand All @@ -22,34 +24,52 @@ var grunt = require('grunt'),
test.ifError(value)
*/

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

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(relativeDest(src))) {
fs.unlinkSync(relativeDest(src));
}
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) {
grunt.helper('coffee', [src], null);
test.equal(grunt.file.read(relativeDest(src)),
'\nconsole.log("Hello CoffeeScript!");\n',
'it should compile the coffee');
test.done();
}
};

0 comments on commit b90ac32

Please sign in to comment.