Unverified Commit 45d8a7a5 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

[Fast Refresh] Fix terminal editors on macOS (#12430)

parent 5e80999c
...@@ -30,6 +30,7 @@ import shellQuote from 'shell-quote' ...@@ -30,6 +30,7 @@ import shellQuote from 'shell-quote'
function isTerminalEditor(editor) { function isTerminalEditor(editor) {
switch (editor) { switch (editor) {
case 'vi':
case 'vim': case 'vim':
case 'emacs': case 'emacs':
case 'nano': { case 'nano': {
...@@ -311,7 +312,6 @@ function printInstructions(fileName, errorMessage) { ...@@ -311,7 +312,6 @@ function printInstructions(fileName, errorMessage) {
console.log() console.log()
} }
let _childProcess = null
function launchEditor(fileName: string, lineNumber: number, colNumber: number) { function launchEditor(fileName: string, lineNumber: number, colNumber: number) {
if (!fs.existsSync(fileName)) { if (!fs.existsSync(fileName)) {
return return
...@@ -393,35 +393,43 @@ function launchEditor(fileName: string, lineNumber: number, colNumber: number) { ...@@ -393,35 +393,43 @@ function launchEditor(fileName: string, lineNumber: number, colNumber: number) {
args.push(fileName) args.push(fileName)
} }
if (_childProcess && isTerminalEditor(editor)) { let p: child_process.ChildProcess
// There's an existing editor process already and it's attached
// to the terminal, so go kill it. Otherwise two separate editor
// instances attach to the stdin/stdout which gets confusing.
_childProcess.kill('SIGKILL')
}
if (process.platform === 'win32') { if (process.platform === 'win32') {
// On Windows, launch the editor in a shell because spawn can only // On Windows, launch the editor in a shell because spawn can only
// launch .exe files. // launch .exe files.
_childProcess = child_process.spawn( p = child_process.spawn('cmd.exe', ['/C', editor].concat(args), {
'cmd.exe', stdio: 'inherit',
['/C', editor].concat(args), })
{ stdio: 'inherit' } } else if (isTerminalEditor(editor)) {
) if (process.platform === 'darwin') {
p = child_process.spawn(
'osascript',
[
'-e',
`tell application "Terminal" to do script "${shellQuote.quote([
editor,
...args,
])}"`,
],
{ stdio: 'ignore' }
)
} else {
printInstructions(fileName, 'Terminal editors can only be used on macOS.')
}
} else { } else {
_childProcess = child_process.spawn(editor, args, { stdio: 'inherit' }) p = child_process.spawn(editor, args, { stdio: 'inherit' })
} }
_childProcess.on('exit', function(errorCode) {
_childProcess = null
if (errorCode) {
printInstructions(fileName, '(code ' + errorCode + ')')
}
})
_childProcess.on('error', function(error) { if (p) {
printInstructions(fileName, error.message) p.on('exit', function(errorCode) {
}) if (errorCode) {
printInstructions(fileName, '(code ' + errorCode + ')')
}
})
p.on('error', function(error) {
printInstructions(fileName, error.message)
})
}
} }
export { launchEditor } export { launchEditor }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment