privatenumber / tsx

⚡️ TypeScript Execute | The easiest way to run TypeScript in Node.js
https://tsx.is
MIT License
9.77k stars 154 forks source link

Add --on-return-key flag to conditionally enable Return key listener #637

Closed iuroc closed 3 months ago

iuroc commented 3 months ago

Add --on-return-key flag to conditionally enable Return key listener

This change allows users to control whether the script should trigger a re-run on Return key press, providing more flexibility in how the watch command operates.


Below is an example demonstrating why the onReturnKey parameter is needed.

Create 1.js:

console.log(1)

require('process')

console.log(2)

Create 2.js:

const { spawn } = require('child_process')

spawn('node', ['1.js'], {
    stdio: ['inherit', 'inherit', 'inherit'],
    shell: true
})
process.stdin.on('data', () => {
    console.log('log in 2.js')
})

Create 3.js:

const { spawn } = require('child_process')

const child = spawn('node', ['2.js'], {
    stdio: ['pipe', 'pipe', 'pipe'],
    shell: true
})
child.stdout.on('data', chunk => {
    console.log(chunk.toString())
})

Here are the execution results:

Running 1.js:

> node 1.js
1
2
(program ends)

Running 2.js:

> node 2.js
1
2
(waiting for input) aaaaa
log in 2.js
bbbbb
log in 2.js
ccccc
log in 2.js
ddddd
log in 2.js
(waiting for input)

Running 3.js:

node 3.js
1
(there is an extra blank line here)
(blocked, unable to input, Enter has no effect)

Analysis