Last active
June 4, 2016 23:51
-
-
Save nikoloza/515f4d4cac656cbe2594 to your computer and use it in GitHub Desktop.
Run any command on recently changed file via Grunt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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