Skip to content

Commit

Permalink
Exit watch process on EOF / Ctrl-D (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
tverlaan authored Nov 17, 2020
1 parent e279de8 commit b19fbdc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
19 changes: 18 additions & 1 deletion test/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
})

0 comments on commit b19fbdc

Please sign in to comment.