Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/node_modules/
tmp
#*
*~
61 changes: 61 additions & 0 deletions Gruntfile.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module.exports = (grunt) ->

grunt.initConfig
pkg: grunt.file.readJSON('package.json')

jshint:
files: [ "src/**/*.js", "test/**/*.js" ]
options:
curly: true
eqeqeq: true
immed: true
latedef: true
newcap: true
noarg: true
sub: true
undef: true
boss: true
eqnull: true
node: true
es5: true

globals: {}

clean:
src: ["tmp/"]

coffee:
coffee:
files:
"tasks/coffee.js": "src/coffee.coffee"
options:
bare: false

test1:
files:
"tmp/hello_world.js": ["test/fixtures/hello_world.coffee"]
options:
bare: false

test2:
files:
"tmp/hello_world_bare.js": "test/fixtures/hello_world.coffee"
options:
bare: true

nodeunit:
files: [ "test/**/*.js" ]

watch:
files: "<config:jshint.files>"
tasks: "default"


grunt.loadTasks "tasks"

grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-nodeunit');
grunt.loadNpmTasks('grunt-contrib-clean');

grunt.registerTask "default", ["coffee:coffee", "jshint", "coffee:test1", "coffee:test2", "nodeunit"]
40 changes: 0 additions & 40 deletions grunt.js

This file was deleted.

14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
{
"name": "Derek Lindahl",
"url": "https://github.com/dlindahl"
},
{
"name": "Elf M. Sternberg",
"url": "https://github.com/elfsternberg"
}
],
"repository": {
Expand All @@ -34,12 +38,16 @@
"test": "grunt test"
},
"dependencies": {
"grunt": "~0.3.8",
"grunt": "=0.4.0a",
"coffee-script": "~1.3.1"
},
"devDependencies": {
"grunt": "~0.3.8",
"coffee-script": "~1.3.1"
"grunt": "~0.4.x",
"coffee-script": "~1.3.1",
"grunt-contrib-watch": "~0.1.4",
"grunt-contrib-nodeunit": "~0.1.1",
"grunt-contrib-jshint": "~0.1.0",
"grunt-contrib-clean": "~0.4.0a"
},
"keywords": [
"gruntplugin"
Expand Down
41 changes: 41 additions & 0 deletions src/coffee.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# grunt-coffee
# https://github.com/avalade/grunt-coffee
#
# Copyright (c) 2012 Aaron D. Valade
# Licensed under the MIT license.
#

module.exports = (grunt) ->

helper = (src, destPath, options, extension) ->

coffee = require("coffee-script")
js = ""

extension = (if extension then extension else ".js")

dest = destPath
destPath = (if destPath then destPath else path.dirname(src))
if destPath.indexOf(extension, destPath.length - extension.length) == -1
dest = path.join(destPath, path.basename(src, ".coffee") + extension)

options = options or {}
options.bare = true if options.bare isnt false

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

path = require("path")

grunt.registerMultiTask "coffee", "Compile CoffeeScript files", ->
[dest, options, extension] = [@files.dest, @data.options, @data.extension]
@files.forEach (fileobj) ->
grunt.file.expand(fileobj.src, {filter: "isFile"}).forEach (filepath) ->
helper filepath, fileobj.dest, grunt.util._.clone(options), extension

(grunt.task.current.errorCount == 0)
97 changes: 40 additions & 57 deletions tasks/coffee.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,42 @@
/*
* grunt-coffee
* https://github.com/avalade/grunt-coffee
*
* Copyright (c) 2012 Aaron D. Valade
* Licensed under the MIT license.
*/

module.exports = function(grunt) {
var path = require('path');

// Please see the grunt documentation for more information regarding task and
// helper creation: https://github.com/cowboy/grunt/blob/master/docs/toc.md

// ==========================================================================
// TASKS
// ==========================================================================

grunt.registerMultiTask('coffee', 'Compile CoffeeScript files', function() {
var dest = this.file.dest,
options = this.data.options,
extension = this.data.extension;

grunt.file.expandFiles(this.file.src).forEach(function(filepath) {
grunt.helper('coffee', filepath, dest, grunt.utils._.clone(options), extension);
(function() {

module.exports = function(grunt) {
var helper, path;
helper = function(src, destPath, options, extension) {
var coffee, dest, js;
coffee = require("coffee-script");
js = "";
extension = (extension ? extension : ".js");
dest = destPath;
destPath = (destPath ? destPath : path.dirname(src));
if (destPath.indexOf(extension, destPath.length - extension.length) === -1) {
dest = path.join(destPath, path.basename(src, ".coffee") + extension);
}
options = options || {};
if (options.bare !== false) {
options.bare = true;
}
try {
js = coffee.compile(grunt.file.read(src), options);
return grunt.file.write(dest, js);
} catch (e) {
grunt.log.error("Error in " + src + ":\n" + e);
return false;
}
};
path = require("path");
return grunt.registerMultiTask("coffee", "Compile CoffeeScript files", function() {
var dest, extension, options, _ref;
_ref = [this.files.dest, this.data.options, this.data.extension], dest = _ref[0], options = _ref[1], extension = _ref[2];
this.files.forEach(function(fileobj) {
return grunt.file.expand(fileobj.src, {
filter: "isFile"
}).forEach(function(filepath) {
return helper(filepath, fileobj.dest, grunt.util._.clone(options), extension);
});
});
return grunt.task.current.errorCount === 0;
});
};

if (grunt.task.current.errorCount) {
return false;
}
});

// ==========================================================================
// HELPERS
// ==========================================================================

grunt.registerHelper('coffee', function(src, destPath, options, extension) {
var coffee = require('coffee-script'),
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 ) {
options.bare = true;
}

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;
}
});

};
}).call(this);
71 changes: 8 additions & 63 deletions test/coffee_test.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,30 @@
"use strict";

var grunt = require('grunt'),
fs = require('fs'),
path = require('path');
path = require('path'),
src = 'test/fixtures/hello_world.coffee',
destFolder = 'tmp/';

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

/*
======== A Handy Little Nodeunit Reference ========
https://github.com/caolan/nodeunit

Test methods:
test.expect(numAssertions)
test.done()
Test assertions:
test.ok(value, [message])
test.equal(actual, expected, [message])
test.notEqual(actual, expected, [message])
test.deepEqual(actual, expected, [message])
test.notDeepEqual(actual, expected, [message])
test.strictEqual(actual, expected, [message])
test.notStrictEqual(actual, expected, [message])
test.throws(block, [error], [message])
test.doesNotThrow(block, [error], [message])
test.ifError(value)
*/
exports.coffee = {

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) {
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) {
'task': function(test) {
test.expect(2);

grunt.helper('coffee', [src], destFolder);
test.equal(grunt.file.read(destFolder + '/hello_world.js'),
test.equal(grunt.file.read(destFolder + '/hello_world_bare.js'),
'\nconsole.log("Hello CoffeeScript!");\n',
'it should compile the coffee');

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();
}
};