Skip to content

Commit d852acc

Browse files
committed
Add Windows support to the terminal interceptor
1 parent 25f39de commit d852acc

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

src/interceptors/fresh-terminal.ts

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
11
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';
35
import * as GSettings from 'node-gsettings-wrapper';
46
import * as commandExists from 'command-exists';
57

68
import { Interceptor } from '.';
79
import { HtkConfig } from '../config';
810

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> => {
1023
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+
}
1231
} else if (process.platform === 'linux') {
1332
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();
1536

16-
if (defaultTerminal) return defaultTerminal;
37+
if (defaultTerminal) return { command: defaultTerminal };
1738
} else if (await commandExists('xterm').catch(() => false)) {
18-
return 'xterm';
39+
return { command: 'xterm' };
1940
}
2041
} else if (process.platform === 'darwin') {
2142
return null; // Coming soon
@@ -42,10 +63,12 @@ export class TerminalInterceptor implements Interceptor {
4263
}
4364

4465
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;
4770

48-
const childProc = spawn(terminalCommand, [], {
71+
const childProc = spawn(command, args || [], _.assign(options || {}, {
4972
env: _.assign({
5073
'http_proxy': `http://localhost:${proxyPort}`,
5174
'HTTP_PROXY': `http://localhost:${proxyPort}`,
@@ -64,7 +87,7 @@ export class TerminalInterceptor implements Interceptor {
6487
'GIT_SSL_CAINFO': this.config.https.certPath
6588
}, process.env),
6689
cwd: process.env.HOME || process.env.USERPROFILE
67-
});
90+
}));
6891

6992
terminals[proxyPort] = (terminals[proxyPort] || []).concat(childProc);
7093

0 commit comments

Comments
 (0)