Skip to content

Commit

Permalink
fix: improve repl (#2425)
Browse files Browse the repository at this point in the history
- show all the globals
- show baudRate
  • Loading branch information
reconbot committed Feb 7, 2022
1 parent fa10fce commit 7538995
Showing 1 changed file with 11 additions and 11 deletions.
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

0 comments on commit 7538995

Please sign in to comment.