From 20efca9234f8696be83e4941d398a3c6e8173c89 Mon Sep 17 00:00:00 2001 From: velvettear Date: Thu, 3 Mar 2022 21:18:07 +0100 Subject: [PATCH] fixed cli method '-ls / --list' --- blinky.js | 8 ++++++-- libs/cli.js | 6 ++++-- libs/constants.js | 4 +++- libs/controller.js | 7 ++----- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/blinky.js b/blinky.js index 7c1c634..fb9ae95 100644 --- a/blinky.js +++ b/blinky.js @@ -4,6 +4,7 @@ const cli = require('./libs/cli.js'); const server = require('./libs/server.js'); const controller = require('./libs/controller.js'); const packageJSON = require('./package'); +const constants = require('./libs/constants.js'); const INTERRUPTS = ['SIGINT', 'SIGTERM']; @@ -23,7 +24,7 @@ async function main() { logger.initialize(); logger.info(global.appName + ' ' + global.appVersion + ' starting...'); if (await cli.handleArguments()) { - exit(); + util.exit(); } await controller.findBlinkstick(); logger.info(await server.start()); @@ -32,7 +33,10 @@ async function main() { async function setGlobalConfig() { let config = __dirname + '/config.json'; - let index = process.argv.indexOf(cli.ARG_CONFIG); + let index = process.argv.indexOf(constants.ARG_CONFIG); + if (index === -1) { + index = process.argv.indexOf(constants.ARG_CONFIG_SHORT); + } if (index >= 0 && process.argv.length >= index + 1) { config = process.argv[index + 1]; } diff --git a/libs/cli.js b/libs/cli.js index 5f9fa72..22de507 100644 --- a/libs/cli.js +++ b/libs/cli.js @@ -7,9 +7,11 @@ async function handleArguments() { for (let index = 0; index < process.argv.length; index++) { switch (process.argv[index]) { case constants.ARG_CONFIG: + case constants.ARG_CONFIG_SHORT: continue; - case constants.ARG_GET_SERIALS: - logger.info('blinksticks: ' + JSON.stringify(await controller.findBlinkstick(constants.ALL))); + case constants.ARG_LIST: + case constants.ARG_LIST_SHORT: + logger.info('blinksticks: ' + JSON.stringify(await controller.findBlinkstick(constants.ALL, true))); handled++; break; } diff --git a/libs/constants.js b/libs/constants.js index ab8b18c..69e0e00 100644 --- a/libs/constants.js +++ b/libs/constants.js @@ -9,7 +9,9 @@ module.exports = { MODE_POWEROFF: 'poweroff', ARG_CONFIG: '--config', - ARG_GET_SERIALS: '--get-blinksticks', + ARG_CONFIG_SHORT: '-c', + ARG_LIST: '--list', + ARG_LIST_SHORT: '-ls', DURATION_DEFAULT: require('../config.json').api.post.duration.default || 1000, DELAY_DEFAULT: require('../config.json').api.post.delay.default || 500 diff --git a/libs/controller.js b/libs/controller.js index 8f302af..4086ff4 100644 --- a/libs/controller.js +++ b/libs/controller.js @@ -11,7 +11,7 @@ let blinksticks; async function findBlinkstick(index, ignoreFilter) { if (!global.config.blinkstick?.cache || blinksticks === undefined) { blinksticks = blinkstick.findAll(); - if (global.config.blinkstick?.serials?.length > 0) { + if (!ignoreFilter && global.config.blinkstick?.serials?.length > 0) { blinksticks = blinksticks.filter((blinkstick) => { return global.config.blinkstick.serials.includes(blinkstick.serial); }); @@ -26,10 +26,7 @@ async function findBlinkstick(index, ignoreFilter) { if (index === undefined) { index = 0; } else if (index !== constants.ALL) { - index = parseInt(index); - } - if (isNaN(index)) { - index = 0; + index = parseInt(index) || 0; } if (index > blinksticks.length - 1) { throw new Error('there is no blinkstick for index \'' + index + '\'');