@@ -4,13 +4,20 @@ import { Mockttp, getLocal } from 'mockttp';
44import { Interceptor } from '..' ;
55import { HtkConfig } from '../../config' ;
66import { getTerminalEnvVars } from './terminal-env-overrides' ;
7- import { getShellScript } from './terminal-scripts' ;
7+ import { getBashShellScript , getFishShellScript } from './terminal-scripts' ;
88
99interface ServerState {
1010 server : Mockttp ;
1111 isActive : boolean ;
1212}
1313
14+ function getShellCommands ( port : number ) {
15+ return {
16+ 'Bash' : { command : `eval "$(curl -sS localhost:${ port } /setup)"` , description : "Bash-compatible" } ,
17+ 'Fish' : { command : `curl -sS localhost:${ port } /fish-setup | source` , description : "Fish" }
18+ }
19+ }
20+
1421export class ExistingTerminalInterceptor implements Interceptor {
1522
1623 private servers : {
@@ -29,40 +36,46 @@ export class ExistingTerminalInterceptor implements Interceptor {
2936 }
3037
3138 isActive ( proxyPort : number ) : boolean {
32- const serverState = this . servers [ proxyPort ] ;
33- return ! ! serverState && serverState . isActive ;
39+ return this . servers [ proxyPort ] ?. isActive ?? false ;
3440 }
3541
36- async activate ( proxyPort : number ) : Promise < { port : number } > {
42+ async activate ( proxyPort : number ) : Promise < { port : number , commands : { [ shellName : string ] : { command : string , description : string } } } > {
3743 if ( this . servers [ proxyPort ] ) {
3844 // Reset isActive, so we wait again for a new request
3945 this . servers [ proxyPort ] . isActive = false ;
40- return { port : this . servers [ proxyPort ] . server . port } ;
46+ const serverPort = this . servers [ proxyPort ] . server . port ;
47+ return {
48+ port : serverPort ,
49+ commands : getShellCommands ( serverPort )
50+ } ;
4151 }
4252
4353 const server = getLocal ( ) ;
4454 await server . start ( { startPort : proxyPort + 1 , endPort : 65535 } ) ;
4555
4656 const envVars = getTerminalEnvVars ( proxyPort , this . config . https , 'runtime-inherit' , { } ) ;
47- const setupScript = getShellScript ( server . urlFor ( '/success' ) , envVars ) ;
4857
4958 const serverState = { server, isActive : false } ;
5059
51- await server . get ( '/setup' ) . thenCallback ( ( ) => {
52- return {
53- status : 200 ,
54- headers : { "content-type" : "text/x-shellscript" } ,
55- body : setupScript
56- } ;
57- } ) ;
60+ await server . forGet ( '/setup' ) . thenReply ( 200 ,
61+ getBashShellScript ( server . urlFor ( '/success' ) , envVars ) ,
62+ { "content-type" : "text/x-shellscript" }
63+ ) ;
64+ await server . forGet ( '/fish-setup' ) . thenReply ( 200 ,
65+ getFishShellScript ( server . urlFor ( '/success' ) , envVars ) ,
66+ { "content-type" : "application/x-fish" }
67+ ) ;
5868
59- await server . post ( '/success' ) . thenCallback ( ( ) => {
69+ await server . forPost ( '/success' ) . thenCallback ( ( ) => {
6070 serverState . isActive = true ;
6171 return { status : 200 } ;
6272 } ) ;
6373
6474 this . servers [ proxyPort ] = serverState ;
65- return { port : server . port } ;
75+ return {
76+ port : server . port ,
77+ commands : getShellCommands ( server . port )
78+ } ;
6679 }
6780
6881 async deactivate ( proxyPort : number ) : Promise < void > {
0 commit comments