Skip to content

Instantly share code, notes, and snippets.

@nikoloza
Last active June 4, 2016 23:51
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 nikoloza/515f4d4cac656cbe2594 to your computer and use it in GitHub Desktop.
Save nikoloza/515f4d4cac656cbe2594 to your computer and use it in GitHub Desktop.
Run any command on recently changed file via Grunt
'use strict'
module.exports = function (grunt) {
grunt.initConfig({
options: {
command: 'touch',
args: '',
filepath: '',
file: ''
},
watch: {
files: ['js/*.js'],
tasks: ['changelog', 'shell'],
options: {
spawn: false
}
},
shell: {
run: {
command: '<%= options.command %> <%= options.file %> <%= options.args %>'
}
},
file_append: {
append: {
files: {
'commands.log': {
prepend: '## Command run: ' +
'"<%= options.command %> <%= options.file %> <%= options.args %>" \n## ' +
new Date() + '\n\n'
}
}
}
}
})
// all npm packages we need:
// npm i grunt-contrib-watch grunt-shell grunt-file-append
require('load-grunt-tasks')(grunt)
grunt.registerTask('changelog', function () {
if (!grunt.file.exists('commands.log')) {
grunt.file.write('commands.log', '')
}
grunt.task.run('file_append')
grunt.task.run('shell')
})
grunt.event.on('watch', function (action, filepath) {
grunt.config(['options', 'filepath'], filepath)
grunt.config(['options', 'file'], filepath.replace(/^.*[\\\/]/, ''))
grunt.log.ok('"' + grunt.template.process('<%= options.command %>') +
' ' + filepath + ' ' + grunt.template.process('<%= options.args %>') +
'" has been runned')
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment