From 5f390fc065c546b7b6f411c6c32ffaa0b3687147 Mon Sep 17 00:00:00 2001 From: Seth Foster Date: Wed, 8 May 2024 09:35:53 -0400 Subject: [PATCH] fix(app-shell-odd): allow running offboard (#15105) It's useful to be able to run the odd shell on your laptop for testing flows, and this code baked in a bunch of directory structure assumptions. These fixes hopefully tolerate the changes by falling back to not scanning for file changes anymore, allowing you to once again run make -C app dev-odd. --- app-shell-odd/src/usb.ts | 68 ++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/app-shell-odd/src/usb.ts b/app-shell-odd/src/usb.ts index 69629eff161..ebfd8bb7f42 100644 --- a/app-shell-odd/src/usb.ts +++ b/app-shell-odd/src/usb.ts @@ -90,40 +90,48 @@ export function watchForMassStorage(dispatch: Dispatch): () => void { prevDirs = present.filter((entry): entry is string => entry !== null) }) - const mediaWatcher = fs.watch( - FLEX_USB_MOUNT_DIR, - { persistent: true }, - (event, fileName) => { - if (!!!fileName) { - rescan(dispatch) - return - } - if (!fileName.match(FLEX_USB_MOUNT_FILTER)) { - return - } - const fullPath = join(FLEX_USB_MOUNT_DIR, fileName) - fsPromises - .stat(fullPath) - .then(info => { - if (!info.isDirectory) { + const mediaWatcherCreator = (): fs.FSWatcher | null => { + try { + return fs.watch( + FLEX_USB_MOUNT_DIR, + { persistent: true }, + (event, fileName) => { + if (!!!fileName) { + rescan(dispatch) return } - if (prevDirs.includes(fullPath)) { + if (!fileName.match(FLEX_USB_MOUNT_FILTER)) { return } - console.log(`New mass storage device ${fileName} detected`) - prevDirs.push(fullPath) - return handleNewlyPresent(fullPath) - }) - .catch(() => { - if (prevDirs.includes(fullPath)) { - console.log(`Mass storage device at ${fileName} removed`) - prevDirs = prevDirs.filter(entry => entry !== fullPath) - dispatch(robotMassStorageDeviceRemoved(fullPath)) - } - }) + const fullPath = join(FLEX_USB_MOUNT_DIR, fileName) + fsPromises + .stat(fullPath) + .then(info => { + if (!info.isDirectory) { + return + } + if (prevDirs.includes(fullPath)) { + return + } + console.log(`New mass storage device ${fileName} detected`) + prevDirs.push(fullPath) + return handleNewlyPresent(fullPath) + }) + .catch(() => { + if (prevDirs.includes(fullPath)) { + console.log(`Mass storage device at ${fileName} removed`) + prevDirs = prevDirs.filter(entry => entry !== fullPath) + dispatch(robotMassStorageDeviceRemoved(fullPath)) + } + }) + } + ) + } catch { + return null } - ) + } + + const mediaWatcher = mediaWatcherCreator() const devWatcher = fs.watch( FLEX_USB_DEVICE_DIR, @@ -150,7 +158,7 @@ export function watchForMassStorage(dispatch: Dispatch): () => void { rescan(dispatch) return () => { - mediaWatcher.close() + mediaWatcher != null && mediaWatcher.close() devWatcher.close() } }