Skip to content

Commit

Permalink
Allow non-TTY stdin watch mode (#448)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
0xradical and 0xradical committed Nov 29, 2022
1 parent e939a68 commit 83771bd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion index.js
Expand Up @@ -61,7 +61,13 @@ let configFile
if (argv.env) process.env.NODE_ENV = argv.env
if (argv.config) argv.config = path.resolve(argv.config)

if (argv.watch) {
let { isTTY } = process.stdin

if (process.env.FORCE_IS_TTY === 'true') {
isTTY = true
}

if (argv.watch && isTTY) {
process.stdin.on('end', () => process.exit(0))
process.stdin.resume()
}
Expand Down
5 changes: 5 additions & 0 deletions test/watch.js
Expand Up @@ -205,13 +205,18 @@ testCb('--watch does exit on closing stdin (Ctrl-D/EOF)', (t) => {

const cp = spawn(`./index.js test/fixtures/a.css -o ${tmp()} -w --no-map`, {
shell: true,
env: {
...process.env,
FORCE_IS_TTY: true,
},
})

cp.on('error', t.end)
cp.on('exit', (code) => {
t.is(code, 0)
t.end()
})

cp.stdin.end()
})

Expand Down

0 comments on commit 83771bd

Please sign in to comment.