Created
August 19, 2012 17:05
-
-
Save micahasmith/3396296 to your computer and use it in GitHub Desktop.
Grunt Livescript Mocha Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global module:false*/ | |
module.exports = function(grunt) { | |
var log = grunt.log; | |
//grunt.loadTasks('grunt/tasks'); | |
function handleResult(from, dest, err, stdout, code, done) { | |
if(err){ | |
grunt.helper('growl', 'ERROR', stdout); | |
log.writeln(from + ': failed to compile to ' + dest + '.'); | |
log.writeln(stdout); | |
done(false); | |
}else{ | |
log.writeln(from + ': compiled to ' + dest + '.'); | |
done(true); | |
} | |
}; | |
grunt.registerHelper('livescript_dir_to_dir', function(fromdir, dest, done) { | |
var args = { | |
cmd: 'livescript', | |
args: [ '--compile', '--output', dest, fromdir ] | |
}; | |
grunt.helper('exec', args, function(err, stdout, code){ | |
handleResult(fromdir, dest, err, stdout, code, done); | |
}); | |
}); | |
grunt.registerMultiTask('livescript', 'compile livescripts', function() { | |
var done = this.async(); | |
var files = this.data.files; | |
var dir = this.data.dir; | |
var dest = this.data.dest; | |
// ex: ./coffee -> ./js | |
if(dir) { | |
// if destination was not defined, compile to same dir | |
if(!dest) { | |
dest = dir; | |
} | |
grunt.helper('livescript_dir_to_dir', dir, dest, done); | |
return; | |
} | |
// ex: [ '1.coffee', '2.coffee' ] -> foo.js | |
if(files) { | |
grunt.helper('livescript_multi_to_one', files, dest, done); | |
return; | |
} | |
}); | |
var exec = require('child_process').exec; | |
// child_process.exec bridge | |
grunt.registerHelper('exec', function(opts, done) { | |
var command = opts.cmd + ' ' + opts.args.join(' '); | |
exec(command, opts.opts, function(code, stdout, stderr) { | |
if(!done){ | |
return; | |
} | |
if(code === 0) { | |
done(null, stdout, code); | |
} else { | |
done(code, stderr, code); | |
} | |
}); | |
}); | |
grunt.registerMultiTask('mocha','Runs the mocha test suite',function(){ | |
var dir = this.data, | |
command = 'mocha -R min '+dir, | |
e = require('child_process').exec, | |
done = this.async(); | |
e(command,function(err,stdout,stderr){ | |
log.writeln(stdout); | |
log.writeln(stderr); | |
done(true); | |
}) | |
}); | |
grunt.registerHelper('growl', function(title, msg) { | |
grunt.helper('exec', { | |
cmd: 'growlnotify', | |
args: [ | |
'-t', "'" + title + "'", | |
'-m', "'" + msg + "'" | |
] | |
}); | |
}); | |
// Project configuration. | |
grunt.initConfig({ | |
watch: { | |
files:"src/**/*.ls", | |
tasks:"default" | |
}, | |
livescript:{ | |
compile:{ | |
dir:"src/" | |
,dest:"compiled/" | |
} | |
}, | |
mocha:{ | |
dir:"compiled/tests/*" | |
} | |
}); | |
// Default task. | |
grunt.registerTask('default', 'livescript mocha'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've update it for 0.4.0, the helper are deprecated
https://gist.github.com/francisl/7148756