From 30345f166ceaa3e44d6e1ae867f82cb5b1503195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BA=84=E4=BF=8A=E9=9C=96?= Date: Tue, 16 May 2023 18:16:55 +0800 Subject: [PATCH 1/2] fix: launch-editor in vscode --- packages/launch-editor/index.js | 35 ++++++++++++++++++++--------- packages/launch-editor/package.json | 2 +- packages/launch-editor/test.js | 3 +++ 3 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 packages/launch-editor/test.js diff --git a/packages/launch-editor/index.js b/packages/launch-editor/index.js index 20bd59e..f2420de 100644 --- a/packages/launch-editor/index.js +++ b/packages/launch-editor/index.js @@ -98,7 +98,12 @@ function launchEditor (file, specifiedEditor, onErrorCallback) { } if (lineNumber) { - const extraArgs = getArgumentsForPosition(editor, fileName, lineNumber, columnNumber) + const extraArgs = getArgumentsForPosition( + editor.includes('Visual Studio Code.app') ? 'code' : editor, + fileName, + lineNumber, + columnNumber + ) args.push.apply(args, extraArgs) } else { args.push(fileName) @@ -119,20 +124,30 @@ function launchEditor (file, specifiedEditor, onErrorCallback) { ['/C', editor].concat(args), { stdio: 'inherit' } ) + } else if (editor === 'code' || editor.includes('Visual Studio Code.app')) { + // Error 'error error: spawn code enoent' will be thrown in the vscode environment, so 'exec' is used + childProcess.exec(`"${editor}" ${args.join(' ')}`, (error) => { + if (error) { + onErrorCallback(fileName, error.message) + } + }) } else { _childProcess = childProcess.spawn(editor, args, { stdio: 'inherit' }) } - _childProcess.on('exit', function (errorCode) { - _childProcess = null + if (_childProcess) { + _childProcess.on('exit', function (errorCode) { + _childProcess = null - if (errorCode) { - onErrorCallback(fileName, '(code ' + errorCode + ')') - } - }) + if (errorCode) { + onErrorCallback(fileName, '(code ' + errorCode + ')') + } + }) - _childProcess.on('error', function (error) { - onErrorCallback(fileName, error.message) - }) + _childProcess.on('error', function (error) { + console.log('error', error) + onErrorCallback(fileName, error.message) + }) + } } module.exports = launchEditor diff --git a/packages/launch-editor/package.json b/packages/launch-editor/package.json index 181ca9d..2d9d36a 100644 --- a/packages/launch-editor/package.json +++ b/packages/launch-editor/package.json @@ -1,6 +1,6 @@ { "name": "launch-editor", - "version": "2.6.0", + "version": "3.0.0", "description": "launch editor from node.js", "main": "index.js", "repository": { diff --git a/packages/launch-editor/test.js b/packages/launch-editor/test.js new file mode 100644 index 0000000..2df2551 --- /dev/null +++ b/packages/launch-editor/test.js @@ -0,0 +1,3 @@ +const launchEditor = require('./index') + +launchEditor('/Volumes/dev/launch-editor/packages/launch-editor/index.js:11:5', '"/Users/zhuangjunlin/Desktop/Visual Studio Code.app/Contents/MacOS/Electron"') From 8cbbb8581ca1142c3eb9f06a8273578dff8cde78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BA=84=E4=BF=8A=E9=9C=96?= Date: Tue, 16 May 2023 18:21:46 +0800 Subject: [PATCH 2/2] fix: fix the problem of error when there are spaces in the path --- packages/launch-editor/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/launch-editor/index.js b/packages/launch-editor/index.js index f2420de..924b4dd 100644 --- a/packages/launch-editor/index.js +++ b/packages/launch-editor/index.js @@ -121,7 +121,7 @@ function launchEditor (file, specifiedEditor, onErrorCallback) { // launch .exe files. _childProcess = childProcess.spawn( 'cmd.exe', - ['/C', editor].concat(args), + ['/C start ""', `"${editor}"`].concat(args), { stdio: 'inherit' } ) } else if (editor === 'code' || editor.includes('Visual Studio Code.app')) {