Skip to content

Commit

Permalink
Add --watch=always option to prevent exit when stdin closes (#9966)
Browse files Browse the repository at this point in the history
* Support `--flag=value` syntax for manually-parsed CLI args

* Don’t exit when stdin closes if using `--watch=always`

* Update changelog
  • Loading branch information
thecrypticace authored Nov 29, 2022
1 parent 705d213 commit 1d23dcb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 24 additions & 13 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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') {
Expand All @@ -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`.
Expand Down
8 changes: 6 additions & 2 deletions src/cli/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 1d23dcb

Please sign in to comment.