Skip to content

Commit

Permalink
fix: refactor ports command to use promises
Browse files Browse the repository at this point in the history
  • Loading branch information
ajfisher committed Dec 7, 2019
1 parent ad8376b commit fb14978
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 35 deletions.
13 changes: 5 additions & 8 deletions lib/inquire.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ const Inquire = function(cb) {

this.callback = cb;

this.interchange.get_ports((err, ports) => {
if (err) {
console.error(err);
return;
}

this.promptQuestions(ports);
});
this.interchange.get_ports()
.then(ports => {
this.promptQuestions(ports);
})
.catch(err => console.error(err));
};

Inquire.prototype.promptQuestions = function(ports) {
Expand Down
41 changes: 14 additions & 27 deletions lib/interchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,39 +44,26 @@ Interchange.prototype.list_devices = function() {
});
});

return ({firmwares: fws});
return (fws);
};

Interchange.prototype.get_ports = function(cb) {
Serialport.list()
.then(function(ports) {
if (cb) return cb(null, ports);
})
.catch((err) => {
if (cb) return cb(err);
});
return new Promise((resolve, reject) => {
Serialport.list()
.then((ports) => {
resolve(ports);
})
.catch((err) => {
reject(err);
});
});
};

Interchange.prototype.list_ports = function(opts) {
Interchange.prototype.list_ports = function() {
// this function lists out all the ports available to flash firmware

const verbose = opts.verbose;

this.get_ports(function(err, ports) {
if (err) {
console.error(err);
return;
}

if (verbose) {
console.info(ports);
} else {
ports.forEach(function(port) {
console.info(port.comName.cyan);
console.info(port.manufacturer);
});
}
});
// this function is now deprecated
console.warn('list_ports method deprecated - use get_ports instead');
return this.get_ports();
};

Interchange.prototype.get_firmware_info = function(port) {
Expand Down

0 comments on commit fb14978

Please sign in to comment.