[ReactNative] Fix launchEditor script

This commit is contained in:
Alex Kotliarskyi 2015-04-24 10:31:28 -07:00
parent 2115b67bd9
commit 04da81d4d5
1 changed files with 25 additions and 18 deletions

View File

@ -8,23 +8,21 @@
*/
'use strict';
var chalk = require('chalk');
var fs = require('fs');
var spawn = require('child_process').spawn;
var exec = require('child_process').exec;
var firstLaunch = true;
function guessEditor() {
if (firstLaunch) {
console.log('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.');
firstLaunch = false;
}
var editor = process.env.REACT_EDITOR || process.env.EDITOR || 'subl';
return editor;
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'));
}
function launchEditor(fileName, lineNumber) {
@ -37,9 +35,18 @@ function launchEditor(fileName, lineNumber) {
argument += ':' + lineNumber;
}
var editor = guessEditor();
console.log('Opening ' + fileName + ' with ' + editor);
spawn(editor, [argument], { stdio: ['pipe', 'pipe', process.stderr] });
var editor = process.env.REACT_EDITOR || process.env.EDITOR;
if (editor) {
console.log('Opening ' + chalk.underline(fileName) + ' with ' + chalk.bold(editor));
exec(editor + ' ' + argument, function(error) {
if (error) {
console.log(chalk.red(error.message));
printInstructions('How to fix');
}
});
} else {
printInstructions('PRO TIP');
}
}
module.exports = launchEditor;