Search code examples
gruntjsgrunt-contrib-watch

Grunt run shell after 'watch' eventListener changes filename


I'm trying to run shell command when file changes. Getting last changed file to use as argument for shell command. Here is the code:

grunt.initConfig({
    changedFile: 'test',

    watch: {
        all: {
            files: ['js/*.js'],
            tasks: ['shell']
        }
    },

    shell: {
        run: {
            // it outputs 'test', not changed file
            command: 'touch <%= changedFile %>'
        }
    }

});

grunt.event.on('watch', function(action, filepath) {
    grunt.config('changedFile', filepath);
});

'watch' eventListener actually works, but it does after shell command runs. How can I run task before event has been triggered?


Solution

  • options: { nospawn: true } for the watch task helped me on this.

    Thanks guys from this thread: How to modify grunt watch tasks based on the file changed?