-
Notifications
You must be signed in to change notification settings - Fork 1
/
beachmsg.js
executable file
·41 lines (33 loc) · 1.13 KB
/
beachmsg.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
#!/usr/bin/env node
import { connect } from 'net';
import fs from 'fs';
import path from 'path';
import { URL } from 'url';
const SOCKET_PATH = '/tmp/beachpatrol.sock';
// if there are no arguments, bail out
if (process.argv.length < 3) {
console.error('Error: No command specified.');
process.exit(1);
}
const [,, commandName, ...args] = process.argv;
// Check if command script exists
const COMMANDS_DIR = 'commands';
const projectRoot = new URL('.', import.meta.url).pathname;
const commandFilePath = path.join(projectRoot, COMMANDS_DIR, `${commandName}.js`);
if (!fs.existsSync(commandFilePath)) {
console.error(`Error: Command script ${commandName}.js does not exist.`);
process.exit(1);
}
// Send command and args
const client = connect(SOCKET_PATH, () => {
client.write([commandName, ...args].join(' '));
});
client.on('data', (data) => {
process.stdout.write(data.toString());
client.end(); // End connection after response is received
});
client.on('error', (err) => {
console.error(`Error: Could not connect to the beachpatrol socket. ${err.message}`);
console.log('Have you started beachpatrol?');
process.exit(1);
});