diff --git a/.gitignore b/.gitignore index 083de6b..515ce9c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /node_modules/ tmp +#* +*~ diff --git a/Gruntfile.coffee b/Gruntfile.coffee new file mode 100644 index 0000000..3069765 --- /dev/null +++ b/Gruntfile.coffee @@ -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: "" + 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"] diff --git a/grunt.js b/grunt.js deleted file mode 100644 index 1bc5c42..0000000 --- a/grunt.js +++ /dev/null @@ -1,40 +0,0 @@ -module.exports = function(grunt) { - - // Project configuration. - grunt.initConfig({ - test: { - files: ['test/**/*.js'] - }, - lint: { - files: ['grunt.js', 'tasks/**/*.js', 'test/**/*.js'] - }, - watch: { - files: '', - tasks: 'default' - }, - jshint: { - 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: {} - } - }); - - // Load local tasks. - grunt.loadTasks('tasks'); - - // Default task. - grunt.registerTask('default', 'lint test'); - -}; diff --git a/package.json b/package.json index 58934d2..b7ef8c3 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,10 @@ { "name": "Derek Lindahl", "url": "https://github.com/dlindahl" + }, + { + "name": "Elf M. Sternberg", + "url": "https://github.com/elfsternberg" } ], "repository": { @@ -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" diff --git a/src/coffee.coffee b/src/coffee.coffee new file mode 100644 index 0000000..554d02b --- /dev/null +++ b/src/coffee.coffee @@ -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) diff --git a/tasks/coffee.js b/tasks/coffee.js index e9ebe9b..8402ff5 100644 --- a/tasks/coffee.js +++ b/tasks/coffee.js @@ -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); diff --git a/test/coffee_test.js b/test/coffee_test.js index 060c999..c4c2df2 100644 --- a/test/coffee_test.js +++ b/test/coffee_test.js @@ -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(); } };