node-serialport-bindings-cpp/packages/repl/lib/repl.js
Francis Gulotta 6b85c3d719 chore: rename comName to path (#1859)
We refer to the data as path everywhere but the port info object. This unifies them.

Accessing `comName` will log a warning for one major release and go away afterwards.
2019-05-03 23:20:59 -04:00

52 lines
1.4 KiB
JavaScript

#!/usr/bin/env node
const { promirepl } = require('promirepl')
const repl = require('repl')
const SerialPort = require('serialport')
process.env.DEBUG = process.env.DEBUG || '*'
// outputs the path to an arduino or nothing
function findArduino() {
return new Promise((resolve, reject) => {
const envPort = process.argv[2] || process.env.TEST_PORT
if (envPort) {
return resolve(envPort)
}
SerialPort.list((err, ports) => {
if (err) {
return reject(err)
}
let resolved = false
ports.forEach(port => {
if (!resolved && /arduino/i.test(port.manufacturer)) {
resolved = true
return resolve(port.path)
}
})
if (!resolved) {
reject(
new Error(
'No arduinos found. You must specify a port to load.\n\nFor example:\n\tserialport-repl COM3\n\tserialport-repl /dev/tty.my-serialport'
)
)
}
})
})
}
findArduino()
.then(portName => {
console.log(`port = SerialPort("${portName}", { autoOpen: false })`)
console.log('globals { SerialPort, portName, port }')
const port = new SerialPort(portName, { autoOpen: false })
const spRepl = repl.start({ prompt: '> ' })
promirepl(spRepl)
spRepl.context.SerialPort = SerialPort
spRepl.context.portName = portName
spRepl.context.port = port
})
.catch(e => {
console.error(e.message)
process.exit(1)
})