Search code examples
javascriptajaxnode.jsgulp-watch

Run executable on gulp-watch


I want to run an executable or a "non-javascript script" on the event add using gulp-watch(so server/developer-side, not user-side). From searching around I'm guessing I need to use ajax as I want to avoid php, but this seems a bit roundabout. Is it possible to do this using node.js instead?


Solution

  • Supposing that you use it with gulp:

    var gulp = require('gulp');
    var watch = require('gulp-watch');
    var filter = require('gulp-filter');
    var exec = require('gulp-exec');
    
    function isAdded(file) {
      return file.event === 'add';
    }
    
    var filterAdded = filter(isAdded);
    
    gulp.task('default', function () {
      return gulp.src('**/*.js')
        .pipe(watch('**/*.js'))
        .pipe(filterAdded)
        .pipe(exec('ls -la'))
        .pipe(filterAdded.restore());
    });