From e95f5fc7365eba2f85d6206cf50590d0a6ba0cbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thiago=20Brand=C3=A3o?= <194487+thiagobrandam@users.noreply.github.com> Date: Mon, 28 Nov 2022 13:53:03 -0300 Subject: [PATCH] Allow non-TTY stdin watch mode 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. --- index.js | 8 +++++++- test/watch.js | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 615638c..da052ce 100755 --- a/index.js +++ b/index.js @@ -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() } diff --git a/test/watch.js b/test/watch.js index 4cd5391..625df1b 100644 --- a/test/watch.js +++ b/test/watch.js @@ -205,6 +205,10 @@ 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) @@ -212,6 +216,7 @@ testCb('--watch does exit on closing stdin (Ctrl-D/EOF)', (t) => { t.is(code, 0) t.end() }) + cp.stdin.end() })