From b19fbdc07718dfad6d8cd501219be6bb2705960b Mon Sep 17 00:00:00 2001 From: Timmo Verlaan Date: Tue, 17 Nov 2020 01:42:58 +0100 Subject: [PATCH] Exit watch process on EOF / Ctrl-D (#358) --- index.js | 5 +++++ test/watch.js | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index edc7a3f..47a6e5a 100644 --- a/index.js +++ b/index.js @@ -51,6 +51,11 @@ let configFile if (argv.env) process.env.NODE_ENV = argv.env if (argv.config) argv.config = path.resolve(argv.config) +if (argv.watch) { + process.stdin.on('end', () => process.exit(0)) + process.stdin.resume() +} + Promise.resolve() .then(() => { if (argv.watch && !(argv.output || argv.replace || argv.dir)) { diff --git a/test/watch.js b/test/watch.js index b5bce40..4a51a11 100644 --- a/test/watch.js +++ b/test/watch.js @@ -3,11 +3,12 @@ const test = require('ava') const fs = require('fs-extra') const path = require('path') -const { exec } = require('child_process') +const { exec, spawn } = require('child_process') const chokidar = require('chokidar') const ENV = require('./helpers/env.js') const read = require('./helpers/read.js') +const tmp = require('./helpers/tmp.js') // XXX: All the tests in this file are skipped on the CI; too flacky there const testCb = process.env.CI ? test.cb.skip : test.cb @@ -285,3 +286,19 @@ testCb("--watch doesn't exit on CssSyntaxError", (t) => { // Timeout: setTimeout(() => t.end('test timeout'), 50000) }) + +testCb('--watch does exit on closing stdin (Ctrl-D/EOF)', (t) => { + t.plan(1) + + const cp = spawn( + `./bin/postcss test/fixtures/a.css -o ${tmp()} -w --no-map`, + { shell: true } + ) + + cp.on('error', t.end) + cp.on('exit', (code) => { + t.is(code, 0) + t.end() + }) + cp.stdin.end() +})