import * as _ from 'lodash' ; import * as fs from 'fs' ; import * as util from 'util' ; import * as os from 'os' ; import * as path from 'path' ; import { canAccess , writeFile , renameFile , readFile } from '../../util/fs' ; import { reportError } from '../../error-tracking' ; import { OVERRIDE_BIN_PATH } from './terminal-env-overrides' ; const POSIX_OVERRIDE_BIN_PATH = process . platform === 'win32' ? OVERRIDE_BIN_PATH . replace ( /\\/g , '/' ) . replace ( /^(\w+):/ , ( _all , driveLetter ) => `/${ driveLetter . toLowerCase ( ) }` ) : OVERRIDE_BIN_PATH ; const SHELL = ( process . env . SHELL || '' ) . split ( '/' ) . slice ( - 1 ) [ 0 ] ; const appendOrCreateFile = util . promisify ( fs . appendFile ) ; const appendToFirstExisting = async ( paths , forceWrite , contents ) => { for ( let path of paths ) { if ( await canAccess ( path ) ) { return appendOrCreateFile ( path , contents ) ; } } if ( forceWrite ) { return appendOrCreateFile ( paths . slice ( - 1 ) [ 0 ] , contents ) ; } } ; const START_CONFIG_SECTION = '# --httptoolkit--' ; const END_CONFIG_SECTION = '# --httptoolkit-end--' ; const SH_SHELL_PATH_CONFIG = `
${ START_CONFIG_SECTION }
# This section will be reset each time a HTTP Toolkit terminal is opened
if [ -n "$HTTP_TOOLKIT_ACTIVE" ]; then
    # When HTTP Toolkit is active, we inject various overrides into PATH
    export PATH="${ POSIX_OVERRIDE_BIN_PATH }:$PATH"

    if command -v winpty >/dev/null 2>&1; then
        # Work around for winpty's hijacking of certain commands
        alias php=php
        alias node=node
    fi
fi
${ END_CONFIG_SECTION }` ; const FISH_SHELL_PATH_CONFIG = `
${ START_CONFIG_SECTION }
# This section will be reset each time a HTTP Toolkit terminal is opened
if [ -n "$HTTP_TOOLKIT_ACTIVE" ]
    # When HTTP Toolkit is active, we inject various overrides into PATH
    set -x PATH "${ POSIX_OVERRIDE_BIN_PATH }" $PATH;

    if command -v winpty >/dev/null 2>&1
        # Work around for winpty's hijacking of certain commands
        alias php=php
        alias node=node
    end
end
${ END_CONFIG_SECTION }` ; export const getShellScript = ( callbackUrl , env ) => `${ _ . map ( env , ( value , key ) => `    export ${ key }="${ value . replace ( /"/g , '\\"' ) }"` ) . join ( '\n' ) }

    if command -v winpty >/dev/null 2>&1; then
        # Work around for winpty's hijacking of certain commands
        alias php=php
        alias node=node
    fi

    if command -v curl >/dev/null 2>&1; then
        # Let the HTTP Toolkit app know this ran succesfully
        (curl --noproxy '*' -X POST "${ callbackUrl }" >/dev/null 2>&1 &) &> /dev/null
    fi

    echo 'HTTP Toolkit interception enabled'
` ; export const editShellStartupScripts = async ( ) => { await resetShellStartupScripts ( ) ; appendOrCreateFile ( path . join ( os . homedir ( ) , '.profile' ) , SH_SHELL_PATH_CONFIG ) . catch ( reportError ) ; appendToFirstExisting ( [ path . join ( os . homedir ( ) , '.bash_profile' ) , path . join ( os . homedir ( ) , '.bash_login' ) ] , false , SH_SHELL_PATH_CONFIG ) . catch ( reportError ) ; appendToFirstExisting ( [ path . join ( os . homedir ( ) , '.bashrc' ) ] , SHELL === 'bash' , SH_SHELL_PATH_CONFIG ) . catch ( reportError ) ; appendToFirstExisting ( [ path . join ( os . homedir ( ) , '.zshenv' ) , path . join ( os . homedir ( ) , '.zshrc' ) ] , SHELL === 'zsh' , SH_SHELL_PATH_CONFIG ) . catch ( reportError ) ; appendToFirstExisting ( [ path . join ( os . homedir ( ) , '.config' , 'fish' , 'config.fish' ) , ] , SHELL === 'fish' || await canAccess ( path . join ( os . homedir ( ) , '.config' , 'fish' ) ) , FISH_SHELL_PATH_CONFIG ) . catch ( reportError ) ; } ; const removeConfigSectionsFromFile = async ( path ) => { let fileLines ; try { fileLines = ( await readFile ( path , 'utf8' ) ) . split ( '\n' ) ; } catch ( e ) { return ; } let sectionStart = _ . findIndex ( fileLines , ( l ) => l . startsWith ( START_CONFIG_SECTION ) ) ; while ( sectionStart !== - 1 ) { let sectionEnd = _ . findIndex ( fileLines , ( l ) => l . startsWith ( END_CONFIG_SECTION ) ) ; if ( sectionEnd === - 1 || sectionEnd <= sectionStart ) return ; fileLines . splice ( sectionStart , ( sectionEnd - sectionStart ) + 1 ) ; sectionStart = _ . findIndex ( fileLines , ( l ) => l . startsWith ( START_CONFIG_SECTION ) ) ; } const tempFile = path + Date . now ( ) + '.temp' ; await writeFile ( tempFile , fileLines . join ( '\n' ) ) ; return renameFile ( tempFile , path ) ; } ; export const resetShellStartupScripts = ( ) => { return Promise . all ( [ path . join ( os . homedir ( ) , '.profile' ) , path . join ( os . homedir ( ) , '.bash_profile' ) , path . join ( os . homedir ( ) , '.bash_login' ) , path . join ( os . homedir ( ) , '.bashrc' ) , path . join ( os . homedir ( ) , '.zshenv' ) , path . join ( os . homedir ( ) , '.zshrc' ) , path . join ( os . homedir ( ) , '.config' , 'fish' , 'config.fish' ) , ] . map ( ( configFile ) => removeConfigSectionsFromFile ( configFile ) . catch ( reportError ) ) ) ; } ;