Allow arguments in the editor env var

Reviewed By: frantic

Differential Revision: D4129207

fbshipit-source-id: a92fbbbe917db9c9afc0d970e3ac546e2a14bced
This commit is contained in:
Matthew Dapena-Tretter 2016-11-04 12:56:39 -07:00 committed by Facebook Github Bot
parent c02c7f3024
commit a643784144
2 changed files with 17 additions and 7 deletions

View File

@ -13,6 +13,7 @@ var fs = require('fs');
var path = require('path');
var child_process = require('child_process');
const isAbsolutePath = require('absolute-path');
const shellQuote = require('shell-quote');
function isTerminalEditor(editor) {
switch (editor) {
@ -49,7 +50,8 @@ function getArgumentsForLineNumber(editor, fileName, lineNumber, workspace) {
case 'mvim':
return [fileName, '+' + lineNumber];
case 'atom':
return addWorkspaceToArgumentsIfExists([fileName + ':' + lineNumber], workspace);
case 'Atom':
case 'Atom Beta':
case 'subl':
case 'sublime':
case 'wstorm':
@ -77,7 +79,7 @@ function getArgumentsForLineNumber(editor, fileName, lineNumber, workspace) {
function guessEditor() {
// Explicit config always wins
if (process.env.REACT_EDITOR) {
return process.env.REACT_EDITOR;
return shellQuote.parse(process.env.REACT_EDITOR);
}
// Using `ps x` on OSX we can find out which editor is currently running.
@ -89,7 +91,7 @@ function guessEditor() {
for (var i = 0; i < processNames.length; i++) {
var processName = processNames[i];
if (output.indexOf(processName) !== -1) {
return COMMON_EDITORS[processName];
return [COMMON_EDITORS[processName]];
}
}
} catch(error) {
@ -98,7 +100,13 @@ function guessEditor() {
}
// Last resort, use old skool env vars
return process.env.VISUAL || process.env.EDITOR;
if (process.env.VISUAL) {
return [process.env.VISUAL];
} else if (process.env.EDITOR) {
return [process.env.EDITOR];
}
return null;
}
function printInstructions(title) {
@ -142,16 +150,17 @@ function launchEditor(fileName, lineNumber, projectRoots) {
return;
}
var editor = guessEditor();
const [editor, ...args] = guessEditor();
if (!editor) {
printInstructions('PRO TIP');
return;
}
var workspace = findRootForFile(projectRoots, fileName);
var args = [fileName];
if (lineNumber) {
args = getArgumentsForLineNumber(editor, fileName, lineNumber, workspace);
args.push(getArgumentsForLineNumber(editor, fileName, lineNumber, workspace));
} else {
args.push(fileName);
}
console.log('Opening ' + chalk.underline(fileName) + ' with ' + chalk.bold(editor));

View File

@ -194,6 +194,7 @@
"regenerator-runtime": "^0.9.5",
"sane": "~1.4.1",
"semver": "^5.0.3",
"shell-quote": "1.6.1",
"source-map": "^0.5.6",
"stacktrace-parser": "^0.1.3",
"temp": "0.8.3",