Skip to content

Commit 86957a1

Browse files
committed
Extact terminal env vars into a separate file
1 parent 6489008 commit 86957a1

File tree

3 files changed

+58
-44
lines changed

3 files changed

+58
-44
lines changed

src/interceptors/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { HtkConfig } from '../config';
44

55
import { FreshChrome } from './fresh-chrome';
66
import { FreshFirefox } from './fresh-firefox';
7-
import { TerminalInterceptor } from './fresh-terminal';
7+
import { TerminalInterceptor } from './terminal/fresh-terminal-interceptor';
88
import { addShutdownHandler } from '../shutdown';
99

1010
export interface Interceptor {

src/interceptors/fresh-terminal.ts renamed to src/interceptors/terminal/fresh-terminal-interceptor.ts

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,23 @@ import * as ensureCommandExists from 'command-exists';
1010
import findOsxExecutableCb = require('osx-find-executable');
1111
const findOsxExecutable = util.promisify(findOsxExecutableCb);
1212

13-
import { Interceptor } from '.';
14-
import { HtkConfig } from '../config';
15-
import { reportError } from '../error-tracking';
13+
import { Interceptor } from '..';
14+
import { HtkConfig } from '../../config';
15+
import { reportError } from '../../error-tracking';
16+
import { OVERRIDE_BIN_PATH, getTerminalEnvVars } from './terminal-env-overrides';
1617

1718
const checkAccess = util.promisify(fs.access);
1819
const canAccess = (path: string) => checkAccess(path).then(() => true).catch(() => false);
1920

2021
const commandExists = (path: string): Promise<boolean> => ensureCommandExists(path).then(() => true).catch(() => false);
2122

2223
const DEFAULT_GIT_BASH_PATH = 'C:/Program Files/git/git-bash.exe';
23-
const PATH_VAR_SEPARATOR = process.platform === 'win32' ? ';' : ':';
2424
const SHELL = (process.env.SHELL || '').split('/').slice(-1)[0];
2525

26-
const OVERRIDES_DIR = path.join(__dirname, '..', '..', 'overrides');
27-
28-
const OVERRIDE_BIN_PATH = path.join(OVERRIDES_DIR, 'path');
2926
// Generate POSIX paths for git-bash on Windows (or use the normal path everywhere where)
3027
const POSIX_OVERRIDE_BIN_PATH = process.platform === 'win32'
3128
? OVERRIDE_BIN_PATH.replace(/\\/g, '/').replace(/^(\w+):/, (_all, driveLetter) => `/${driveLetter.toLowerCase()}`)
3229
: OVERRIDE_BIN_PATH;
33-
const OVERRIDE_RUBYGEMS_PATH = path.join(OVERRIDES_DIR, 'gems');
3430

3531
interface SpawnArgs {
3632
command: string;
@@ -365,41 +361,7 @@ export class TerminalInterceptor implements Interceptor {
365361
command,
366362
(args || []),
367363
_.assign(options || {}, {
368-
env: _.assign({}, process.env, {
369-
'http_proxy': `http://localhost:${proxyPort}`,
370-
'HTTP_PROXY': `http://localhost:${proxyPort}`,
371-
'https_proxy': `http://localhost:${proxyPort}`,
372-
'HTTPS_PROXY': `http://localhost:${proxyPort}`,
373-
// Used by global-agent to configure node.js HTTP(S) defaults
374-
'GLOBAL_AGENT_HTTP_PROXY': `http://localhost:${proxyPort}`,
375-
// Used by some CGI engines to avoid 'httpoxy' vulnerability
376-
'CGI_HTTP_PROXY': `http://localhost:${proxyPort}`,
377-
// Used by npm, for versions that don't support HTTP_PROXY etc
378-
'npm_config_proxy': `http://localhost:${proxyPort}`,
379-
'npm_config_https_proxy': `http://localhost:${proxyPort}`,
380-
// Stop npm warning about having a different 'node' in $PATH
381-
'npm_config_scripts_prepend_node_path': 'false',
382-
383-
// Trust cert when using OpenSSL with default settings
384-
'SSL_CERT_FILE': this.config.https.certPath,
385-
// Trust cert when using Node 7.3.0+
386-
'NODE_EXTRA_CA_CERTS': this.config.https.certPath,
387-
// Trust cert when using Requests (Python)
388-
'REQUESTS_CA_BUNDLE': this.config.https.certPath,
389-
// Trust cert when using Perl LWP
390-
'PERL_LWP_SSL_CA_FILE': this.config.https.certPath,
391-
// Trust cert for HTTPS requests from Git
392-
'GIT_SSL_CAINFO': this.config.https.certPath,
393-
394-
'HTTP_TOOLKIT_ACTIVE': 'true',
395-
396-
// Prepend our bin overrides into $PATH
397-
'PATH': `${OVERRIDE_BIN_PATH}${PATH_VAR_SEPARATOR}${process.env.PATH}`,
398-
// Prepend our Ruby gem overrides into $LOAD_PATH
399-
'RUBYLIB': process.env.RUBYLIB
400-
? `${OVERRIDE_RUBYGEMS_PATH}:${process.env.RUBYLIB}`
401-
: OVERRIDE_RUBYGEMS_PATH
402-
}),
364+
env: _.assign({}, process.env, getTerminalEnvVars(proxyPort, this.config.https)),
403365
cwd: process.env.HOME || process.env.USERPROFILE
404366
}));
405367

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import * as path from 'path';
2+
3+
import { HttpsPathOptions } from 'mockttp/dist/util/tls';
4+
5+
const PATH_VAR_SEPARATOR = process.platform === 'win32' ? ';' : ':';
6+
7+
const OVERRIDES_DIR = path.join(__dirname, '..', '..', 'overrides');
8+
const OVERRIDE_RUBYGEMS_PATH = path.join(OVERRIDES_DIR, 'gems');
9+
10+
export const OVERRIDE_BIN_PATH = path.join(OVERRIDES_DIR, 'path');
11+
12+
export function getTerminalEnvVars(
13+
proxyPort: number,
14+
httpsConfig: HttpsPathOptions
15+
): { [key: string]: string } {
16+
return {
17+
'http_proxy': `http://localhost:${proxyPort}`,
18+
'HTTP_PROXY': `http://localhost:${proxyPort}`,
19+
'https_proxy': `http://localhost:${proxyPort}`,
20+
'HTTPS_PROXY': `http://localhost:${proxyPort}`,
21+
// Used by global-agent to configure node.js HTTP(S) defaults
22+
'GLOBAL_AGENT_HTTP_PROXY': `http://localhost:${proxyPort}`,
23+
// Used by some CGI engines to avoid 'httpoxy' vulnerability
24+
'CGI_HTTP_PROXY': `http://localhost:${proxyPort}`,
25+
// Used by npm, for versions that don't support HTTP_PROXY etc
26+
'npm_config_proxy': `http://localhost:${proxyPort}`,
27+
'npm_config_https_proxy': `http://localhost:${proxyPort}`,
28+
// Stop npm warning about having a different 'node' in $PATH
29+
'npm_config_scripts_prepend_node_path': 'false',
30+
31+
// Trust cert when using OpenSSL with default settings
32+
'SSL_CERT_FILE': httpsConfig.certPath,
33+
// Trust cert when using Node 7.3.0+
34+
'NODE_EXTRA_CA_CERTS': httpsConfig.certPath,
35+
// Trust cert when using Requests (Python)
36+
'REQUESTS_CA_BUNDLE': httpsConfig.certPath,
37+
// Trust cert when using Perl LWP
38+
'PERL_LWP_SSL_CA_FILE': httpsConfig.certPath,
39+
// Trust cert for HTTPS requests from Git
40+
'GIT_SSL_CAINFO': httpsConfig.certPath,
41+
42+
// Flag used by subprocesses to check they're running in an intercepted env
43+
'HTTP_TOOLKIT_ACTIVE': 'true',
44+
45+
// Prepend our bin overrides into $PATH
46+
'PATH': `${OVERRIDE_BIN_PATH}${PATH_VAR_SEPARATOR}${process.env.PATH}`,
47+
// Prepend our Ruby gem overrides into $LOAD_PATH
48+
'RUBYLIB': process.env.RUBYLIB
49+
? `${OVERRIDE_RUBYGEMS_PATH}:${process.env.RUBYLIB}`
50+
: OVERRIDE_RUBYGEMS_PATH
51+
};
52+
}

0 commit comments

Comments
 (0)