Skip to content

Commit

Permalink
make usb an optional dependency (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewLuGit authored Dec 14, 2024
1 parent e496167 commit 222f90a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 26 deletions.
22 changes: 16 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,9 @@
"seek-bzip": "^2.0.0",
"semver": "^7.5.2",
"tar-fs": "^2.1.1",
"universal-analytics": "^0.5.3",
"universal-analytics": "^0.5.3"
},
"optionalDependencies": {
"usb": "^2.10.0"
}
}
22 changes: 14 additions & 8 deletions src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { prosLogger } from "./extension";
import { PREFIX } from "./commands/cli-parsing";
import { StatusBarItem, window, workspace } from "vscode";
import { BaseCommand } from "./commands";
import { usb } from "usb";
try {
var usb = require("usb").usb;
} catch (err) {
usb = null;
}

/* eslint-disable @typescript-eslint/naming-convention */
export type DeviceInfo = {
Expand Down Expand Up @@ -269,12 +273,14 @@ export const setTeam = async (team: string): Promise<void> => {
};

export const startPortMonitoring = (status: StatusBarItem): void => {
status.show();
usb.addListener("attach", () => {
resolvePort(status);
});
usb.addListener("detach", () => {
if (usb) {
status.show();
usb.addListener("attach", () => {
resolvePort(status);
});
usb.addListener("detach", () => {
resolvePort(status);
});
resolvePort(status);
});
resolvePort(status);
}
};
28 changes: 17 additions & 11 deletions src/views/brain-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
setTeam,
} from "../device";
import { getNonce } from "./nonce";
import { usb } from "usb";
try {
var usb = require("usb").usb;
} catch (err) {
usb = null;
}

export class BrainViewProvider implements vscode.WebviewViewProvider {
public static readonly viewType = "pros.brainView";
Expand Down Expand Up @@ -51,16 +55,18 @@ export class BrainViewProvider implements vscode.WebviewViewProvider {
break;
}
});
usb.addListener("attach", () => {
setTimeout(() => {
this.updateDeviceList();
}, 1000);
});
usb.addListener("detach", () => {
setTimeout(() => {
this.updateDeviceList();
}, 1000);
});
if (usb) {
usb.addListener("attach", () => {
setTimeout(() => {
this.updateDeviceList();
}, 1000);
});
usb.addListener("detach", () => {
setTimeout(() => {
this.updateDeviceList();
}, 1000);
});
}
this.updateDeviceList();
}

Expand Down

0 comments on commit 222f90a

Please sign in to comment.