2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-03-23 18:48:02 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-04-24 17:31:28 +00:00
|
|
|
var chalk = require('chalk');
|
2015-02-20 04:10:52 +00:00
|
|
|
var fs = require('fs');
|
Fix various issues with packager editor launcher
Summary: There are a few small bugs with the code that launches the editor from the packager:
* First of all, the filepath is not escaped which means tokens like `(` or spaces will mess up the process execution. Dropbox unfortunately decided to use spaces in its enterprise product, so I was getting this error:
![screen shot 2015-07-11 at 3 20 54 pm](https://cloud.githubusercontent.com/assets/1135007/8635748/186e7f2e-27ea-11e5-8058-1f4dabb79634.png)
* Next, the line number argument formatting was assumed to be in a specific format (`:%d`) which actually errors out vim and other editors.
* Lastly, the process was started synchronously but not attached to the stdin / stdout of the parent process. This means that only editors like mvim, sublime, and others would work since they spawn a new window. Editors like emacs, vi, nano, etc wouldn't work and instead just hang at the command line.
So I whipped up this diff to fix a number of these issues, demo here:
http://recordit.co/M6zwiUj7hp
The demo shows both
Closes https://github.com/facebook/react-native/pull/1957
Reviewed By: @vjeux, @pcottle
Differential Revision: D2420941
Pulled By: @frantic
2015-09-14 16:57:43 +00:00
|
|
|
var spawn = require('child_process').spawn;
|
|
|
|
|
|
|
|
function isTerminalEditor(editor) {
|
|
|
|
switch (editor) {
|
|
|
|
case 'vim':
|
|
|
|
case 'emacs':
|
|
|
|
case 'nano':
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getArgumentsForLineNumber(editor, fileName, lineNumber) {
|
|
|
|
switch (editor) {
|
|
|
|
case 'vim':
|
|
|
|
case 'mvim':
|
|
|
|
return [fileName, '+' + lineNumber];
|
|
|
|
case 'atom':
|
|
|
|
case 'subl':
|
|
|
|
case 'sublime':
|
|
|
|
return [fileName + ':' + lineNumber];
|
|
|
|
case 'joe':
|
|
|
|
case 'emacs':
|
2015-10-21 16:55:21 +00:00
|
|
|
case 'emacsclient':
|
Fix various issues with packager editor launcher
Summary: There are a few small bugs with the code that launches the editor from the packager:
* First of all, the filepath is not escaped which means tokens like `(` or spaces will mess up the process execution. Dropbox unfortunately decided to use spaces in its enterprise product, so I was getting this error:
![screen shot 2015-07-11 at 3 20 54 pm](https://cloud.githubusercontent.com/assets/1135007/8635748/186e7f2e-27ea-11e5-8058-1f4dabb79634.png)
* Next, the line number argument formatting was assumed to be in a specific format (`:%d`) which actually errors out vim and other editors.
* Lastly, the process was started synchronously but not attached to the stdin / stdout of the parent process. This means that only editors like mvim, sublime, and others would work since they spawn a new window. Editors like emacs, vi, nano, etc wouldn't work and instead just hang at the command line.
So I whipped up this diff to fix a number of these issues, demo here:
http://recordit.co/M6zwiUj7hp
The demo shows both
Closes https://github.com/facebook/react-native/pull/1957
Reviewed By: @vjeux, @pcottle
Differential Revision: D2420941
Pulled By: @frantic
2015-09-14 16:57:43 +00:00
|
|
|
return ['+' + lineNumber, fileName];
|
2015-09-23 23:59:44 +00:00
|
|
|
case 'rmate':
|
|
|
|
case 'mate':
|
|
|
|
return ['--line', lineNumber, fileName];
|
Fix various issues with packager editor launcher
Summary: There are a few small bugs with the code that launches the editor from the packager:
* First of all, the filepath is not escaped which means tokens like `(` or spaces will mess up the process execution. Dropbox unfortunately decided to use spaces in its enterprise product, so I was getting this error:
![screen shot 2015-07-11 at 3 20 54 pm](https://cloud.githubusercontent.com/assets/1135007/8635748/186e7f2e-27ea-11e5-8058-1f4dabb79634.png)
* Next, the line number argument formatting was assumed to be in a specific format (`:%d`) which actually errors out vim and other editors.
* Lastly, the process was started synchronously but not attached to the stdin / stdout of the parent process. This means that only editors like mvim, sublime, and others would work since they spawn a new window. Editors like emacs, vi, nano, etc wouldn't work and instead just hang at the command line.
So I whipped up this diff to fix a number of these issues, demo here:
http://recordit.co/M6zwiUj7hp
The demo shows both
Closes https://github.com/facebook/react-native/pull/1957
Reviewed By: @vjeux, @pcottle
Differential Revision: D2420941
Pulled By: @frantic
2015-09-14 16:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For all others, drop the lineNumber until we have
|
|
|
|
// a mapping above, since providing the lineNumber incorrectly
|
|
|
|
// can result in errors or confusing behavior.
|
|
|
|
return [fileName];
|
|
|
|
}
|
2015-04-24 17:31:28 +00:00
|
|
|
|
|
|
|
function printInstructions(title) {
|
|
|
|
console.log([
|
|
|
|
'',
|
|
|
|
chalk.bgBlue.white.bold(' ' + title + ' '),
|
|
|
|
' When you see Red Box with stack trace, you can click any ',
|
|
|
|
' stack frame to jump to the source file. The packager will launch your ',
|
|
|
|
' editor of choice. It will first look at REACT_EDITOR environment ',
|
|
|
|
' variable, then at EDITOR. To set it up, you can add something like ',
|
|
|
|
' REACT_EDITOR=atom to your .bashrc.',
|
|
|
|
''
|
|
|
|
].join('\n'));
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
Fix various issues with packager editor launcher
Summary: There are a few small bugs with the code that launches the editor from the packager:
* First of all, the filepath is not escaped which means tokens like `(` or spaces will mess up the process execution. Dropbox unfortunately decided to use spaces in its enterprise product, so I was getting this error:
![screen shot 2015-07-11 at 3 20 54 pm](https://cloud.githubusercontent.com/assets/1135007/8635748/186e7f2e-27ea-11e5-8058-1f4dabb79634.png)
* Next, the line number argument formatting was assumed to be in a specific format (`:%d`) which actually errors out vim and other editors.
* Lastly, the process was started synchronously but not attached to the stdin / stdout of the parent process. This means that only editors like mvim, sublime, and others would work since they spawn a new window. Editors like emacs, vi, nano, etc wouldn't work and instead just hang at the command line.
So I whipped up this diff to fix a number of these issues, demo here:
http://recordit.co/M6zwiUj7hp
The demo shows both
Closes https://github.com/facebook/react-native/pull/1957
Reviewed By: @vjeux, @pcottle
Differential Revision: D2420941
Pulled By: @frantic
2015-09-14 16:57:43 +00:00
|
|
|
var _childProcess = null;
|
2015-02-20 04:10:52 +00:00
|
|
|
function launchEditor(fileName, lineNumber) {
|
|
|
|
if (!fs.existsSync(fileName)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Fix various issues with packager editor launcher
Summary: There are a few small bugs with the code that launches the editor from the packager:
* First of all, the filepath is not escaped which means tokens like `(` or spaces will mess up the process execution. Dropbox unfortunately decided to use spaces in its enterprise product, so I was getting this error:
![screen shot 2015-07-11 at 3 20 54 pm](https://cloud.githubusercontent.com/assets/1135007/8635748/186e7f2e-27ea-11e5-8058-1f4dabb79634.png)
* Next, the line number argument formatting was assumed to be in a specific format (`:%d`) which actually errors out vim and other editors.
* Lastly, the process was started synchronously but not attached to the stdin / stdout of the parent process. This means that only editors like mvim, sublime, and others would work since they spawn a new window. Editors like emacs, vi, nano, etc wouldn't work and instead just hang at the command line.
So I whipped up this diff to fix a number of these issues, demo here:
http://recordit.co/M6zwiUj7hp
The demo shows both
Closes https://github.com/facebook/react-native/pull/1957
Reviewed By: @vjeux, @pcottle
Differential Revision: D2420941
Pulled By: @frantic
2015-09-14 16:57:43 +00:00
|
|
|
var editor = process.env.REACT_EDITOR || process.env.EDITOR;
|
|
|
|
if (!editor) {
|
|
|
|
printInstructions('PRO TIP');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var args = [fileName];
|
2015-02-20 04:10:52 +00:00
|
|
|
if (lineNumber) {
|
Fix various issues with packager editor launcher
Summary: There are a few small bugs with the code that launches the editor from the packager:
* First of all, the filepath is not escaped which means tokens like `(` or spaces will mess up the process execution. Dropbox unfortunately decided to use spaces in its enterprise product, so I was getting this error:
![screen shot 2015-07-11 at 3 20 54 pm](https://cloud.githubusercontent.com/assets/1135007/8635748/186e7f2e-27ea-11e5-8058-1f4dabb79634.png)
* Next, the line number argument formatting was assumed to be in a specific format (`:%d`) which actually errors out vim and other editors.
* Lastly, the process was started synchronously but not attached to the stdin / stdout of the parent process. This means that only editors like mvim, sublime, and others would work since they spawn a new window. Editors like emacs, vi, nano, etc wouldn't work and instead just hang at the command line.
So I whipped up this diff to fix a number of these issues, demo here:
http://recordit.co/M6zwiUj7hp
The demo shows both
Closes https://github.com/facebook/react-native/pull/1957
Reviewed By: @vjeux, @pcottle
Differential Revision: D2420941
Pulled By: @frantic
2015-09-14 16:57:43 +00:00
|
|
|
args = getArgumentsForLineNumber(editor, fileName, lineNumber);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
Fix various issues with packager editor launcher
Summary: There are a few small bugs with the code that launches the editor from the packager:
* First of all, the filepath is not escaped which means tokens like `(` or spaces will mess up the process execution. Dropbox unfortunately decided to use spaces in its enterprise product, so I was getting this error:
![screen shot 2015-07-11 at 3 20 54 pm](https://cloud.githubusercontent.com/assets/1135007/8635748/186e7f2e-27ea-11e5-8058-1f4dabb79634.png)
* Next, the line number argument formatting was assumed to be in a specific format (`:%d`) which actually errors out vim and other editors.
* Lastly, the process was started synchronously but not attached to the stdin / stdout of the parent process. This means that only editors like mvim, sublime, and others would work since they spawn a new window. Editors like emacs, vi, nano, etc wouldn't work and instead just hang at the command line.
So I whipped up this diff to fix a number of these issues, demo here:
http://recordit.co/M6zwiUj7hp
The demo shows both
Closes https://github.com/facebook/react-native/pull/1957
Reviewed By: @vjeux, @pcottle
Differential Revision: D2420941
Pulled By: @frantic
2015-09-14 16:57:43 +00:00
|
|
|
console.log('Opening ' + chalk.underline(fileName) + ' with ' + chalk.bold(editor));
|
2015-02-20 04:10:52 +00:00
|
|
|
|
Fix various issues with packager editor launcher
Summary: There are a few small bugs with the code that launches the editor from the packager:
* First of all, the filepath is not escaped which means tokens like `(` or spaces will mess up the process execution. Dropbox unfortunately decided to use spaces in its enterprise product, so I was getting this error:
![screen shot 2015-07-11 at 3 20 54 pm](https://cloud.githubusercontent.com/assets/1135007/8635748/186e7f2e-27ea-11e5-8058-1f4dabb79634.png)
* Next, the line number argument formatting was assumed to be in a specific format (`:%d`) which actually errors out vim and other editors.
* Lastly, the process was started synchronously but not attached to the stdin / stdout of the parent process. This means that only editors like mvim, sublime, and others would work since they spawn a new window. Editors like emacs, vi, nano, etc wouldn't work and instead just hang at the command line.
So I whipped up this diff to fix a number of these issues, demo here:
http://recordit.co/M6zwiUj7hp
The demo shows both
Closes https://github.com/facebook/react-native/pull/1957
Reviewed By: @vjeux, @pcottle
Differential Revision: D2420941
Pulled By: @frantic
2015-09-14 16:57:43 +00:00
|
|
|
if (_childProcess && isTerminalEditor(editor)) {
|
|
|
|
// 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');
|
2015-04-24 17:31:28 +00:00
|
|
|
}
|
Fix various issues with packager editor launcher
Summary: There are a few small bugs with the code that launches the editor from the packager:
* First of all, the filepath is not escaped which means tokens like `(` or spaces will mess up the process execution. Dropbox unfortunately decided to use spaces in its enterprise product, so I was getting this error:
![screen shot 2015-07-11 at 3 20 54 pm](https://cloud.githubusercontent.com/assets/1135007/8635748/186e7f2e-27ea-11e5-8058-1f4dabb79634.png)
* Next, the line number argument formatting was assumed to be in a specific format (`:%d`) which actually errors out vim and other editors.
* Lastly, the process was started synchronously but not attached to the stdin / stdout of the parent process. This means that only editors like mvim, sublime, and others would work since they spawn a new window. Editors like emacs, vi, nano, etc wouldn't work and instead just hang at the command line.
So I whipped up this diff to fix a number of these issues, demo here:
http://recordit.co/M6zwiUj7hp
The demo shows both
Closes https://github.com/facebook/react-native/pull/1957
Reviewed By: @vjeux, @pcottle
Differential Revision: D2420941
Pulled By: @frantic
2015-09-14 16:57:43 +00:00
|
|
|
|
|
|
|
_childProcess = spawn(editor, args, {stdio: 'inherit'});
|
|
|
|
_childProcess.on('exit', function(errorCode) {
|
|
|
|
_childProcess = null;
|
|
|
|
|
|
|
|
if (errorCode) {
|
|
|
|
console.log(chalk.red('Your editor exited with an error!'));
|
|
|
|
printInstructions('Keep these instructions in mind:');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
_childProcess.on('error', function(error) {
|
|
|
|
console.log(chalk.red(error.message));
|
|
|
|
printInstructions('How to fix:');
|
|
|
|
})
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = launchEditor;
|