-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtual-host-proxy.js
executable file
·149 lines (131 loc) · 3.52 KB
/
virtual-host-proxy.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env node
const daemonize = require('daemonize2');
const { configDir } = require('./config');
const { mkdirSync, writeFileSync, unlinkSync } = require('fs');
const AutoLaunch = require('auto-launch');
const { join, basename } = require('path');
const { ensureResolversConfig } = require('./config');
mkdirSync(configDir, { recursive: true });
const daemon = daemonize.setup({
main: 'daemon.js',
name: 'virtual-host-proxy',
pidfile: configDir + '/.pid',
silent: true,
});
daemon
.on('starting', function () {
console.log('Starting daemon...');
})
.on('started', function (pid) {
console.log(`Daemon started (PID: ${pid})`);
})
.on('stopping', function () {
console.log('Stopping daemon...');
})
.on('stopped', function (pid) {
console.log('Daemon stopped');
})
.on('running', function (pid) {
console.log(`Daemon already running (PID: ${pid})`);
})
.on('notrunning', function () {
console.log('Daemon is not running');
})
.on('error', function (err) {
console.log('Daemon failed to start: ' + err.message);
});
const autolauncher = new AutoLaunch({
name: 'virtual-host-proxy',
path: process.argv[1],
mac: { useLaunchAgent: true },
});
switch (process.argv[2]) {
case 'init':
ensureResolversConfig(rootDomain).then(() => {
console.log('Initialized');
});
break;
case 'autolaunch':
switch (process.argv[3]) {
case 'enable':
autolauncher.enable().then(
() => console.log('Enabled'),
(err) =>
console.error(`Could not enable auto launch: ${err.message}`),
);
break;
case 'disable':
autolauncher.disable().then(
() => console.log('Disabled'),
(err) =>
console.error(`Could not disable auto launch: ${err.message}`),
);
break;
case undefined:
case 'status':
autolauncher.isEnabled().then(
(enabled) => console.log(enabled ? 'Enabled' : 'Disabled'),
(err) =>
console.error(
`Could not retrieve auto launch status: ${err.message}`,
),
);
break;
default:
console.log(
'Usage: virtual-host-proxy autolaunch [enable|disable|status]',
);
}
break;
case 'start':
daemon.start().once('started', () => {
process.exit();
});
break;
case 'stop':
daemon.stop();
break;
case 'kill':
daemon.kill();
break;
case 'restart':
if (daemon.status()) {
daemon.stop().once('stopped', () => {
daemon.start().once('started', () => {
process.exit();
});
});
} else {
daemon.start().once('started', () => {
process.exit();
});
}
break;
case 'status':
const pid = daemon.status();
if (pid) console.log(`Running (PID: ${pid})`);
else console.log('Stopped');
break;
case undefined:
case 'run':
require('./daemon');
break;
case 'add':
if (!process.argv[3] || isNaN(parseInt(process.argv[4]))) {
console.log('Usage virtual-host-proxy add <domain> <port>');
} else {
writeFileSync(join(configDir, process.argv[3]), process.argv[4]);
}
break;
case 'remove':
if (!process.argv[3]) {
console.log('Usage virtual-host-proxy remove <domain>');
} else {
unlinkSync(join(configDir, basename(process.argv[3])));
}
break;
default:
console.log(
'Usage: virtual-host-proxy [init|autolaunch|run|start|stop|kill|restart|reload|status|add|remove]',
);
}