diff --git a/packages/uhk-common/src/models/uhk-products.ts b/packages/uhk-common/src/models/uhk-products.ts index 11fcb745796..6b253d1ff8d 100644 --- a/packages/uhk-common/src/models/uhk-products.ts +++ b/packages/uhk-common/src/models/uhk-products.ts @@ -42,7 +42,7 @@ export const UNKNOWN_DEVICE: UhkDeviceProduct = { export const UHK_60_DEVICE: UhkDeviceProduct = { id: UHK_DEVICE_IDS.UHK60V1_RIGHT, - asCliArg: 'uhk60v1', + asCliArg: 'uhk60', firmwareUpgradeMethod: FIRMWARE_UPGRADE_METHODS.KBOOT, logName: 'UHK 60 v1', name: 'UHK 60 v1', @@ -112,7 +112,7 @@ export const UHK_60_V2_DEVICE: UhkDeviceProduct = { export const UHK_80_DEVICE_LEFT: UhkDeviceProduct = { id: UHK_DEVICE_IDS.UHK80_LEFT, - asCliArg: 'uhk80left', + asCliArg: 'uhk80-left', firmwareUpgradeMethod: FIRMWARE_UPGRADE_METHODS.MCUBOOT, logName: 'UHK 80 left', name: 'UHK 80', @@ -134,7 +134,7 @@ export const UHK_80_DEVICE_LEFT: UhkDeviceProduct = { export const UHK_80_DEVICE: UhkDeviceProduct = { id: UHK_DEVICE_IDS.UHK80_RIGHT, - asCliArg: 'uhk80', + asCliArg: 'uhk80-right', firmwareUpgradeMethod: FIRMWARE_UPGRADE_METHODS.MCUBOOT, logName: 'UHK 80 right', name: 'UHK 80', diff --git a/packages/usb/factory-update.ts b/packages/usb/factory-update.ts index 9e79b9c5d65..00b9b8e5380 100755 --- a/packages/usb/factory-update.ts +++ b/packages/usb/factory-update.ts @@ -5,25 +5,33 @@ import * as fs from 'fs'; import { FIRMWARE_UPGRADE_METHODS, LEFT_HALF_MODULE, + UHK_60_DEVICE, + UHK_60_V2_DEVICE, UHK_80_DEVICE_LEFT, } from 'uhk-common'; -import { waitForUhkDeviceConnected } from 'uhk-usb'; -import { isUhkDeviceConnected } from 'uhk-usb'; import { getCurrentUhkDeviceProduct, getDeviceFirmwarePath, getDeviceUserConfigPath, getFirmwarePackageJson, getModuleFirmwarePath, + isUhkDeviceConnected, + waitForUhkDeviceConnected, } from 'uhk-usb'; -import Uhk, { errorHandler, getDeviceIdFromArg, yargs } from './src/index.js'; +import Uhk, { errorHandler, getUhkDeviceProductFromArg, getDevicesOptions, yargs } from './src/index.js'; + +const DEVICES = [ + UHK_60_DEVICE, + UHK_60_V2_DEVICE, +]; +const devicesOptions = getDevicesOptions(DEVICES); (async function () { try { const argv = yargs .scriptName('./factory-update.ts') - .usage('Usage: $0 {uhk60v1|uhk60v2} {iso|ansi}') + .usage(`Usage: $0 {${devicesOptions} {iso|ansi}`) .demandCommand(3) .option('set-serial-number', { description: 'Use the given serial number instead of randomly generated one.', @@ -32,7 +40,7 @@ import Uhk, { errorHandler, getDeviceIdFromArg, yargs } from './src/index.js'; .argv; const firmwarePath = argv._[0] as string; - const deviceId = getDeviceIdFromArg(argv._[1] as string); + const deviceId = getUhkDeviceProductFromArg(DEVICES, argv._[1] as string).id; const layout = argv._[2] as string; const uhkDeviceProduct = await getCurrentUhkDeviceProduct(argv); diff --git a/packages/usb/src/util/get-device-id-from-arg.ts b/packages/usb/src/util/get-device-id-from-arg.ts deleted file mode 100644 index d972604467d..00000000000 --- a/packages/usb/src/util/get-device-id-from-arg.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { - UHK_60_DEVICE, - UHK_60_V2_DEVICE, - UHK_80_DEVICE, -} from 'uhk-common'; - -import { InvalidArgumentError } from '../invalid-argument-error.js'; - -const DEVICES = new Map([ - ['uhk60v1', UHK_60_DEVICE.id], - ['uhk60v2', UHK_60_V2_DEVICE.id], - ['uhk80', UHK_80_DEVICE.id], -]); - -function getKeys(): string { - return Array.from(DEVICES.keys()).join('|'); -} - -export function getDeviceIdFromArg(device: string): number { - if (DEVICES.has(device)) { - return DEVICES.get(device); - } - - throw new InvalidArgumentError(`Invalid deviceId. DeviceId should be either {${getKeys()}}`); -} diff --git a/packages/usb/src/util/get-uhk-device-product-from-arg.ts b/packages/usb/src/util/get-uhk-device-product-from-arg.ts new file mode 100644 index 00000000000..dbf84dd41c5 --- /dev/null +++ b/packages/usb/src/util/get-uhk-device-product-from-arg.ts @@ -0,0 +1,19 @@ +import { + UhkDeviceProduct, +} from 'uhk-common'; + +export function getUhkDeviceProductFromArg(uhkDevices: UhkDeviceProduct[], deviceArg: string): UhkDeviceProduct { + const uhkDeviceProduct = uhkDevices.find(uhkDevice => uhkDevice.asCliArg === deviceArg); + + if (uhkDeviceProduct) { + return uhkDeviceProduct; + } + + const devicesOptions = getDevicesOptions(uhkDevices); + console.error(`Invalid device argument: ${deviceArg}. Available options: {${devicesOptions}}`); + process.exit(1); +} + +export function getDevicesOptions(uhkDevices: UhkDeviceProduct[]): string { + return uhkDevices.map(uhkDevice => uhkDevice.asCliArg).join('|'); +} diff --git a/packages/usb/src/util/index.ts b/packages/usb/src/util/index.ts index f9b5921a9cc..06f461d2703 100644 --- a/packages/usb/src/util/index.ts +++ b/packages/usb/src/util/index.ts @@ -1,3 +1,3 @@ -export * from './get-device-id-from-arg.js'; +export * from './get-uhk-device-product-from-arg.js'; export * from './get-i2c-address-from-arg.js'; export * from './get-module-slot-id-from-arg.js'; diff --git a/packages/usb/update-firmwares-and-configs.ts b/packages/usb/update-firmwares-and-configs.ts index 5b546e62492..bde121ead48 100755 --- a/packages/usb/update-firmwares-and-configs.ts +++ b/packages/usb/update-firmwares-and-configs.ts @@ -5,25 +5,33 @@ import * as fs from 'fs'; import { FIRMWARE_UPGRADE_METHODS, LEFT_HALF_MODULE, + UHK_60_DEVICE, + UHK_60_V2_DEVICE, UHK_80_DEVICE_LEFT, } from 'uhk-common'; -import { waitForUhkDeviceConnected } from 'uhk-usb'; -import { isUhkDeviceConnected } from 'uhk-usb'; import { getCurrentUhkDeviceProduct, getDeviceFirmwarePath, getDeviceUserConfigPath, getFirmwarePackageJson, - getModuleFirmwarePath + getModuleFirmwarePath, + isUhkDeviceConnected, + waitForUhkDeviceConnected, } from 'uhk-usb'; -import Uhk, { errorHandler, getDeviceIdFromArg, yargs } from './src/index.js'; +import Uhk, { errorHandler, getUhkDeviceProductFromArg, getDevicesOptions, yargs } from './src/index.js'; + +const DEVICES = [ + UHK_60_DEVICE, + UHK_60_V2_DEVICE, +]; +const devicesOptions = getDevicesOptions(DEVICES); (async () => { try { const argv = yargs .scriptName('./update-firmwares-and-configs.ts') - .usage('Usage: $0 {uhk60v1|uhk60v2} {iso|ansi}') + .usage(`Usage: $0 {${devicesOptions}} {iso|ansi}`) .demandCommand(2, 'Both firmwarePath and layout must be specified.') .option('set-serial-number', { description: 'Use the given serial number instead of randomly generated one.', @@ -32,7 +40,7 @@ import Uhk, { errorHandler, getDeviceIdFromArg, yargs } from './src/index.js'; .argv; const firmwarePath = argv._[0]; - const deviceId = getDeviceIdFromArg(argv._[1] as string); + const deviceId = getUhkDeviceProductFromArg(DEVICES, argv._[1] as string).id; const layout = argv._[2]; if (!fs.existsSync(firmwarePath)) { diff --git a/packages/usb/wait-for-device.ts b/packages/usb/wait-for-device.ts index 6265c01fe83..fce32c8ae4a 100755 --- a/packages/usb/wait-for-device.ts +++ b/packages/usb/wait-for-device.ts @@ -12,11 +12,11 @@ import { snooze, } from 'uhk-usb'; -import { yargs } from './src/index.js'; +import { getDevicesOptions, getUhkDeviceProductFromArg, yargs } from './src/index.js'; const REENUMERATION_MODES = ['device', 'bootloader', 'buspal']; const reenumerationOptions = REENUMERATION_MODES.join('|'); -const devicesOptions = ALL_UHK_DEVICES.map(uhkDevice => uhkDevice.asCliArg).join('|'); +const devicesOptions = getDevicesOptions(ALL_UHK_DEVICES); const argv = yargs .scriptName('./wait-for-device.ts') @@ -28,12 +28,7 @@ const deviceArg = argv._[0] as string; const enumerationModeArg = argv._[1] as string; const timeoutArg = argv._[2] as string; -const uhkDeviceProduct = ALL_UHK_DEVICES.find(uhkDevice => uhkDevice.asCliArg === deviceArg); - -if (!uhkDeviceProduct) { - console.error(`Invalid device: ${deviceArg}. Available options: ${devicesOptions}`); - process.exit(1); -} +const uhkDeviceProduct = getUhkDeviceProductFromArg(ALL_UHK_DEVICES, deviceArg); const reenumerationMode = REENUMERATION_MODES.find(value => value === enumerationModeArg); diff --git a/packages/usb/write-hardware-config.ts b/packages/usb/write-hardware-config.ts index d18106d6972..307542521ae 100755 --- a/packages/usb/write-hardware-config.ts +++ b/packages/usb/write-hardware-config.ts @@ -1,12 +1,18 @@ #!/usr/bin/env -S node --loader ts-node/esm --no-warnings=ExperimentalWarning -import Uhk, { errorHandler, getDeviceIdFromArg, yargs } from './src/index.js'; +import { + ALL_UHK_DEVICES, +} from 'uhk-common'; + +import Uhk, { errorHandler, getUhkDeviceProductFromArg, getDevicesOptions, yargs } from './src/index.js'; + +const devicesOptions = getDevicesOptions(ALL_UHK_DEVICES); (async () => { try { const argv = yargs .scriptName('./write-hardware-config.ts') - .usage('Usage: $0 {uhk60v1|uhk60v2|uhk80} {iso|ansi}') + .usage(`Usage: $0 {${devicesOptions}} {iso|ansi}`) .demandCommand(2, 'DeviceId and layout is required.') .option('set-serial-number', { description: 'Use the given serial number instead of randomly generated one.', @@ -14,7 +20,7 @@ import Uhk, { errorHandler, getDeviceIdFromArg, yargs } from './src/index.js'; }) .argv; - const deviceId = getDeviceIdFromArg(argv._[0] as string); + const deviceId = getUhkDeviceProductFromArg(ALL_UHK_DEVICES, argv._[0] as string).id; const layout = argv._[1] as string; if (!['ansi', 'iso'].includes(layout)) {