1import spawnAsync from '@expo/spawn-async'; 2import editors from 'env-editor'; 3 4import { env } from './env'; 5import * as Log from '../log'; 6 7const debug = require('debug')('expo:utils:editor') as typeof console.log; 8 9/** Guess what the default editor is and default to VSCode. */ 10export function guessEditor(): editors.Editor { 11 try { 12 const editor = env.EXPO_EDITOR; 13 if (editor) { 14 debug('Using $EXPO_EDITOR:', editor); 15 return editors.getEditor(editor); 16 } 17 debug('Falling back on $EDITOR:', editor); 18 return editors.defaultEditor(); 19 } catch { 20 debug('Falling back on vscode'); 21 return editors.getEditor('vscode'); 22 } 23} 24 25/** Open a file path in a given editor. */ 26export async function openInEditorAsync(path: string): Promise<boolean> { 27 const editor = guessEditor(); 28 29 debug(`Opening ${path} in ${editor?.name} (bin: ${editor?.binary}, id: ${editor?.id})`); 30 if (editor) { 31 try { 32 await spawnAsync(editor.binary, [path]); 33 return true; 34 } catch (error: any) { 35 debug(`Failed to auto open path in editor (path: ${path}, binary: ${editor.binary}):`, error); 36 } 37 } 38 39 Log.error( 40 '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")' 41 ); 42 return false; 43} 44