|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const { spawn } = require("child_process"); |
| 4 | +const os = require("os"); |
| 5 | +const { resolveServer } = require("./resolve"); |
| 6 | + |
| 7 | +const argv = process.argv.slice(3); |
| 8 | + |
| 9 | +function parseArgs(argv) { |
| 10 | + const flags = {}; |
| 11 | + for (let i = 0; i < argv.length; i++) { |
| 12 | + const arg = argv[i]; |
| 13 | + if (arg.startsWith("--")) { |
| 14 | + const key = arg.slice(2); |
| 15 | + const next = argv[i + 1]; |
| 16 | + if (next && !next.startsWith("--")) { flags[key] = next; i++; } |
| 17 | + else flags[key] = true; |
| 18 | + } |
| 19 | + } |
| 20 | + return flags; |
| 21 | +} |
| 22 | + |
| 23 | +// Write text to the system clipboard. |
| 24 | +// Falls back to printing the text if the clipboard tool is not found. |
| 25 | +function copyToClipboard(text) { |
| 26 | + let cmd, args; |
| 27 | + if (process.platform === "darwin") { |
| 28 | + cmd = "pbcopy"; args = []; |
| 29 | + } else if (process.platform === "win32") { |
| 30 | + cmd = "clip"; args = []; |
| 31 | + } else { |
| 32 | + cmd = "xclip"; args = ["-sel", "c"]; |
| 33 | + } |
| 34 | + |
| 35 | + const proc = spawn(cmd, args, { stdio: ["pipe", "ignore", "ignore"] }); |
| 36 | + proc.on("error", () => process.stdout.write(text + "\n")); // fallback |
| 37 | + proc.stdin.write(text, "utf-8"); |
| 38 | + proc.stdin.end(); |
| 39 | +} |
| 40 | + |
| 41 | +function truncate(str, len) { |
| 42 | + return str.length > len ? str.slice(0, len) + "…" : str; |
| 43 | +} |
| 44 | + |
| 45 | +function run() { |
| 46 | + const flags = parseArgs(argv); |
| 47 | + |
| 48 | + if (flags.help) { |
| 49 | + console.log("Usage: instbyte watch [--channel <name>] [--server <url>] [--passphrase <pass>] [--output]"); |
| 50 | + process.exit(0); |
| 51 | + } |
| 52 | + |
| 53 | + const { url } = resolveServer(flags); |
| 54 | + const channel = flags.channel || "general"; |
| 55 | + const outputMode = !!flags.output; |
| 56 | + |
| 57 | + // In --output mode, status messages go to stderr so stdout stays clean for piping |
| 58 | + const status = outputMode |
| 59 | + ? (msg) => process.stderr.write(msg + "\n") |
| 60 | + : (msg) => process.stdout.write(msg + "\n"); |
| 61 | + |
| 62 | + const { io } = require("socket.io-client"); |
| 63 | + |
| 64 | + const socket = io(url, { |
| 65 | + reconnection: true, |
| 66 | + reconnectionDelay: 2000, |
| 67 | + reconnectionDelayMax: 10000 |
| 68 | + }); |
| 69 | + |
| 70 | + let connected = false; |
| 71 | + let connectErrShown = false; |
| 72 | + let stopping = false; |
| 73 | + |
| 74 | + socket.on("connect", () => { |
| 75 | + socket.emit("join", os.userInfo().username); |
| 76 | + status(connected |
| 77 | + ? "Reconnected." |
| 78 | + : `Watching ${channel} on ${url}... (Ctrl+C to stop)` |
| 79 | + ); |
| 80 | + connected = true; |
| 81 | + connectErrShown = false; |
| 82 | + }); |
| 83 | + |
| 84 | + socket.on("disconnect", () => { |
| 85 | + if (stopping) return; |
| 86 | + connected = false; |
| 87 | + status("Reconnecting..."); |
| 88 | + }); |
| 89 | + |
| 90 | + socket.on("connect_error", () => { |
| 91 | + if (!connectErrShown) { |
| 92 | + status(`✗ Cannot connect to ${url}`); |
| 93 | + status(` Start a server with: npx instbyte`); |
| 94 | + connectErrShown = true; |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + socket.on("new-item", (item) => { |
| 99 | + if (item.channel !== channel) return; |
| 100 | + |
| 101 | + if (item.type === "file") { |
| 102 | + process.stdout.write(`📎 File: ${url}/uploads/${item.filename}\n`); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + if (item.type === "text") { |
| 107 | + if (outputMode) { |
| 108 | + process.stdout.write(item.content + "\n"); |
| 109 | + } else { |
| 110 | + copyToClipboard(item.content); |
| 111 | + process.stdout.write(`✓ Copied: "${truncate(item.content, 60)}"\n`); |
| 112 | + } |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + process.on("SIGINT", () => { |
| 117 | + stopping = true; |
| 118 | + socket.disconnect(); |
| 119 | + process.stdout.write("\nStopped watching.\n"); |
| 120 | + process.exit(0); |
| 121 | + }); |
| 122 | +} |
| 123 | + |
| 124 | +run(); |
0 commit comments