forked from avalade/grunt-coffee
/
coffee.js
55 lines (44 loc) · 1.59 KB
/
coffee.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* 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;
grunt.file.expandFiles(this.file.src).forEach(function(filepath) {
grunt.helper('coffee', filepath, dest, options);
});
if (grunt.task.current.errorCount) {
return false;
}
});
// ==========================================================================
// HELPERS
// ==========================================================================
grunt.registerHelper('coffee', function(src, destPath, options) {
var coffee = require('coffee-script'),
js = '';
destPath = destPath ? destPath : path.dirname(src);
var dest = path.join(destPath, path.basename(src, '.coffee') + '.js');
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("Unable to compile your coffee", e);
}
});
};