From 0b63247b4d6d38d3215ba4ad5c8188a6fcdb76f2 Mon Sep 17 00:00:00 2001 From: Roberto Mosca Date: Wed, 13 Dec 2023 18:43:33 +0100 Subject: [PATCH] Remove dependency of extension from SerialPort --- src/device.ts | 9 +++++++++ src/extension.ts | 15 +++++++++------ src/test/device.test.ts | 21 +++++++++++++++++---- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/device.ts b/src/device.ts index 106c5cd..9855463 100644 --- a/src/device.ts +++ b/src/device.ts @@ -43,6 +43,11 @@ export type StorageStatus = { slots: SlotsInfo; }; +type DeviceSpec = { + path?: string; + manufacturer?: string; +}; + function checkSlotId(slotId: number) { if (slotId < 0 || slotId > 19) { throw new Error(`Invalid program slot index ${slotId}, valid slots: 0-19`); @@ -298,4 +303,8 @@ export function removeDeviceAllListeners() { device?.removeAllListeners(); } +export async function listDevices(): Promise { + return _testing.SerialPortType.list(); +} + export { _testing }; diff --git a/src/extension.ts b/src/extension.ts index 9398761..8cdd0f6 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -6,14 +6,14 @@ import { disconnectDevice, getDeviceSlots, isDeviceConnected, + listDevices, moveProgramOnDevice, - runProgramOnDevice, removeProgramFromDevice, + runProgramOnDevice, stopProgramOnDevice, uploadProgramToDevice, } from './device'; import { existsSync } from 'fs'; -import { SerialPort } from 'serialport'; import { basename } from 'path'; const toastDuration = 5000; @@ -94,12 +94,15 @@ async function askDeviceName() { } async function askDeviceFromList(manualEntry: string) { - const serialPorts = await SerialPort.list(); + const devices = await listDevices(); + // using this promise in the quick-pick will cause a progress // bar to show if there are no items. - const list = serialPorts - .filter((port) => port.manufacturer === 'LEGO System A/S') - .map((port) => port.path); + const list = devices + .filter( + (device) => device.manufacturer === 'LEGO System A/S' && device.path, + ) + .map((device) => device.path!); list.push(manualEntry); const selected = await vscode.window.showQuickPick(list, { diff --git a/src/test/device.test.ts b/src/test/device.test.ts index 206d994..beec120 100644 --- a/src/test/device.test.ts +++ b/src/test/device.test.ts @@ -12,6 +12,7 @@ import { addDeviceOnChangeCallbak, getDeviceInfo, removeDeviceAllListeners, + listDevices, } from '../device'; import { SerialPortMock } from 'serialport'; import * as chai from 'chai'; @@ -60,12 +61,11 @@ describe('Device', () => { APIRequestStub.restore(); }); - const createPortMock = (data: string, opts?: any) => { - return SerialPortMock.binding.createPort(deviceName, { + const createPortMock = (alternativeName?: string) => { + return SerialPortMock.binding.createPort(alternativeName || deviceName, { echo: true, record: true, - ...opts, - readyData: Buffer.from(data), + readyData: Buffer.from(''), }); }; @@ -464,4 +464,17 @@ describe('Device', () => { expect(onChangeFn).to.have.been.calledOnce; }); }); + + describe('listDevices', function () { + it('should list all available devices', async function () { + createPortMock('/dev/usbmodem1'); + createPortMock('/dev/usbmodem2'); + + const devices = await listDevices(); + + expect(devices).to.have.lengthOf(2); + expect(devices[0].path).to.equal('/dev/usbmodem1'); + expect(devices[1].path).to.equal('/dev/usbmodem2'); + }); + }); });