Skip to content

Commit 1d23dcb

Browse files
Add --watch=always option to prevent exit when stdin closes (#9966)
* Support `--flag=value` syntax for manually-parsed CLI args * Don’t exit when stdin closes if using `--watch=always` * Update changelog
1 parent 705d213 commit 1d23dcb

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Add `line-height` modifier support to `font-size` utilities ([#9875](https://github.com/tailwindlabs/tailwindcss/pull/9875))
1313
- Support using variables as arbitrary values without `var(...)` ([#9880](https://github.com/tailwindlabs/tailwindcss/pull/9880))
14+
- Add `--watch=always` option to prevent exit when stdin closes ([#9966](https://github.com/tailwindlabs/tailwindcss/pull/9966))
1415

1516
### Fixed
1617

src/cli.js

+24-13
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ let commands = {
6262
args: {
6363
'--input': { type: String, description: 'Input file' },
6464
'--output': { type: String, description: 'Output file' },
65-
'--watch': { type: Boolean, description: 'Watch for changes and rebuild as needed' },
65+
'--watch': {
66+
type: oneOf(String, Boolean),
67+
description: 'Watch for changes and rebuild as needed',
68+
},
6669
'--poll': {
6770
type: Boolean,
6871
description: 'Use polling instead of filesystem events when watching',
@@ -159,8 +162,8 @@ let args = (() => {
159162
let flag = result['_'][i]
160163
if (!flag.startsWith('-')) continue
161164

162-
let flagName = flag
163-
let handler = flags[flag]
165+
let [flagName, flagValue] = flag.split('=')
166+
let handler = flags[flagName]
164167

165168
// Resolve flagName & handler
166169
while (typeof handler === 'string') {
@@ -173,19 +176,27 @@ let args = (() => {
173176
let args = []
174177
let offset = i + 1
175178

176-
// Parse args for current flag
177-
while (result['_'][offset] && !result['_'][offset].startsWith('-')) {
178-
args.push(result['_'][offset++])
179-
}
179+
// --flag value syntax was used so we need to pull `value` from `args`
180+
if (flagValue === undefined) {
181+
// Parse args for current flag
182+
while (result['_'][offset] && !result['_'][offset].startsWith('-')) {
183+
args.push(result['_'][offset++])
184+
}
185+
186+
// Cleanup manually parsed flags + args
187+
result['_'].splice(i, 1 + args.length)
180188

181-
// Cleanup manually parsed flags + args
182-
result['_'].splice(i, 1 + args.length)
189+
// No args were provided, use default value defined in handler
190+
// One arg was provided, use that directly
191+
// Multiple args were provided so pass them all in an array
192+
flagValue = args.length === 0 ? undefined : args.length === 1 ? args[0] : args
193+
} else {
194+
// Remove the whole flag from the args array
195+
result['_'].splice(i, 1)
196+
}
183197

184198
// Set the resolved value in the `result` object
185-
result[flagName] = handler.type(
186-
args.length === 0 ? undefined : args.length === 1 ? args[0] : args,
187-
flagName
188-
)
199+
result[flagName] = handler.type(flagValue, flagName)
189200
}
190201

191202
// Ensure that the `command` is always the first argument in the `args`.

src/cli/build/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ export async function build(args, configs) {
3434
let processor = await createProcessor(args, configPath)
3535

3636
if (shouldWatch) {
37-
/* Abort the watcher if stdin is closed to avoid zombie processes */
38-
process.stdin.on('end', () => process.exit(0))
37+
// Abort the watcher if stdin is closed to avoid zombie processes
38+
// You can disable this behavior with --watch=always
39+
if (args['--watch'] !== 'always') {
40+
process.stdin.on('end', () => process.exit(0))
41+
}
42+
3943
process.stdin.resume()
4044

4145
await processor.watch()

0 commit comments

Comments
 (0)