From 1d23dcbe9279f5524e005a71e9ba7bcb80bdcde0 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Tue, 29 Nov 2022 14:57:07 -0500 Subject: [PATCH] Add `--watch=always` option to prevent exit when stdin closes (#9966) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Support `--flag=value` syntax for manually-parsed CLI args * Don’t exit when stdin closes if using `--watch=always` * Update changelog --- CHANGELOG.md | 1 + src/cli.js | 37 ++++++++++++++++++++++++------------- src/cli/build/index.js | 8 ++++++-- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71e59f1c2719..afeecbb539ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add `line-height` modifier support to `font-size` utilities ([#9875](https://github.com/tailwindlabs/tailwindcss/pull/9875)) - Support using variables as arbitrary values without `var(...)` ([#9880](https://github.com/tailwindlabs/tailwindcss/pull/9880)) +- Add `--watch=always` option to prevent exit when stdin closes ([#9966](https://github.com/tailwindlabs/tailwindcss/pull/9966)) ### Fixed diff --git a/src/cli.js b/src/cli.js index b2199e00c707..fbc2d20853bb 100644 --- a/src/cli.js +++ b/src/cli.js @@ -62,7 +62,10 @@ let commands = { args: { '--input': { type: String, description: 'Input file' }, '--output': { type: String, description: 'Output file' }, - '--watch': { type: Boolean, description: 'Watch for changes and rebuild as needed' }, + '--watch': { + type: oneOf(String, Boolean), + description: 'Watch for changes and rebuild as needed', + }, '--poll': { type: Boolean, description: 'Use polling instead of filesystem events when watching', @@ -159,8 +162,8 @@ let args = (() => { let flag = result['_'][i] if (!flag.startsWith('-')) continue - let flagName = flag - let handler = flags[flag] + let [flagName, flagValue] = flag.split('=') + let handler = flags[flagName] // Resolve flagName & handler while (typeof handler === 'string') { @@ -173,19 +176,27 @@ let args = (() => { let args = [] let offset = i + 1 - // Parse args for current flag - while (result['_'][offset] && !result['_'][offset].startsWith('-')) { - args.push(result['_'][offset++]) - } + // --flag value syntax was used so we need to pull `value` from `args` + if (flagValue === undefined) { + // Parse args for current flag + while (result['_'][offset] && !result['_'][offset].startsWith('-')) { + args.push(result['_'][offset++]) + } + + // Cleanup manually parsed flags + args + result['_'].splice(i, 1 + args.length) - // Cleanup manually parsed flags + args - result['_'].splice(i, 1 + args.length) + // No args were provided, use default value defined in handler + // One arg was provided, use that directly + // Multiple args were provided so pass them all in an array + flagValue = args.length === 0 ? undefined : args.length === 1 ? args[0] : args + } else { + // Remove the whole flag from the args array + result['_'].splice(i, 1) + } // Set the resolved value in the `result` object - result[flagName] = handler.type( - args.length === 0 ? undefined : args.length === 1 ? args[0] : args, - flagName - ) + result[flagName] = handler.type(flagValue, flagName) } // Ensure that the `command` is always the first argument in the `args`. diff --git a/src/cli/build/index.js b/src/cli/build/index.js index 763b3d56b75a..c75b719da8eb 100644 --- a/src/cli/build/index.js +++ b/src/cli/build/index.js @@ -34,8 +34,12 @@ export async function build(args, configs) { let processor = await createProcessor(args, configPath) if (shouldWatch) { - /* Abort the watcher if stdin is closed to avoid zombie processes */ - process.stdin.on('end', () => process.exit(0)) + // Abort the watcher if stdin is closed to avoid zombie processes + // You can disable this behavior with --watch=always + if (args['--watch'] !== 'always') { + process.stdin.on('end', () => process.exit(0)) + } + process.stdin.resume() await processor.watch()