Skip to content

Instantly share code, notes, and snippets.

@mohayonao
Created July 15, 2014 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohayonao/d8083b7b8f541bf05c9e to your computer and use it in GitHub Desktop.
Save mohayonao/d8083b7b8f541bf05c9e to your computer and use it in GitHub Desktop.
run grunt tasks only on changed
module.exports = (grunt)->
'use strict'
_ = require 'underscore'
toTaskArgs = (args)->
switch
when _.isUndefined args then ''
when _.isArguments args then _.map(args, toTaskArgs).join ':'
else args
grunt.initConfig
pkg: grunt.file.readJSON 'package.json'
grunt.registerTask(
'default'
'Run predefined tasks whenever watched files change'
[ '-watch' ]
)
grunt.registerTask(
'coffee'
'Compile CoffeeScript files into JavaScript'
-> grunt.task.run "-coffee:#{toTaskArgs(arguments)}"
)
if _.some([ '-h', '--help' ], (opt)->_.contains process.argv, opt)
return
loadNpmTasksIfNeeded = (name)->
if not loadNpmTasksIfNeeded.hasOwnProperty name
loadNpmTasksIfNeeded[name] = true
grunt.loadNpmTasks name
filterFiles = (list, filter)->
if filter
_.filter list, (file)-> file.indexOf(filter) isnt -1
else list
grunt.registerTask '-watch', ->
loadNpmTasksIfNeeded 'grunt-este-watch'
grunt.config.data.esteWatch =
options:
dirs: 'src/**/'
coffee: (file)-> "-coffee:#{file}"
grunt.task.run 'esteWatch'
grunt.registerTask '-coffee', (filter)->
src = grunt.file.expand 'src/*.coffee'
src = filterFiles src, filter
return if src.length is 0
loadNpmTasksIfNeeded 'grunt-contrib-coffee'
grunt.config.data.coffee =
files:
expand: yes
cwd : 'src/'
src : _.map(src, (path)-> _.rest(path.split('/')).join '/')
dest: 'lib/'
ext : '.js'
grunt.task.run 'coffee'
{
"name": "run-grunt-tasks-only-on-changed",
"version": "2014.07.15",
"dependencies": {
"grunt": "^0.4.5",
"grunt-contrib-coffee": "^0.10.1",
"grunt-este-watch": "^0.1.16",
"underscore": "^1.6.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment