const fs = require("fs"); const path = "E:/GitHub/blog/bot/index.js"; let c = fs.readFileSync(path, "utf8"); // Insert start/stop/status/ip handlers before CMD_HANDLERS.view var insertAt = c.indexOf("\nCMD_HANDLERS.view = async function"); if (insertAt === -1) { console.log("ERR: view not found"); process.exit(1); } insertAt++; // after the newline var handlers = ` CMD_HANDLERS.start = async function (chatId) { if (writeProcess && writeReady) { var ip = getLanIP(); await sendMessage(chatId, "\u2705 Write is running\n\nDesktop: http://localhost:" + WRITE_PORT + "\nPhone: http://" + ip + ":" + WRITE_PORT); return CMD_HANDLERS.help(chatId); } if (writeProcess && !writeReady) { return sendMessage(chatId, "\u23f3 Write is starting, please wait..."); } var result = startWriteServer(); if (!result.started) return sendMessage(chatId, "\u274c Failed to start"); await sendMessage(chatId, "\u23f3 Starting Write..."); var ready = await waitForReady(WRITE_PORT); if (ready) { writeReady = true; var ip = getLanIP(); await sendMessage(chatId, "\u2705 Write is ready!\n\nDesktop: http://localhost:" + WRITE_PORT + "\nPhone: http://" + ip + ":" + WRITE_PORT); return CMD_HANDLERS.help(chatId); } return sendMessage(chatId, "\u274c Start timed out, check console log."); }, async stop(chatId) { if (!writeProcess) return sendMessage(chatId, "\u23f8 Write is not running."); stopWriteServer(); return sendMessage(chatId, "\u26a1 Write stopped."); }, async status(chatId) { var s = getWriteStatus(); var status = s.running ? "\u2705 Running" : s.starting ? "\u23f3 Starting..." : "\u23f8 Stopped"; var lines = ["Status: " + status]; if (s.running) { lines.push("Local: http://localhost:" + WRITE_PORT); lines.push("Phone: http://" + s.ip + ":" + WRITE_PORT); } return sendMessage(chatId, lines.join("\n")); }, async ip(chatId) { var ip = getLanIP(); return sendMessage(chatId, "Phone: http://" + ip + ":" + WRITE_PORT + " (" + PREFER_IFACE + ")"); }, `; c = c.substring(0, insertAt) + handlers + c.substring(insertAt); fs.writeFileSync(path, c, "utf8"); console.log("start/stop/status/ip restored");