Skip to content

Commit

Permalink
chore: unify device commandline argument of the usb scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ert78gb committed Dec 11, 2024
1 parent e10c624 commit 8aac10d
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 51 deletions.
6 changes: 3 additions & 3 deletions packages/uhk-common/src/models/uhk-products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down
18 changes: 13 additions & 5 deletions packages/usb/factory-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <firmwarePath> {uhk60v1|uhk60v2} {iso|ansi}')
.usage(`Usage: $0 <firmwarePath> {${devicesOptions} {iso|ansi}`)
.demandCommand(3)
.option('set-serial-number', {
description: 'Use the given serial number instead of randomly generated one.',
Expand All @@ -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);
Expand Down
25 changes: 0 additions & 25 deletions packages/usb/src/util/get-device-id-from-arg.ts

This file was deleted.

19 changes: 19 additions & 0 deletions packages/usb/src/util/get-uhk-device-product-from-arg.ts
Original file line number Diff line number Diff line change
@@ -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('|');
}
2 changes: 1 addition & 1 deletion packages/usb/src/util/index.ts
Original file line number Diff line number Diff line change
@@ -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';
20 changes: 14 additions & 6 deletions packages/usb/update-firmwares-and-configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <firmware directory> {uhk60v1|uhk60v2} {iso|ansi}')
.usage(`Usage: $0 <firmware directory> {${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.',
Expand All @@ -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)) {
Expand Down
11 changes: 3 additions & 8 deletions packages/usb/wait-for-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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);

Expand Down
12 changes: 9 additions & 3 deletions packages/usb/write-hardware-config.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
#!/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.',
type: 'number',
})
.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)) {
Expand Down

0 comments on commit 8aac10d

Please sign in to comment.