Skip to content

Commit 83771bd

Browse files
authored
Allow non-TTY stdin watch mode (postcss#448)
The presence of stdin doesn't necessarily mean there's an allocated tty. This breaks watch mode in non-TTY stdin contexts (e.g. docker, foreman, etc). A simple process.stdin.isTTY check would theoretically be enough but unfortunately, subprocesses don't have the same API, which are used extensively to test via calls to `spawn`. A simple solution is to inject an env var dependency where we tell the process that it's indeed a TTY-allocated process and so, watch mode with exit handling is good to go. Co-authored-by: Thiago Brandão <194487+thiagobrandam@users.noreply.github.com>
1 parent e939a68 commit 83771bd

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ let configFile
6161
if (argv.env) process.env.NODE_ENV = argv.env
6262
if (argv.config) argv.config = path.resolve(argv.config)
6363

64-
if (argv.watch) {
64+
let { isTTY } = process.stdin
65+
66+
if (process.env.FORCE_IS_TTY === 'true') {
67+
isTTY = true
68+
}
69+
70+
if (argv.watch && isTTY) {
6571
process.stdin.on('end', () => process.exit(0))
6672
process.stdin.resume()
6773
}

test/watch.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,18 @@ testCb('--watch does exit on closing stdin (Ctrl-D/EOF)', (t) => {
205205

206206
const cp = spawn(`./index.js test/fixtures/a.css -o ${tmp()} -w --no-map`, {
207207
shell: true,
208+
env: {
209+
...process.env,
210+
FORCE_IS_TTY: true,
211+
},
208212
})
209213

210214
cp.on('error', t.end)
211215
cp.on('exit', (code) => {
212216
t.is(code, 0)
213217
t.end()
214218
})
219+
215220
cp.stdin.end()
216221
})
217222

0 commit comments

Comments
 (0)