Skip to content

Commit

Permalink
fix(terminal): specifying a port now behaves correctly (#1463)
Browse files Browse the repository at this point in the history
- Don't list the ports when specifying a port
- Small promise refactor.
  • Loading branch information
reconbot authored Feb 4, 2018
1 parent 7d0ff88 commit 1fa20e7
Showing 1 changed file with 29 additions and 42 deletions.
71 changes: 29 additions & 42 deletions bin/terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,53 +24,40 @@ args
.option('--echo --localecho', 'Print characters as you type them.')
.parse(process.argv);

function logErrorAndExit(error) {
console.error(error);
process.exit(1);
}

function listPorts() {
SerialPort.list((err, ports) => {
if (err) {
console.error('Error listing ports', err);
} else {
ports.forEach((port) => {
console.log(`${port.comName}\t${port.pnpId || ''}\t${port.manufacturer || ''}`);
});
}
SerialPort.list().then(ports => {
ports.forEach((port) => {
console.log(`${port.comName}\t${port.pnpId || ''}\t${port.manufacturer || ''}`);
});
}, err => {
console.error('Error listing ports', err);
});
};

function setupPort() {
if (args.port) {
createPort(args.port);
}
function askForPort() {
return SerialPort.list().then(ports => {
if (ports.length === 0) {
console.error('No ports detected and none specified');
process.exit(2);
}

SerialPort.list((err, ports) => {
if (err) {
console.error('Error listing ports, and missing port argument.', err);
args.outputHelp();
args.missingArgument('port');
process.exit(4);
} else {
if (ports.length > 0) {
const portSelection = new List({
name: 'serial-port-selection',
message: 'Select a serial port to open',
choices: ports.map((port, i) => `[${i + 1}]\t${port.comName}\t${port.pnpId || ''}\t${port.manufacturer || ''}`)
});
const portSelection = new List({
name: 'serial-port-selection',
message: 'Select a serial port to open',
choices: ports.map((port, i) => `[${i + 1}]\t${port.comName}\t${port.pnpId || ''}\t${port.manufacturer || ''}`)
});

portSelection.run()
.then(answer => {
const choice = answer.split('\t')[1];
console.log(`Opening serial port: ${choice}`);
createPort(choice);
})
.catch(error => {
console.log(`Could not select a port: ${error}`);
process.exit(2);
});
} else {
args.outputHelp();
args.missingArgument('port');
process.exit(3);
}
}
return portSelection.run()
.then(answer => {
const choice = answer.split('\t')[1];
console.log(`Opening serial port: ${choice}`);
return choice;
});
});
}

Expand Down Expand Up @@ -114,5 +101,5 @@ function createPort(selectedPort) {
if (args.list) {
listPorts();
} else {
setupPort();
Promise.resolve(args.port || askForPort()).then(createPort).catch(logErrorAndExit);
}

0 comments on commit 1fa20e7

Please sign in to comment.