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(terminal): specifying a port now behaves correctly #1463

Merged
merged 1 commit into from
Feb 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}