@@ -82,16 +82,6 @@ const getTerminalCommand = _.memoize(async (): Promise<SpawnArgs | null> => {
82
82
return null ;
83
83
} ) ;
84
84
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
-
95
85
const appendOrCreateFile = util . promisify ( fs . appendFile ) ;
96
86
const appendToFirstExisting = async ( paths : string [ ] , forceWrite : boolean , contents : string ) => {
97
87
for ( let path of paths ) {
@@ -107,6 +97,33 @@ const appendToFirstExisting = async (paths: string[], forceWrite: boolean, conte
107
97
}
108
98
} ;
109
99
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
+
110
127
// Find the relevant user shell config file, add the above line to it, so that
111
128
// shells launched with HTTP_TOOLKIT_ACTIVE set use the interception PATH.
112
129
const editShellStartupScripts = async ( ) => {
@@ -149,7 +166,7 @@ const editShellStartupScripts = async () => {
149
166
const readFile = util . promisify ( fs . readFile ) ;
150
167
const writeFile = util . promisify ( fs . writeFile ) ;
151
168
const renameFile = util . promisify ( fs . rename ) ;
152
- const removeMatchingInFile = async ( path : string , matcher : RegExp ) => {
169
+ const removeConfigSectionsFromFile = async ( path : string ) => {
153
170
let fileLines : string [ ] ;
154
171
155
172
try {
@@ -159,8 +176,16 @@ const removeMatchingInFile = async (path: string, matcher: RegExp) => {
159
176
return ;
160
177
}
161
178
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
+
164
189
// Write & rename to ensure this is atomic, and avoid races here
165
190
// as much as we reasonably can.
166
191
const tempFile = path + Date . now ( ) + '.temp' ;
@@ -181,8 +206,7 @@ const resetShellStartupScripts = () => {
181
206
path . join ( os . homedir ( ) , '.zshrc' ) ,
182
207
path . join ( os . homedir ( ) , '.config' , 'fish' , 'config.fish' ) ,
183
208
] . map ( ( configFile ) =>
184
- removeMatchingInFile ( configFile , SHELL_PATH_CONFIG_MATCHER )
185
- . catch ( reportError )
209
+ removeConfigSectionsFromFile ( configFile ) . catch ( reportError )
186
210
) ) ;
187
211
} ;
188
212
0 commit comments