amaran-light-cli
Version:
Command line tool for controlling Aputure Amaran lights via WebSocket to a local Amaran desktop app.
62 lines • 2.09 kB
JavaScript
import chalk from 'chalk';
const LIGHT_NODE_PATTERN = /^[A-Z0-9]+-[A-Z0-9]+$/i;
export function isLightDevice(device) {
return typeof device.node_id === 'string' && LIGHT_NODE_PATTERN.test(device.node_id);
}
export function getLightDevices(devices) {
return devices.filter(isLightDevice);
}
export function commandCallbackPromise(register) {
return new Promise((resolve, reject) => {
register((success, message) => {
if (!success) {
reject(new Error(message));
return;
}
resolve();
});
});
}
/**
* Adds standard options to a commander command
*/
export function addStandardOptions(command) {
return command
.option('-u, --url <url>', 'WebSocket URL')
.option('-c, --client-id <id>', 'Client ID')
.option('-d, --debug', 'Enable debug mode');
}
/**
* Common pattern for connecting, finding devices, executing an action, and disconnecting
*/
export async function runDeviceAction({ deps, options, deviceQuery, actionName, onSuccess }, action, allAction) {
const { createController, findDevice } = deps;
const controller = await createController(options.url, options.clientId, options.debug);
try {
if (!deviceQuery || deviceQuery.toLowerCase() === 'all') {
await allAction(controller);
return;
}
const device = findDevice(controller, deviceQuery);
if (!device) {
console.error(chalk.red(`Device "${deviceQuery}" not found`));
process.exit(1);
}
if (!device.node_id) {
console.error(chalk.red(`Device "${deviceQuery}" has no node_id`));
process.exit(1);
}
await action(device, controller);
if (onSuccess) {
console.log(chalk.green(onSuccess(device)));
}
}
catch (error) {
console.error(chalk.red(`✗ Failed to ${actionName}: ${error.message}`));
throw error;
}
finally {
await controller.disconnect();
}
}
//# sourceMappingURL=cmdUtils.js.map