Skip to content

Commit 94f7aeb

Browse files
committed
Add support for Git Bash existing terminal interception
1 parent cd944b8 commit 94f7aeb

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/interceptors/terminal/existing-terminal-interceptor.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Mockttp, getLocal } from 'mockttp';
44
import { Interceptor } from '..';
55
import { HtkConfig } from '../../config';
66
import { getTerminalEnvVars } from './terminal-env-overrides';
7-
import { getBashShellScript, getFishShellScript, getPowerShellScript } from './terminal-scripts';
7+
import { getBashShellScript, getFishShellScript, getGitBashShellScript, getPowerShellScript } from './terminal-scripts';
88

99
interface ServerState {
1010
server: Mockttp;
@@ -18,6 +18,11 @@ const getBashDefinition = (port: number) => ({
1818
command: `eval "$(curl -sS localhost:${port}/setup)"`
1919
});
2020

21+
const getGitBashDefinition = (port: number) => ({
22+
description: "Git Bash",
23+
command: `eval "$(curl -sS localhost:${port}/gb-setup)"`
24+
});
25+
2126
const getFishDefinition = (port: number) => ({
2227
description: "Fish",
2328
command: `curl -sS localhost:${port}/fish-setup | source`
@@ -31,7 +36,8 @@ const getPowershellDefinition = (port: number) => ({
3136
function getShellCommands(port: number): { [shellName: string]: ShellDefinition } {
3237
if (process.platform === 'win32') {
3338
return {
34-
'Powershell': getPowershellDefinition(port)
39+
'Powershell': getPowershellDefinition(port),
40+
'Git Bash': getGitBashDefinition(port)
3541
}
3642
} else {
3743
return {
@@ -84,6 +90,10 @@ export class ExistingTerminalInterceptor implements Interceptor {
8490
getBashShellScript(server.urlFor('/success'), envVars),
8591
{ "content-type": "text/x-shellscript" }
8692
);
93+
await server.forGet('/gb-setup').thenReply(200,
94+
getGitBashShellScript(server.urlFor('/success'), envVars),
95+
{ "content-type": "text/x-shellscript" }
96+
);
8797
await server.forGet('/fish-setup').thenReply(200,
8898
getFishShellScript(server.urlFor('/success'), envVars),
8999
{ "content-type": "application/x-fish" }

src/interceptors/terminal/terminal-scripts.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ export const getBashShellScript = (callbackUrl: string, env: { [name: string]: s
8888
echo 'HTTP Toolkit interception enabled'
8989
`;
9090

91+
// Build a Bash script, but first just translate the $PATH (which Git Bash expects to include its own
92+
// path-format strings) from Windows to Git Bash format.
93+
export const getGitBashShellScript = (callbackUrl: string, env: { [name: string]: string }) =>
94+
getBashShellScript(callbackUrl, {
95+
...env,
96+
// We convert path from being the override bin & ; separator to /C/path/to/overrides.
97+
PATH: env['PATH']
98+
.replace(
99+
`${OVERRIDE_BIN_PATH};`,
100+
POSIX_OVERRIDE_BIN_PATH + ':' // Unix format $PATH separator
101+
)
102+
});
103+
91104
export const getFishShellScript = (callbackUrl: string, env: { [name: string]: string }) => `${
92105
_.map(env, (value, key) => ` set -x ${key} "${value.replace(/"/g, '\\"')}"`).join('\n')
93106
}

0 commit comments

Comments
 (0)