-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathbeachmsg.js
More file actions
executable file
·80 lines (68 loc) · 2.18 KB
/
beachmsg.js
File metadata and controls
executable file
·80 lines (68 loc) · 2.18 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
73
74
75
76
77
78
79
80
#!/usr/bin/env node
import { connect } from "net";
import os from "os";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const HOME_DIR = os.homedir();
const PROJECT_ROOT = path.dirname(fileURLToPath(import.meta.url));
// if --help/-h, print usage
if (process.argv.includes("--help") || process.argv.includes("-h")) {
console.log(`
Usage: beachmsg <command> [args...]
- Sends a command to the beachpatrol server controlling the browser.
- The provided command must exist in the "commands" directory of beachpatrol.
Options:
--help Show this help message.
--version Show version.
`.trimStart());
process.exit(0);
}
// handle --version/-v
if (process.argv.includes("--version") || process.argv.includes("-v")) {
// get version from package.json
const packageJsonPath = path.join(PROJECT_ROOT, "package.json");
const version = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")).version;
console.log(`v${version}`);
process.exit(0);
}
// 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 commandFilePath = path.join(
PROJECT_ROOT,
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
let endpoint;
if (process.platform !== "win32") {
const DATA_DIR =
process.env.XDG_DATA_HOME || path.join(HOME_DIR, ".local/share");
endpoint = `${DATA_DIR}/beachpatrol/beachpatrol.sock`;
} else {
endpoint = String.raw`\\.\pipe\beachpatrol`;
}
const client = connect(endpoint, () => {
client.write(JSON.stringify([commandName, ...args]));
});
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);
});