Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix path with whitespace and fix "spawn" in vscode #57

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions packages/launch-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -116,23 +121,33 @@ 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')) {
// 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
2 changes: 1 addition & 1 deletion packages/launch-editor/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
3 changes: 3 additions & 0 deletions packages/launch-editor/test.js
Original file line number Diff line number Diff line change
@@ -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"')