UNPKG

@0xdaniiel/keepawake

Version:

Prevents your Node.js process or online dev session from going to sleep by running a persistent heartbeat.

88 lines (74 loc) • 2.27 kB
#!/usr/bin/env node import http from "http"; import https from "https"; async function main() { const args = process.argv.slice(2); const urlArg = args.find((arg) => arg.startsWith("http")); const isLocal = args.includes("--local"); const isOnce = args.includes("--once"); const intervalArg = args.find((arg) => arg.startsWith("--interval=")); const interval = intervalArg ? parseInt(intervalArg.split("=")[1], 10) * 1000 : 5 * 60 * 1000; // 5 min default // āœ… Help & Version const showHelp = args.includes("--help") || args.includes("-h"); const showVersion = args.includes("--version") || args.includes("-v"); if (showHelp) { console.log(` šŸ“¦ keepawake - Prevent apps from sleeping Usage: keepawake <url> [options] Options: --interval=<seconds> Set ping interval (default: 300) --once Send a single ping and exit --local Keep local process awake with log pings -h, --help Show help message -v, --version Show version info `); return; } if (showVersion) { console.log("keepawake v1.0.0"); return; } // šŸ›”ļø Local mode if (isLocal) { console.log( "šŸ›”ļø Keepawake local mode activated — keeping process alive.\n" ); setInterval(() => { console.log("ā³ Still awake..."); }, interval); return; } // āŒ No URL if (!urlArg) { console.error("āŒ Please provide a URL to ping."); console.log( "Usage: keepawake <url> [--once] [--interval=seconds] [--local]" ); process.exit(1); } const lib = urlArg.startsWith("https") ? https : http; // šŸ“” Ping logic function sendPing() { lib .get(urlArg, (res) => { console.log(`āœ… Pinged ${urlArg} — Status: ${res.statusCode}`); }) .on("error", (err) => { console.error(`āŒ Failed to ping ${urlArg}: ${err.message}`); }); } if (isOnce) { console.log(`šŸ“” Sending one ping to: ${urlArg}`); sendPing(); return; } console.log( `šŸ“” Starting keepawake pings to: ${urlArg} every ${interval / 1000}s` ); sendPing(); setInterval(sendPing, interval); } main();