Skip to content

Commit

Permalink
handle missing usb package (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan authored Nov 6, 2023
1 parent e9b3d1f commit f63afdb
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 2,879 deletions.
7 changes: 4 additions & 3 deletions cli/src/flash.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SerialPort } from "serialport"
import { delimiter, join, resolve } from "path"
import { existsSync, readFileSync, Stats, writeFileSync } from "fs"
import { spawn } from "child_process"
Expand All @@ -14,6 +13,7 @@ import { mkdirp } from "fs-extra"
import { delay, groupBy } from "jacdac-ts"
import { buildConfigFromDir } from "./build"
import { patchCustomBoard } from "./binpatch"
import { tryRequire } from "./require"

let buildConfig: ResolvedBuildConfig

Expand Down Expand Up @@ -126,6 +126,7 @@ export async function resolveBoard(arch: string, options: FlashOptions) {
}

export async function flashESP32(options: FlashESP32Options) {
const { SerialPort } = await tryRequire("serialport")
const { board } = await resolveBoard("esp32", options)

if (options.remote) {
Expand Down Expand Up @@ -177,7 +178,7 @@ export async function flashESP32(options: FlashESP32Options) {

const filterPorts = () => {
if (!options.allSerial) {
ports = ports.filter(p =>
ports = ports.filter((p: any) =>
vendors.includes(parseInt(p.vendorId, 16))
)
if (!msg && ports.length == 0) {
Expand All @@ -188,7 +189,7 @@ export async function flashESP32(options: FlashESP32Options) {
}

if (options.port) {
ports = ports.filter(p => p.path == options.port)
ports = ports.filter((p: any) => p.path == options.port)
if (ports.length == 0) {
printPorts()
fatal(`port ${options.port} not found`)
Expand Down
2 changes: 1 addition & 1 deletion cli/src/packageinstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function getPackageInstallerCommand(

if (!packageName) return ["npm", "install"]

return ["npm", "install", "--save", "--no-workspaces", packageName]
return ["npm", "install", "--save", "-D", "--no-workspaces", packageName]
}

async function isPackageInstalledLocally(pkgName: string): Promise<boolean> {
Expand Down
22 changes: 22 additions & 0 deletions cli/src/require.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { isInteractive } from "./command"
import { askForPackageInstallation } from "./packageinstaller"

export class RequireError extends Error {
constructor(public readonly packageName: string) {
super(`failed to require package "${packageName}"`)
}
}

export async function tryRequire(name: string) {
try {
return require(name)
} catch (e) {
console.log(`failed to require package "${name}"`)
console.debug(e.stderr?.toString())
if (isInteractive) {
await askForPackageInstallation(name)
return require(name)
}
throw new RequireError(name)
}
}
24 changes: 2 additions & 22 deletions cli/src/transport.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isInteractive, log } from "./command"
import { log } from "./command"
import {
CONNECTION_STATE,
createNodeSPITransport,
Expand Down Expand Up @@ -27,34 +27,14 @@ import type {
SideUploadJsonFromDevice,
} from "@devicescript/interop"
import { printDmesg } from "./vmworker"
import { askForPackageInstallation } from "./packageinstaller"
import { RequireError, tryRequire } from "./require"

export interface TransportsOptions {
usb?: boolean
serial?: boolean
spi?: boolean
}

export class RequireError extends Error {
constructor(readonly packageName: string) {
super(`failed to require package "${packageName}"`)
}
}

async function tryRequire(name: string) {
try {
return require(name)
} catch (e) {
log(`failed to require package "${name}"`)
console.debug(e.stderr?.toString())
if (isInteractive) {
await askForPackageInstallation(name)
return require(name)
}
throw new RequireError(name)
}
}

async function createSPI() {
log(`adding SPI transport (requires "rpio" package)`)
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down
Loading

0 comments on commit f63afdb

Please sign in to comment.