1
1
import * as _ from 'lodash' ;
2
- import { spawn , ChildProcess } from 'child_process' ;
2
+ import * as fs from 'fs' ;
3
+ import * as util from 'util' ;
4
+ import { spawn , ChildProcess , SpawnOptions } from 'child_process' ;
3
5
import * as GSettings from 'node-gsettings-wrapper' ;
4
6
import * as commandExists from 'command-exists' ;
5
7
6
8
import { Interceptor } from '.' ;
7
9
import { HtkConfig } from '../config' ;
8
10
9
- const getTerminalCommand = _ . memoize ( async ( ) : Promise < string | null > => {
11
+ const checkAccess = util . promisify ( fs . access ) ;
12
+ const canAccess = ( path : string ) => checkAccess ( path ) . then ( ( ) => true ) . catch ( ( ) => false ) ;
13
+
14
+ const DEFAULT_GIT_BASH_PATH = 'C:/Program Files/git/git-bash.exe' ;
15
+
16
+ interface SpawnArgs {
17
+ command : string ;
18
+ args ?: string [ ] ;
19
+ options ?: SpawnOptions ;
20
+ }
21
+
22
+ const getTerminalCommand = _ . memoize ( async ( ) : Promise < SpawnArgs | null > => {
10
23
if ( process . platform === 'win32' ) {
11
- return null ; // Coming soon
24
+ if ( await commandExists ( 'git-bash' ) . catch ( ( ) => false ) ) {
25
+ return { command : 'git-bash' } ;
26
+ } else if ( await canAccess ( DEFAULT_GIT_BASH_PATH ) ) {
27
+ return { command : DEFAULT_GIT_BASH_PATH } ;
28
+ } else {
29
+ return { command : 'start' , args : [ 'cmd' ] , options : { shell : true } } ;
30
+ }
12
31
} else if ( process . platform === 'linux' ) {
13
32
if ( GSettings . isAvailable ( ) ) {
14
- const defaultTerminal = GSettings . Key . findById ( 'org.gnome.desktop.default-applications.terminal' , 'exec' ) . getValue ( ) ;
33
+ const defaultTerminal = GSettings . Key . findById (
34
+ 'org.gnome.desktop.default-applications.terminal' , 'exec'
35
+ ) . getValue ( ) ;
15
36
16
- if ( defaultTerminal ) return defaultTerminal ;
37
+ if ( defaultTerminal ) return { command : defaultTerminal } ;
17
38
} else if ( await commandExists ( 'xterm' ) . catch ( ( ) => false ) ) {
18
- return 'xterm' ;
39
+ return { command : 'xterm' } ;
19
40
}
20
41
} else if ( process . platform === 'darwin' ) {
21
42
return null ; // Coming soon
@@ -42,10 +63,12 @@ export class TerminalInterceptor implements Interceptor {
42
63
}
43
64
44
65
async activate ( proxyPort : number ) : Promise < void > {
45
- const terminalCommand = await getTerminalCommand ( ) ;
46
- if ( ! terminalCommand ) throw new Error ( 'Could not find a suitable terminal' ) ;
66
+ const terminalSpawnArgs = await getTerminalCommand ( ) ;
67
+ if ( ! terminalSpawnArgs ) throw new Error ( 'Could not find a suitable terminal' ) ;
68
+
69
+ const { command, args, options } = terminalSpawnArgs ;
47
70
48
- const childProc = spawn ( terminalCommand , [ ] , {
71
+ const childProc = spawn ( command , args || [ ] , _ . assign ( options || { } , {
49
72
env : _ . assign ( {
50
73
'http_proxy' : `http://localhost:${ proxyPort } ` ,
51
74
'HTTP_PROXY' : `http://localhost:${ proxyPort } ` ,
@@ -64,7 +87,7 @@ export class TerminalInterceptor implements Interceptor {
64
87
'GIT_SSL_CAINFO' : this . config . https . certPath
65
88
} , process . env ) ,
66
89
cwd : process . env . HOME || process . env . USERPROFILE
67
- } ) ;
90
+ } ) ) ;
68
91
69
92
terminals [ proxyPort ] = ( terminals [ proxyPort ] || [ ] ) . concat ( childProc ) ;
70
93
0 commit comments