Skip to content

Instantly share code, notes, and snippets.

@lifedraft
Last active December 29, 2015 17:29
Show Gist options
  • Save lifedraft/7704321 to your computer and use it in GitHub Desktop.
Save lifedraft/7704321 to your computer and use it in GitHub Desktop.
Compile all coffee and compass files recursively within an project with grunt.
module.exports = function( grunt ) {
// Compile all coffee and compass files recursively within an project with grunt.
// Usage:
// > cd <my-project>
// > npm install grunt-contrib-watch
// > npm install grunt-contrib-coffee
// > npm install grunt-contrib-compass
// > grunt assets_config
// this generates an assets_config.js file, so when you create new coffee or sass file you have to rerun this task.
// > grunt watch
if (grunt.file.exists('assets_config.js')) {
var config = grunt.file.readJSON('assets_config.js');
grunt.initConfig(config);
};
grunt.registerTask('assets_config', 'search all modules', function() {
var compass = grunt.config.get('compass') || {};
var coffee = grunt.config.get('coffee') || {};
var watch = grunt.config.get('watch') || {};
grunt.file.recurse('.', function(abspath, rootdir, subdir, filename) {
if (!subdir) {
return false;
};
if (subdir.indexOf('/sass') !== -1) {
if (!(subdir in compass)) {
var stylesheets_dir = subdir.split("sass").join('') + 'stylesheets';
watch[subdir] = {
files: [subdir + '/*.{scss,sass}'],
tasks: ['compass:' + subdir]
};
compass[subdir] = {
options: {
sassDir: subdir,
cssDir: stylesheets_dir
}
};
};
};
if (filename.indexOf('.coffee') !== -1) {
coffee[subdir] = coffee[subdir] || {
files: {},
options: {
sourceMap: true
},
};
var javascripts_dir = subdir.split("coffeescripts").join('') + 'javascripts';
if (!(subdir in watch)) {
watch[subdir] = {
// files: '<config.coffee.' + subdir +'.files>', // [subdir + '/*.coffee'],
files: [subdir + '/*.coffee'],
tasks: ['coffee:' + subdir]
};
}
coffee[subdir].files[javascripts_dir + '/' + filename + '.js'] = abspath;
}
});
var asset_config = {
compass: compass,
coffee: coffee,
watch: watch
};
grunt.file.delete('assets_config.js');
grunt.file.write('assets_config.js', JSON.stringify(asset_config));
});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-coffee');
grunt.loadNpmTasks('grunt-contrib-compass');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment