Skip to content

Instantly share code, notes, and snippets.

@henrahmagix
Last active December 18, 2015 16:09
Show Gist options
  • Save henrahmagix/5809771 to your computer and use it in GitHub Desktop.
Save henrahmagix/5809771 to your computer and use it in GitHub Desktop.
Delete compiled JS files when the source Coffee file gets deleted on a grunt watch.
module.exports = (grunt) ->
coffeeDir = 'coffee/'
jsDir = 'project/'
grunt.initConfig
coffee:
project:
expand: true
cwd: coffeeDir
src: ['*.coffee', '**/*.coffee']
dest: jsDir
ext: '.js'
options:
bare: true
watch:
coffee:
files: ['coffee/*.coffee', 'coffee/**/*.coffee']
tasks: ['coffee']
grunt.event.on 'watch', (action, filepath) ->
# Delete compiled .js file of deleted source .coffee file.
if action == 'deleted' and grunt.file.exists jsFile
filepath = filepath.replace coffeeDir, ''
jsFile = jsDir + filepath.replace /\.coffee$/, '.js'
grunt.file.delete jsFile
grunt.log.ok 'File ' + jsFile + ' deleted'
grunt.loadNpmTasks task for task in ['grunt-contrib-coffee', 'grunt-contrib-watch']
grunt.registerTask 'default', ['watch']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment