-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsmtp4dev.ts
More file actions
72 lines (66 loc) · 2.04 KB
/
smtp4dev.ts
File metadata and controls
72 lines (66 loc) · 2.04 KB
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
import { dockerCreateNetwork, dockerRun, dockerStop, dockerRm } from './index';
import {
isEnvironmentMode,
getEnvironmentSlug,
registerServiceWithTraefik,
deregisterServiceFromTraefik,
} from '../helpers/environment-config';
import { execSync } from 'child_process';
interface Options {
mailClientPort?: number;
traefikServiceName?: string;
}
let _smtpServiceName = 'smtp';
function smtpContainerName(): string {
if (isEnvironmentMode()) {
return `boxel-${_smtpServiceName}-${getEnvironmentSlug()}`;
}
return 'boxel-smtp';
}
export async function smtpStart(opts?: Options) {
if (opts?.traefikServiceName) {
_smtpServiceName = opts.traefikServiceName;
}
let containerName = smtpContainerName();
try {
await smtpStop();
} catch (e: any) {
if (!e.message.includes('No such container')) {
throw e;
}
}
let envMode = isEnvironmentMode();
let mailClientPort = envMode
? 0
: (opts?.mailClientPort ?? parseInt(process.env.SMTP_PORT || '5001', 10));
let portMapping = envMode ? '0:80' : `${mailClientPort}:80`;
await dockerCreateNetwork({ networkName: 'boxel' });
const containerId = await dockerRun({
image: 'rnwood/smtp4dev:v3.1',
containerName,
dockerParams: ['-p', portMapping, '--network=boxel'],
});
if (envMode) {
let portOutput = execSync(`docker port ${containerId} 80/tcp`, {
encoding: 'utf-8',
}).trim();
let hostPort = parseInt(portOutput.split('\n')[0].split(':').pop()!, 10);
registerServiceWithTraefik(_smtpServiceName, hostPort);
console.log(
`Started smtp4dev with id ${containerId} on dynamic port ${hostPort} (Traefik).`,
);
} else {
console.log(
`Started smtp4dev with id ${containerId} mapped to host port ${mailClientPort}.`,
);
}
return containerId;
}
export async function smtpStop() {
let containerName = smtpContainerName();
if (isEnvironmentMode()) {
deregisterServiceFromTraefik(_smtpServiceName);
}
await dockerStop({ containerId: containerName });
await dockerRm({ containerId: containerName });
}