jiggle-genius
Version:
Jiggle Genius is a simple and efficient mouse jiggler CLI tool designed to keep your computer awake during those times when you need it to stay active. Whether you're preventing your screen from locking during a presentation or keeping your online status
142 lines (141 loc) • 5.07 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.jiggleGenius = exports.calculateMousePosition = exports.validateConfig = exports.DEFAULT_CONFIG = exports.JiggleError = void 0;
const robotjs_1 = __importDefault(require("robotjs"));
const yargs_1 = __importDefault(require("yargs"));
const helpers_1 = require("yargs/helpers");
/**
* Error class for jiggle-specific validation errors
*/
class JiggleError extends Error {
constructor(message) {
super(message);
this.name = 'JiggleError';
}
}
exports.JiggleError = JiggleError;
/**
* Default configuration values
*/
exports.DEFAULT_CONFIG = {
duration: 30,
radius: 10,
speed: 2,
};
/**
* Validates the input parameters for the jiggle function
* @param config - The configuration object to validate
* @throws {JiggleError} If any parameters are invalid
*/
function validateConfig(config) {
if (typeof config.duration !== 'number' || config.duration <= 0) {
throw new JiggleError('Duration must be a positive number');
}
if (typeof config.radius !== 'number' || config.radius <= 0) {
throw new JiggleError('Radius must be a positive number');
}
if (typeof config.speed !== 'number' || config.speed <= 0 || config.speed > 10) {
throw new JiggleError('Speed must be a number between 1 and 10');
}
}
exports.validateConfig = validateConfig;
/**
* Calculates the next mouse position based on the current angle and configuration
*/
function calculateMousePosition(centerX, centerY, angle, radius) {
return {
x: Math.round(centerX + radius * Math.cos(angle)),
y: Math.round(centerY + radius * Math.sin(angle)),
};
}
exports.calculateMousePosition = calculateMousePosition;
/**
* Logs a message to the console
* @param message - The message to log
*/
function log(message) {
console.info(message);
}
/**
* Moves the mouse in a circular pattern to prevent screen timeout
* @param config - Configuration object containing duration, radius, and speed parameters
* @returns Promise that resolves when the jiggling is complete
*/
async function jiggleGenius(config) {
try {
// Parse command line arguments
const argv = await (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
.options({
duration: {
alias: 'd',
description: 'Duration in minutes',
type: 'number',
default: exports.DEFAULT_CONFIG.duration,
},
radius: {
alias: 'r',
description: 'Radius of the circular movement in pixels',
type: 'number',
default: exports.DEFAULT_CONFIG.radius,
},
speed: {
alias: 's',
description: 'Movement speed (1-10)',
type: 'number',
default: exports.DEFAULT_CONFIG.speed,
},
})
.help()
.alias('help', 'h')
.version()
.argv;
const jiggleConfig = {
duration: config?.duration ?? argv.duration,
radius: config?.radius ?? argv.radius,
speed: config?.speed ?? argv.speed,
};
validateConfig(jiggleConfig);
const startTime = new Date();
const endTime = new Date(startTime.getTime() + jiggleConfig.duration * 60000);
// Configure robot
robotjs_1.default.setMouseDelay(2);
const screenSize = robotjs_1.default.getScreenSize();
const centerX = screenSize.width / 2;
const centerY = screenSize.height / 2;
log('Starting Jiggle Genius...');
log(`Duration: ${jiggleConfig.duration} minutes`);
log(`End Time: ${endTime.toLocaleTimeString()}`);
log('Press Ctrl+C to stop\n');
let angle = 0;
const intervalId = setInterval(() => {
const position = calculateMousePosition(centerX, centerY, angle, jiggleConfig.radius);
robotjs_1.default.moveMouse(position.x, position.y);
angle += (Math.PI / 180) * jiggleConfig.speed;
if (Date.now() >= endTime.getTime()) {
clearInterval(intervalId);
log('\nJiggle session completed!');
process.exit(0);
}
}, 50);
// Handle graceful shutdown
process.on('SIGINT', () => {
clearInterval(intervalId);
log('\nJiggle session stopped by user');
process.exit(0);
});
}
catch (error) {
console.error('Error:', error instanceof Error ? error.message : 'An unknown error occurred');
process.exit(1);
}
}
exports.jiggleGenius = jiggleGenius;
exports.default = jiggleGenius;
// Run the function if this module is executed directly
if (require.main === module) {
void jiggleGenius();
}