-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjam-server.mjs
executable file
·96 lines (81 loc) · 1.97 KB
/
jam-server.mjs
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
#!/usr/bin/env node
// Entrypoint
import arg from 'arg';
import server from './src/server/index.mjs';
import fs from 'fs';
import fetch from 'node-fetch';
import Tail from 'tail-file';
import { enable as enableDiagnostics } from './src/diagnostics.js';
enableDiagnostics();
const tail = async (port = 9000) => {
const config = await (
await fetch(`http://localhost:${port}/api/config`)
).json();
if (!fs.existsSync(config.logLocation)) {
console.log(`Could not locate ${config.logLocation}.`);
return;
}
console.log(`Tailing ${config.logLocation}`);
new Tail(config.logLocation, console.log);
};
const shutdown = (port = 9000) => {
return fetch(`http://localhost:${port}/shutdown`).then((res) => res.text());
};
const ping = (port = 9000) => {
return fetch(`http://localhost:${port}`);
};
const run = async (argv) => {
console.log('📻 Jambox Server 📻');
const [subCommand] = argv.slice(2);
if (subCommand === 'shutdown') {
const port = 9000;
console.log(`Attempt a shutdown at localhost:${port}...`);
await shutdown(port)
.then((res) => {
console.log(res);
process.exit(0);
})
.catch((e) => {
console.error(e);
process.exit(1);
});
return;
} else if (subCommand === 'ping') {
const port = 9000;
console.log(`Attempt a ping to localhost:${port}...`);
await ping(port)
.then(() => {
console.log('pong');
process.exit(0);
})
.catch((e) => {
console.error(e);
process.exit(1);
});
return;
} else if (subCommand === 'tail') {
await tail(9000)
.then()
.catch((e) => {
console.error(e);
process.exit(1);
});
return;
}
const options = arg(
{
'--port': Number,
'-p': '--port',
},
argv
);
server({
port: options['--port'] || 9000,
})
.then()
.catch((e) => {
console.log(e);
process.exit(1);
});
};
run(process.argv);