Support vscode as a React Editor

Summary:
When opening a text editor from react native there are a list of editors being supported. This PR adds `VSCode` to that.

As a difference with the current supported editors, `VSCode` has a notion of `workspace` (a directory that contains all of the project's files). The `workspace` can be passed to `VSCode` as a parameter so that if the workspace is already open we don't get new instances of `VSCode` every time a new file is open. The `workspace` is gotten by comparing the file location with the different `project roots`.

This code relies on `VSCode`'s `code` command, which it's documentation can be found at: https://code.visualstudio.com/Docs/editor/codebasics#_launching-from-the-command-line
Closes https://github.com/facebook/react-native/pull/7757

Differential Revision: D3463761

fbshipit-source-id: ee9ec999747ad6d16d95ec8317f551f3535286c9
This commit is contained in:
Patricio Beltran 2016-06-21 10:45:44 -07:00 committed by Facebook Github Bot 7
parent 0cdad4f52f
commit d1690a8f9e
3 changed files with 42 additions and 12 deletions

View File

@ -10,12 +10,14 @@
const launchEditor = require('../util/launchEditor');
module.exports = function(req, res, next) {
if (req.url === '/open-stack-frame') {
var frame = JSON.parse(req.rawBody);
launchEditor(frame.file, frame.lineNumber);
res.end('OK');
} else {
next();
}
module.exports = function(args) {
return function(req, res, next) {
if (req.url === '/open-stack-frame') {
var frame = JSON.parse(req.rawBody);
launchEditor(frame.file, frame.lineNumber, args['projectRoots']);
res.end('OK');
} else {
next();
}
};
};

View File

@ -32,7 +32,7 @@ function runServer(args, config, readyCallback) {
.use(connect.compress())
.use(getDevToolsMiddleware(args, () => wsProxy && wsProxy.isChromeConnected()))
.use(getDevToolsMiddleware(args, () => ms && ms.isChromeConnected()))
.use(openStackFrameInEditorMiddleware)
.use(openStackFrameInEditorMiddleware(args))
.use(statusPageMiddleware)
.use(systraceProfileMiddleware)
.use(cpuProfilerMiddleware)

View File

@ -12,6 +12,7 @@ var chalk = require('chalk');
var fs = require('fs');
var path = require('path');
var child_process = require('child_process');
const isAbsolutePath = require('absolute-path');
function isTerminalEditor(editor) {
switch (editor) {
@ -32,14 +33,23 @@ var COMMON_EDITORS = {
'/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl',
'/Applications/Sublime Text 2.app/Contents/MacOS/Sublime Text 2':
'/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl',
'/Applications/Visual Studio Code.app/Contents/MacOS/Electron': 'code',
};
function getArgumentsForLineNumber(editor, fileName, lineNumber) {
function addWorkspaceToArgumentsIfExists(args, workspace) {
if (workspace) {
args.unshift(workspace);
}
return args;
}
function getArgumentsForLineNumber(editor, fileName, lineNumber, workspace) {
switch (path.basename(editor)) {
case 'vim':
case 'mvim':
return [fileName, '+' + lineNumber];
case 'atom':
return addWorkspaceToArgumentsIfExists([fileName + ':' + lineNumber], workspace);
case 'subl':
case 'sublime':
return [fileName + ':' + lineNumber];
@ -51,6 +61,8 @@ function getArgumentsForLineNumber(editor, fileName, lineNumber) {
case 'mate':
case 'mine':
return ['--line', lineNumber, fileName];
case 'code':
return addWorkspaceToArgumentsIfExists(['-g', fileName + ':' + lineNumber], workspace);
}
// For all others, drop the lineNumber until we have
@ -100,8 +112,23 @@ function printInstructions(title) {
].join('\n'));
}
function transformToAbsolutePathIfNeeded(pathName) {
if (!isAbsolutePath(pathName)) {
pathName = path.resolve(process.cwd(), pathName);
}
return pathName;
}
function findRootForFile(projectRoots, fileName) {
fileName = transformToAbsolutePathIfNeeded(fileName);
return projectRoots.find((root) => {
root = transformToAbsolutePathIfNeeded(root);
return fileName.startsWith(root + path.sep);
});
}
var _childProcess = null;
function launchEditor(fileName, lineNumber) {
function launchEditor(fileName, lineNumber, projectRoots) {
if (!fs.existsSync(fileName)) {
return;
}
@ -118,9 +145,10 @@ function launchEditor(fileName, lineNumber) {
return;
}
var workspace = findRootForFile(projectRoots, fileName);
var args = [fileName];
if (lineNumber) {
args = getArgumentsForLineNumber(editor, fileName, lineNumber);
args = getArgumentsForLineNumber(editor, fileName, lineNumber, workspace);
}
console.log('Opening ' + chalk.underline(fileName) + ' with ' + chalk.bold(editor));