simple-keep-pc-awake
Advanced tools
Comparing version 1.0.3 to 1.0.4
93
index.js
#!/usr/bin/env node | ||
//@ts-check | ||
// Import nut-js which handles the mouse-moving functionalities | ||
const { mouse, Point } = require("@nut-tree/nut-js"); | ||
// Read the command line arguments | ||
const args = process.argv.slice(2); | ||
const DEFAULTS = { | ||
OFFSET_PX: 1, | ||
INTERVAL_SEC: 30, | ||
}; | ||
//#region time-helpers | ||
/** | ||
@@ -30,9 +40,74 @@ * Representing the digits in a double digit format (0 to 00, 8 to 08, 12 to 12, etc.); | ||
}; | ||
//#endregion | ||
//#region args-parser-helpers | ||
/** | ||
* Extracts argument from the process args with the key provided | ||
* @param {string} param Key of the extracted value | ||
* @param {number} defaultValue Default value to be used if no such value is found. | ||
* @returns | ||
*/ | ||
const getArgParam = (param, defaultValue) => { | ||
// Find the arg index which corresponds to the param provided | ||
const paramTagIndex = args.findIndex((arg) => arg === param); | ||
if (paramTagIndex < 0) return defaultValue; | ||
// Get the next element in the array, so we know the value of the param | ||
const paramValueIndex = paramTagIndex + 1; | ||
// If there is such value after param tag | ||
if (args[paramValueIndex]) { | ||
// Try to parse it to float | ||
const paramValueAsFloat = parseFloat(args[paramValueIndex]); | ||
// If it is not a valid number, return the default value | ||
if (isNaN(paramValueAsFloat)) { | ||
return defaultValue; | ||
} | ||
// Else return the user-configured value | ||
else { | ||
return paramValueAsFloat; | ||
} | ||
} | ||
// Default return, if no arg after --offset is provided | ||
return defaultValue; | ||
}; | ||
/** | ||
* Extracts the offset value from the --offset argument | ||
* @returns {number} Amount of pixels to move the cursor by. User defined or default. | ||
*/ | ||
const getMoveOffset = () => { | ||
return getArgParam("--offset", DEFAULTS.OFFSET_PX); | ||
}; | ||
/** | ||
* Extracts the interval value from the --interval argument | ||
* @returns {number} Amount of MS to wait before moving the cursor again. User defined or default. | ||
*/ | ||
const getMoveInterval = () => { | ||
return getArgParam("--interval", DEFAULTS.INTERVAL_SEC) * 1_000; | ||
}; | ||
/** | ||
* Searches the args for --quiet param and if found should return true to surpass console.logs | ||
*/ | ||
const isQuietMode = args.some((arg) => arg === "--quiet"); | ||
//#endregion | ||
/** | ||
* console.logs the message if quiet mode is off | ||
* @param {string} message | ||
*/ | ||
const log = (message) => (!isQuietMode ? console.log(message) : null); | ||
/** | ||
* Moves the mouse cursor by X amount of pixels | ||
* @param {number} offset How many pixels should the mouse be moved by | ||
* @param {number} interval How many milliseconds to wait before moving the mouse again | ||
*/ | ||
const mouseMover = async (offset) => { | ||
console.log(`\x1b[33m${now()}\x1b[0m Moving mouse cursor by ${offset} pixels`); | ||
const mouseMover = async (offset, interval) => { | ||
log(`\x1b[33m${now()}\x1b[0m Moved mouse cursor by ${offset} pixels`); | ||
@@ -44,7 +119,13 @@ const prevPosition = await mouse.getPosition(); | ||
setTimeout(() => { | ||
mouseMover(offset * -1); | ||
}, 30_000); | ||
mouseMover(offset * -1, interval); | ||
}, interval); | ||
}; | ||
console.log("\x1b[32mStarting mouse mover script...\x1b[0m"); | ||
mouseMover(1); | ||
const moveOffset = getMoveOffset(); | ||
const moveInterval = getMoveInterval(); | ||
log("\x1b[32m 🚀 Started simple-keep-pc-awake...\x1b[0m"); | ||
log(`\x1b[34m INFO \x1b[0m Moving mouse cursor by ${moveOffset} pixels`); | ||
log(`\x1b[34m INFO \x1b[0m Moving mouse cursor every ${moveInterval / 1_000} seconds`); | ||
mouseMover(moveOffset, moveInterval); |
{ | ||
"name": "simple-keep-pc-awake", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "A simple way to keep your PC awake with no configuration and setup.", | ||
@@ -22,3 +22,6 @@ "main": "index.js", | ||
"@nut-tree/nut-js": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^17.0.16" | ||
} | ||
} |
@@ -5,7 +5,9 @@ # Simple Keep PC Awake | ||
# Run via npx | ||
## Run via npx | ||
To run the awake, just open a new terminal and run `npx simple-keep-pc-awake`. This will start the script. When you want to stop it just exit with `Ctrl+C`. | ||
To run the awake, just open a new terminal and run | ||
`npx simple-keep-pc-awake` | ||
This will start the script. When you want to stop it just exit with `Ctrl+C`. | ||
# Running via node | ||
## Running via node | ||
@@ -23,4 +25,24 @@ To keep with node on your pc you have to perform the following steps: | ||
# Running via executable | ||
## Running via executable | ||
To run via built executables, go to [the releases tab in the repo](https://github.com/mutafow/simple-keep-pc-awake/releases) and download the latest version of the binaries for your machine. Then simply run it when you need to keep your PC awake and close it when you don't! | ||
## Options | ||
When running via node or NPX you can set some options: | ||
``` | ||
npx simple-keep-pc-awake [options] | ||
Options: | ||
--quiet quiet mode, does not output any logs (default: false) | ||
--offset by how many pixels should the mouse move (default: 1) | ||
--interval how much time should pass before mouse is moved again, in seconds (default: 30s) | ||
Examples: | ||
npx simple-keep-pc-awake --quiet --offset 5 moves the cursor by 5px each 30 seconds, in quiet mode | ||
npx simple-keep-pc-awake --offset 5 --interval 100 moves the cursor by 5px each 100 seconds | ||
npx simple-keep-pc-awake default behavior, moves the cursor by 1px every 30 seconds | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6311
106
47
1