diff --git a/packager/launchEditor.js b/packager/launchEditor.js index cf89ed4f04b..b572b5cbd7c 100644 --- a/packager/launchEditor.js +++ b/packager/launchEditor.js @@ -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;