Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve repl #2425

Merged
merged 1 commit into from
Feb 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions packages/repl/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import { promirepl } from 'promirepl'
import repl from 'repl'
import { SerialPort, SerialPortMock } from 'serialport'

const baudRate = Number(process.env.BAUDRATE) || 9600

// outputs the path to an arduino or nothing
async function findArduino() {
const envPort = process.argv[2] || process.env.TEST_PORT
if (envPort) {
return envPort
const path = process.argv[2] || process.env.TEST_PORT
const baudRate = Number(process.argv[3] || process.env.BAUDRATE) || 9600
if (path && baudRate) {
return { path, baudRate }
}

const ports = await SerialPort.list()
for (const port of ports) {
if (/arduino/i.test(port.manufacturer || '')) {
return port.path
return { path: port.path, baudRate }
}
}
throw new Error(
Expand All @@ -24,16 +24,16 @@ async function findArduino() {
}

findArduino()
.then(portName => {
.then(({ path, baudRate }: { path: string; baudRate: number }) => {
console.log(`DEBUG=${process.env.DEBUG || ''} # enable debugging with DEBUG=serialport*`)
console.log(`port = SerialPort({ path: "${portName}", autoOpen: false })`)
console.log('globals { SerialPort, SerialPortMock, portName, port }')
const port = new SerialPort({ path: portName, baudRate, autoOpen: false })
console.log(`port = SerialPort({ path: "${path}", baudRate: ${baudRate}, autoOpen: false })`)
console.log('globals { SerialPort, SerialPortMock, path, port }')
const port = new SerialPort({ path, baudRate, autoOpen: false })
const spRepl = repl.start({ prompt: '> ' })
promirepl(spRepl)
spRepl.context.SerialPort = SerialPort
spRepl.context.SerialPortMock = SerialPortMock
spRepl.context.portName = portName
spRepl.context.path = path
spRepl.context.port = port
})
.catch(e => {
Expand Down