-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathenvironments.ts
85 lines (70 loc) · 2.07 KB
/
environments.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import fs from 'fs';
import { ServerOptions } from 'https';
import path from 'path';
import { issueHttpsCertificate } from '../utils/misc';
require('dotenv-flow').config();
const {
NODE_ENV,
HTTP_PORT,
HTTPS_PORT,
CREDENTIALS_ENABLED,
CREDENTIALS_PATH,
CREDENTIALS_CA,
CREDENTIALS_KEY,
CREDENTIALS_CERT,
WEBHOOK_ENABLED,
WEBHOOK_SECRET,
GITHUB_CLIENT_ID,
GITHUB_CLIENT_SECRET,
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
} = process.env as {
[key: string]: string,
};
const isEnabled = (v: string) => v === '1';
const missingVars = [
'NODE_ENV',
'HTTP_PORT',
'HTTPS_PORT',
'CREDENTIALS_ENABLED',
...(isEnabled(CREDENTIALS_ENABLED) ? [
'CREDENTIALS_PATH',
'CREDENTIALS_CA',
'CREDENTIALS_KEY',
'CREDENTIALS_CERT',
] : []),
'WEBHOOK_ENABLED',
...(isEnabled(WEBHOOK_ENABLED) ? [
'WEBHOOK_SECRET',
] : []),
'GITHUB_CLIENT_ID',
'GITHUB_CLIENT_SECRET',
'AWS_ACCESS_KEY_ID',
'AWS_SECRET_ACCESS_KEY',
].filter(variable => process.env[variable] === undefined);
if (missingVars.length) throw new Error(`The following environment variables are missing: ${missingVars.join(', ')}`);
export const __PROD__ = NODE_ENV === 'production';
export const __DEV__ = NODE_ENV === 'development';
export const httpPort = parseInt(HTTP_PORT);
export const httpsPort = parseInt(HTTPS_PORT);
export const webhookOptions = isEnabled(WEBHOOK_ENABLED) ? {
path: '/webhook',
secret: WEBHOOK_SECRET,
} : undefined;
export let credentials: ServerOptions | undefined;
if (isEnabled(CREDENTIALS_ENABLED)) {
if (fs.existsSync(CREDENTIALS_PATH)) {
const readCredentials = (file: string) => fs.readFileSync(path.resolve(CREDENTIALS_PATH, file));
credentials = {
ca: readCredentials(CREDENTIALS_CA),
key: readCredentials(CREDENTIALS_KEY),
cert: readCredentials(CREDENTIALS_CERT),
};
} else {
issueHttpsCertificate();
}
}
export const githubClientId = GITHUB_CLIENT_ID;
export const githubClientSecret = GITHUB_CLIENT_SECRET;
export const awsAccessKeyId = AWS_ACCESS_KEY_ID;
export const awsSecretAccessKey = AWS_SECRET_ACCESS_KEY;