Skip to content

Commit

Permalink
Improve serialport version comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
reyiyo committed Nov 6, 2016
1 parent 9bc4990 commit 81359cd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
38 changes: 21 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ VirtualSerialPort.prototype.write = function write(buffer, callback) {
}
};

VirtualSerialPort.prototype.pause = function pause() {
};
VirtualSerialPort.prototype.pause = function pause() {};

VirtualSerialPort.prototype.resume = function resume() {
};
VirtualSerialPort.prototype.resume = function resume() {};

VirtualSerialPort.prototype.flush = function flush(callback) {
if(callback) {
Expand All @@ -71,20 +69,26 @@ VirtualSerialPort.prototype.isOpen = function isOpen() {
return this.open ? true : false;
};

try {
var SerialPort = require('serialport');
if (SerialPort.SerialPort) {
function VirtualSerialPortFactory() {
try {
var SerialPort = require('serialport');
var serialportPackage = require('serialport/package.json');
var semver = require('semver');

// for v2.x serialport API
VirtualSerialPort = {
SerialPort: VirtualSerialPort,
parsers: SerialPort.parsers
};
} else {
VirtualSerialPort.parsers = SerialPort.parsers;
if(semver.satisfies(serialportPackage.version, '<3.X')) {
this.SerialPort = VirtualSerialPort;
this.parsers = SerialPort.parsers;
return this;
}

VirtualSerialPort.prototype.parsers = SerialPort.parsers;
return VirtualSerialPort;
} catch (error) {
console.warn('VirtualSerialPort - NO parsers available');
}
}
catch(error) {
console.warn('VirtualSerialPort - NO parsers available');

return VirtualSerialPort;
}

module.exports = VirtualSerialPort;
module.exports = new VirtualSerialPortFactory();
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "virtual-serialport",
"version": "0.3.0",
"version": "0.3.1",
"description": "A drop-in virtual replacement for node-serialport's SerialPort object",
"main": "index.js",
"directories": {
Expand Down Expand Up @@ -30,6 +30,7 @@
"url": "https://github.com/reyiyo/virtual-serialport/issues"
},
"optionalDependencies": {
"serialport": "*"
"serialport": "*",
"semver": "^5.3.0"
}
}

0 comments on commit 81359cd

Please sign in to comment.