Skip to content

Commit

Permalink
Adds --list to list available serial ports.
Browse files Browse the repository at this point in the history
Also exits when there's a serial port error.
  • Loading branch information
mildmojo committed Sep 23, 2017
1 parent ccfbcce commit 71221bb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
28 changes: 24 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const fs = require('fs');
const {spawn} = require('child_process');
const SerialPort = require('serialport');
const nconf = require('nconf');
const cjson = require('cjson');
const chalk = require('chalk');
Expand All @@ -13,14 +14,13 @@ nconf.set('port', nconf.get('PORT') || nconf.get('port') || 3000)
nconf.set('testmode', nconf.get('TESTMODE') || nconf.get('testmode'));
verifyConfig(nconf);

if (nconf.get('version')) {
console.log(version);
process.exit();
}
if (nconf.get('version')) showVersionQuit();
if (nconf.get('list')) showPortsQuit();

if (nconf.get('help')) {
console.log('Options:');
console.log(' --help - Prints this usage info');
console.log(' --list - Prints available serial ports');
console.log(' --port n - Listen port number for HTTP server (default: 3000)');
console.log(" --testmode - Simulate serial port activity, don't open real ports");
console.log(' --wrap-cmd cmd - Spawn `cmd` as a subprocess; kill server when subprocess dies and vice versa');
Expand All @@ -36,6 +36,26 @@ server
.then(() => nconf.get('wrap-cmd') && spawnChild())
.catch(console.error);

function showVersionQuit() {
console.log(version);
process.exit();
}

function showPortsQuit() {
SerialPort.list()
.then(portList => {
console.log(chalk.yellow.bold("Paste one or more of these serial ports into 'config.json':"));
for (let port of portList) {
let attrs = Object.keys(port)
.filter(key => key !== 'comName')
.reduce((sum, key) => port[key] ? sum.concat(`${key}: ${port[key]}`) : sum, []);
console.log(chalk.yellow(`"${port.comName}", ` + (attrs.length ? `// ${attrs.join(', ')}` : '')));
}
process.exit();
})
.catch(console.error);
}

function readConfig(file) {
if (!fs.existsSync('./config.json')) {
console.warn(chalk.red.bold(`Could not find ${file}. Please copy` +
Expand Down
14 changes: 9 additions & 5 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,17 @@ function openSerialPorts(portNames, testmode) {
console.warn(`Error with port ${portName}: ${err}`);
SerialPort.list()
.then(portList => {
console.log("Make sure you've configured the right ports. Maybe paste" +
" one or more of these into 'config.json':");
console.log(chalk.yellow.bold("Make sure you've configured the right" +
" ports. Maybe paste one or more of these into 'config.json':"));
for (let port of portList) {
let attrs = Object.keys(port).reduce((sum, key) => port[key] ? sum.concat(`${key}: ${port[key]}`) : sum, []);
console.log(`"${port.comName}", // ${attrs.join(', ')}`);
let attrs = Object.keys(port)
.filter(key => key !== 'comName')
.reduce((sum, key) => port[key] ? sum.concat(`${key}: ${port[key]}`) : sum, []);
console.log(chalk.yellow(`"${port.comName}", ` + (attrs.length ? `// ${attrs.join(', ')}` : '')));
}
});
process.exit(1);
})
.catch(console.error);
});

return serial;
Expand Down

0 comments on commit 71221bb

Please sign in to comment.