18d307f52SEvan Baconimport spawnAsync from '@expo/spawn-async'; 28d307f52SEvan Baconimport editors from 'env-editor'; 38d307f52SEvan Bacon 4873c6c5aSEvan Baconimport { env } from './env'; 5*8a424bebSJames Ideimport * as Log from '../log'; 68d307f52SEvan Bacon 7474a7a4bSEvan Baconconst debug = require('debug')('expo:utils:editor') as typeof console.log; 8474a7a4bSEvan Bacon 98d307f52SEvan Bacon/** Guess what the default editor is and default to VSCode. */ 108d307f52SEvan Baconexport function guessEditor(): editors.Editor { 118d307f52SEvan Bacon try { 12873c6c5aSEvan Bacon const editor = env.EXPO_EDITOR; 13873c6c5aSEvan Bacon if (editor) { 14873c6c5aSEvan Bacon debug('Using $EXPO_EDITOR:', editor); 15873c6c5aSEvan Bacon return editors.getEditor(editor); 16873c6c5aSEvan Bacon } 17873c6c5aSEvan Bacon debug('Falling back on $EDITOR:', editor); 188d307f52SEvan Bacon return editors.defaultEditor(); 198d307f52SEvan Bacon } catch { 20873c6c5aSEvan Bacon debug('Falling back on vscode'); 218d307f52SEvan Bacon return editors.getEditor('vscode'); 228d307f52SEvan Bacon } 238d307f52SEvan Bacon} 248d307f52SEvan Bacon 258d307f52SEvan Bacon/** Open a file path in a given editor. */ 268d307f52SEvan Baconexport async function openInEditorAsync(path: string): Promise<boolean> { 278d307f52SEvan Bacon const editor = guessEditor(); 288d307f52SEvan Bacon 29474a7a4bSEvan Bacon debug(`Opening ${path} in ${editor?.name} (bin: ${editor?.binary}, id: ${editor?.id})`); 308d307f52SEvan Bacon if (editor) { 318d307f52SEvan Bacon try { 328d307f52SEvan Bacon await spawnAsync(editor.binary, [path]); 338d307f52SEvan Bacon return true; 3429975bfdSEvan Bacon } catch (error: any) { 35474a7a4bSEvan Bacon debug(`Failed to auto open path in editor (path: ${path}, binary: ${editor.binary}):`, error); 368d307f52SEvan Bacon } 378d307f52SEvan Bacon } 388d307f52SEvan Bacon 398d307f52SEvan Bacon Log.error( 408d307f52SEvan Bacon 'Could not open editor, you can set it by defining the $EDITOR environment variable with the binary of your editor. (e.g. "vscode" or "atom")' 418d307f52SEvan Bacon ); 428d307f52SEvan Bacon return false; 438d307f52SEvan Bacon} 44