Skip to content

Commit b19fbdc

Browse files
authored
Exit watch process on EOF / Ctrl-D (postcss#358)
1 parent e279de8 commit b19fbdc

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ let configFile
5151
if (argv.env) process.env.NODE_ENV = argv.env
5252
if (argv.config) argv.config = path.resolve(argv.config)
5353

54+
if (argv.watch) {
55+
process.stdin.on('end', () => process.exit(0))
56+
process.stdin.resume()
57+
}
58+
5459
Promise.resolve()
5560
.then(() => {
5661
if (argv.watch && !(argv.output || argv.replace || argv.dir)) {

test/watch.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ const test = require('ava')
33

44
const fs = require('fs-extra')
55
const path = require('path')
6-
const { exec } = require('child_process')
6+
const { exec, spawn } = require('child_process')
77
const chokidar = require('chokidar')
88

99
const ENV = require('./helpers/env.js')
1010
const read = require('./helpers/read.js')
11+
const tmp = require('./helpers/tmp.js')
1112

1213
// XXX: All the tests in this file are skipped on the CI; too flacky there
1314
const testCb = process.env.CI ? test.cb.skip : test.cb
@@ -285,3 +286,19 @@ testCb("--watch doesn't exit on CssSyntaxError", (t) => {
285286
// Timeout:
286287
setTimeout(() => t.end('test timeout'), 50000)
287288
})
289+
290+
testCb('--watch does exit on closing stdin (Ctrl-D/EOF)', (t) => {
291+
t.plan(1)
292+
293+
const cp = spawn(
294+
`./bin/postcss test/fixtures/a.css -o ${tmp()} -w --no-map`,
295+
{ shell: true }
296+
)
297+
298+
cp.on('error', t.end)
299+
cp.on('exit', (code) => {
300+
t.is(code, 0)
301+
t.end()
302+
})
303+
cp.stdin.end()
304+
})

0 commit comments

Comments
 (0)