gulp-server-fork

Gulp plugin for starting a fork process and kill it before via pipe stream

Usage no npm install needed!

<script type="module">
  import gulpServerFork from 'https://cdn.skypack.dev/gulp-server-fork';
</script>

README

gulp-server-fork

Noop pipe plugin for spawning and killing a forked node js application

Examples

Simple

gulp.task('test', function() {
    return gulp.src(['lib/**'])
        .pipe(gulpServerFork('lib/server.js')
        // Do something with the srcs, those are untouched...
        .pipe(gulpServerFork.kill())
})

Full

gulp.task('test', function() {
    return gulp.src(['lib/**'])
        .pipe(gulpServerFork({
            id: 'Server 1',
            timeout: 20 * 1000,
            logfile: 'output.log',
            env: {
                PORT: 8080
            },
        })
        // Do something with the srcs, those are untouched...
        .pipe(gulpServerFork.kill('Server 1'))
})

ready event

For notifying the plugin when to liberate the pipe stream your app should emit a ready event through process.send.

For example, using it with an express instance, the code would look as follows.

app.listen(app.get('port'), function(err) {
    if (err)
        throw err

    if (process.send) // if available, means we are being called as a child process, so notify
        process.send('ready');

    console.log('Server listening on port ' + app.get('port'));
});

Options

  • id: The internal id for the process. This is useful if you are going to fork several processes and want to kill them on different moments. Defaults to Date.now()
  • timeout: Time in milliseconds. Once this time has passed, if the spawned process hasn't emitted a ready event, the pipe stream will continue. Defaults to 20000.
  • logfile: File where to dump the output of the spawned process. Defaults to null
  • env: Object. Environment passed to the spawned process.