How to properly end a gulp task
<p>I'm trying to write a gulp task to execute a program and to wait for any key to be pressed to exit the program.</p>

<p>So far I have this:</p>

<pre><code>gulp.task('runDevelopment', function (done) {
  console.log('\n\n---\nPress any key to exit\n---\n');

  var api = spawn('node', ['node_modules/api-gateway-server/server.js', 'etc/swagger.yaml']);
  api.stdout.on('data', function (data) {
    console.log(data.toString());
  });
  api.stderr.on('data', function (data) {
    console.log(data.toString());
  });

  process.stdin.on('data', function () {
    api.kill('SIGKILL');
    done();
  });
});
</code></pre>

<p>This runs the program, and kills it as expected but gulp never quits. I see this in the console:</p>

<pre><code>20:04 $ gulp
[20:06:54] Using gulpfile ~/Development/swiki/gulpfile.js
[20:06:54] Starting 'documentCopy'...
[20:06:54] Starting 'documentZip'...
[20:06:54] Starting 'runDevelopment'...


---
Press any key to exit
---

[20:06:54] Finished 'documentCopy' after 52 ms
[20:06:54] Finished 'documentZip' after 41 ms
[20:06:54] Starting 'package'...
[20:06:54] Finished 'package' after 4.9 μs
API Gateway server listening on port 7111

[20:06:57] Finished 'runDevelopment' after 3.06 s
[20:06:57] Starting 'run'...
[20:06:57] Finished 'run' after 2.72 μs
[20:06:57] Starting 'default'...
[20:06:57] Finished 'default' after 1.16 μs
Terminated: 15
✘-TERM ~/Development/swiki [master|…6] 
20:07 $ 
</code></pre>

<p>gulp gives me the <code>Terminated: 15</code> only after I <code>killall gulp</code> in another terminal.</p>

<p>How can I get this to work properly?</p>