@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.18 kB
JavaScript
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();