@@ -82,16 +82,6 @@ const getTerminalCommand = _.memoize(async (): Promise<SpawnArgs | null> => {
8282 return null ;
8383} ) ;
8484
85- // Works in bash, zsh, dash, ksh, sh (not fish)
86- const SH_SHELL_PATH_CONFIG = `
87- # Do not edit (all lines including $HTTP_TOOLKIT_ACTIVE will be removed automatically)
88- if [ -n "$HTTP_TOOLKIT_ACTIVE" ]; then export PATH="${ OVERRIDE_BIN_PATH } :$PATH"; fi` ;
89- const FISH_SHELL_PATH_CONFIG = `
90- # Do not edit (all lines including $HTTP_TOOLKIT_ACTIVE will be removed automatically)
91- [ -n "$HTTP_TOOLKIT_ACTIVE" ]; and set -x PATH "${ OVERRIDE_BIN_PATH } " $PATH;` ;
92- // Used to remove these lines from the config later
93- const SHELL_PATH_CONFIG_MATCHER = / .* \$ H T T P _ T O O L K I T _ A C T I V E .* / ;
94-
9585const appendOrCreateFile = util . promisify ( fs . appendFile ) ;
9686const appendToFirstExisting = async ( paths : string [ ] , forceWrite : boolean , contents : string ) => {
9787 for ( let path of paths ) {
@@ -107,6 +97,33 @@ const appendToFirstExisting = async (paths: string[], forceWrite: boolean, conte
10797 }
10898} ;
10999
100+ const START_CONFIG_SECTION = '# --httptoolkit--' ;
101+ const END_CONFIG_SECTION = '# --httptoolkit-end--' ;
102+
103+ // Works in bash, zsh, dash, ksh, sh (not fish)
104+ const SH_SHELL_PATH_CONFIG = `
105+ ${ START_CONFIG_SECTION } This section will be removed shortly
106+ if [ -n "$HTTP_TOOLKIT_ACTIVE" ]; then
107+ export PATH="${ POSIX_OVERRIDE_BIN_PATH } :$PATH"
108+
109+ if command -v winpty >/dev/null 2>&1; then
110+ # Work around for winpty's hijacking of certain commands
111+ alias php=php
112+ fi
113+ fi
114+ ${ END_CONFIG_SECTION } `;
115+ const FISH_SHELL_PATH_CONFIG = `
116+ ${ START_CONFIG_SECTION } This section will be removed shortly
117+ if [ -n "$HTTP_TOOLKIT_ACTIVE" ]
118+ set -x PATH "${ POSIX_OVERRIDE_BIN_PATH } " $PATH;
119+
120+ if command -v winpty >/dev/null 2>&1
121+ # Work around for winpty's hijacking of certain commands
122+ alias php=php
123+ end
124+ end
125+ ${ END_CONFIG_SECTION } `;
126+
110127// Find the relevant user shell config file, add the above line to it, so that
111128// shells launched with HTTP_TOOLKIT_ACTIVE set use the interception PATH.
112129const editShellStartupScripts = async ( ) => {
@@ -149,7 +166,7 @@ const editShellStartupScripts = async () => {
149166const readFile = util . promisify ( fs . readFile ) ;
150167const writeFile = util . promisify ( fs . writeFile ) ;
151168const renameFile = util . promisify ( fs . rename ) ;
152- const removeMatchingInFile = async ( path : string , matcher : RegExp ) => {
169+ const removeConfigSectionsFromFile = async ( path : string ) => {
153170 let fileLines : string [ ] ;
154171
155172 try {
@@ -159,8 +176,16 @@ const removeMatchingInFile = async (path: string, matcher: RegExp) => {
159176 return ;
160177 }
161178
162- // Drop all matching lines from the config file
163- fileLines = fileLines . filter ( line => ! matcher . test ( line ) ) ;
179+ // Remove everything between each pair of start/end section markers
180+ let sectionStart = _ . findIndex ( fileLines , ( l ) => l . startsWith ( START_CONFIG_SECTION ) ) ;
181+ while ( sectionStart !== - 1 ) {
182+ let sectionEnd = _ . findIndex ( fileLines , ( l ) => l . startsWith ( END_CONFIG_SECTION ) ) ;
183+
184+ if ( sectionEnd === - 1 || sectionEnd <= sectionStart ) return ; // Odd config file state - don't edit it
185+ fileLines . splice ( sectionStart , ( sectionEnd - sectionStart ) + 1 ) ;
186+ sectionStart = _ . findIndex ( fileLines , ( l ) => l . startsWith ( START_CONFIG_SECTION ) ) ;
187+ }
188+
164189 // Write & rename to ensure this is atomic, and avoid races here
165190 // as much as we reasonably can.
166191 const tempFile = path + Date . now ( ) + '.temp' ;
@@ -181,8 +206,7 @@ const resetShellStartupScripts = () => {
181206 path . join ( os . homedir ( ) , '.zshrc' ) ,
182207 path . join ( os . homedir ( ) , '.config' , 'fish' , 'config.fish' ) ,
183208 ] . map ( ( configFile ) =>
184- removeMatchingInFile ( configFile , SHELL_PATH_CONFIG_MATCHER )
185- . catch ( reportError )
209+ removeConfigSectionsFromFile ( configFile ) . catch ( reportError )
186210 ) ) ;
187211} ;
188212
0 commit comments