From 2faf87512f71033a4838737cee0a5537c990542a Mon Sep 17 00:00:00 2001 From: Lsong Date: Tue, 20 Aug 2019 14:46:15 +0800 Subject: [PATCH] make serialport module as optional dependency #221 and fix test case # --- .gitignore | 10 ++++----- adapter/serial.js | 2 +- package.json | 20 ++++++++++-------- test/test.js | 54 +++-------------------------------------------- 4 files changed, 20 insertions(+), 66 deletions(-) diff --git a/.gitignore b/.gitignore index 8059743..639dad1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ -.DS_* -node_modules -*.sublime* -psd -thumb + + *.log +yarn.lock package-lock.json + +node_modules/ diff --git a/adapter/serial.js b/adapter/serial.js index ea9b67f..6c1a94f 100644 --- a/adapter/serial.js +++ b/adapter/serial.js @@ -1,7 +1,6 @@ 'use strict'; const util = require('util'); const EventEmitter = require('events'); -const SerialPort = require('serialport'); /** * SerialPort device @@ -14,6 +13,7 @@ function Serial(port, options){ baudRate: 9600, autoOpen: false }; + const SerialPort = require('serialport'); this.device = new SerialPort(port, options); this.device.on('close', function() { self.emit('disconnect', self.device); diff --git a/package.json b/package.json index 7220b14..e5f06af 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "ESC/POS Printer driver for nodejs", "main": "index.js", "scripts": { - "test": "mocha" + "test": "mocha", + "start": "node server.js" }, "repository": { "type": "git", @@ -14,11 +15,7 @@ "escpos", "printer" ], - "author": { - "name": "Lsong", - "email": "hi@lsong.org", - "url": "https://lsong.org" - }, + "author": "Lsong (https://lsong.org)", "contributors": [ "Jose Vera ", "Sébastien Vidal ", @@ -33,14 +30,14 @@ }, "homepage": "https://github.com/song940/node-escpos#readme", "dependencies": { + "qr-image": "*", + "get-pixels": "*", "iconv-lite": "*", "mutable-buffer": "^2.0.3" }, "optionalDependencies": { - "get-pixels": "*", - "serialport": "*", - "qr-image": "*", "usb": "*", + "serialport": "*", "node-bluetooth": "*" }, "devDependencies": { @@ -48,5 +45,10 @@ }, "engines": { "node": ">=8.x" + }, + "directories": { + "doc": "docs", + "example": "examples", + "test": "test" } } diff --git a/test/test.js b/test/test.js index eefe72b..04a5f53 100644 --- a/test/test.js +++ b/test/test.js @@ -8,63 +8,15 @@ describe('ESC/POS printing test', function() { assert.equal(data.length, 3); done(); }); - device.write(new Buffer(3)); + device.write(Buffer.alloc(3)); }) it('printer#print', function(done){ var device = new escpos.Console(function(data){ - assert.deepEqual(data, new Buffer('hello world')); + assert.deepEqual(data, Buffer.from('hello world')); done(); }); var printer = new escpos.Printer(device); printer.print('hello world').flush(); }) - - it('printer#print2', function(done){ - - const networkDevice = new escpos.Network('192.168.1.87', 9100); - - - const options = { encoding: "windows1254" /* default */ } - // encoding is optional - - const printer = new escpos.Printer(networkDevice, options); - - - - networkDevice.open(function(){ - printer.tableCustom([ // Prints table with custom settings (text, align, width, cols,) - { text:"Left", align:"LEFT", width:0.33 }, - { text:"Large Message for New Line ", align:"LEFT", width:0.33 }, - { text:"Right", align:"RIGHT", width:0.33 } - ]); - printer.tableCustom([ // Prints table with custom settings (text, align, width, cols) - { text:"Left", align:"LEFT", width:0.13 }, - { text:"Left", align:"LEFT", width:0.10 }, - { text:"Center", align:"CENTER", width:0.33 }, - { text:"Right", align:"RIGHT", width:0.33 } - ]); - printer.drawLine(); - printer.tableCustom([ // Prints table with custom settings (text, align, width, cols) - { text:"Left", align:"LEFT", width:0.33 }, - { text:"Center", align:"CENTER", width:0.33}, - { text:"Right", align:"RIGHT", width:0.33 } - ]); - printer.tableCustom([ // Prints table with custom settings (text, align, width, cols) - { text:"Left", align:"LEFT", width:0.33 }, - { text:"Center", align:"CENTER", width:0.33 }, - { text:"Right", align:"RIGHT", width:0.33 } - ]); - printer.newLine(); - printer.newLine(); - printer.newLine(); - printer.newLine(); - printer.cut(); - printer.close(); - printer.beep(); - - done(); - }); - }) - -}); \ No newline at end of file +});