@@ -48,14 +48,13 @@ export async function checkReceiptStatus(printer) {
48
48
logger . info ( 'Printer changed:' , printer . printer , printer . info ) ;
49
49
const usbDevices = fs . readdirSync ( '/dev/usb' ) ;
50
50
for ( const f of usbDevices ) {
51
- if ( f . startsWith ( 'lp' ) ) {
52
- const lpid = fs . readFileSync ( `/sys/class/usbmisc/${ f } /device/ieee1284_id` , 'utf8' ) . trim ( ) ;
53
- if ( lpid === oldPrinter . info ) {
54
- logger . info ( 'Printer found:' , f , ':' , lpid ) ;
55
- oldPrinter . printer = `/dev/usb/${ f } ` ;
56
- printer = oldPrinter ;
57
- return printer ;
58
- }
51
+ if ( ! f . startsWith ( 'lp' ) ) continue ;
52
+ const lpid = fs . readFileSync ( `/sys/class/usbmisc/${ f } /device/ieee1284_id` , 'utf8' ) . trim ( ) ;
53
+ if ( lpid === oldPrinter . info ) {
54
+ logger . info ( 'Printer found:' , f , ':' , lpid ) ;
55
+ oldPrinter . printer = `/dev/usb/${ f } ` ;
56
+ printer = oldPrinter ;
57
+ return printer ;
59
58
}
60
59
}
61
60
if ( oldPrinter . info !== printer . info ) throw Error ( 'Printer not found, please check the printer connection.' ) ;
@@ -65,22 +64,16 @@ export async function checkReceiptStatus(printer) {
65
64
export async function receiptPrint ( printer , text , printCommand = '' ) {
66
65
const filename = `balloon-${ Date . now ( ) } .txt` ;
67
66
await fs . writeFile ( path . resolve ( process . cwd ( ) , 'data' , filename ) , text ) ;
68
- if ( printCommand ) exec ( printCommand . replace ( / \{ f i l e \} / g, path . resolve ( process . cwd ( ) , 'data' , filename ) ) ) ;
69
- else if ( process . platform === 'win32' ) {
70
- await new Promise ( ( resolve , reject ) => {
71
- exec ( `COPY /B "${ path . resolve ( process . cwd ( ) , 'data' , filename ) } " "${ printer . printer } "` , ( err , stdout , stderr ) => {
72
- if ( err ) {
73
- logger . error ( err ) ;
74
- reject ( err ) ;
75
- }
76
- if ( stdout ) logger . info ( stdout ) ;
77
- if ( stderr ) logger . error ( stderr ) ;
78
- resolve ( null ) ;
79
- } ) ;
80
- } ) ;
81
- } else if ( process . platform === 'darwin' ) {
67
+ const command = printCommand
68
+ ? printCommand . replace ( / \{ f i l e \} / g, path . resolve ( process . cwd ( ) , 'data' , filename ) )
69
+ : process . platform === 'win32'
70
+ ? `COPY /B "${ path . resolve ( process . cwd ( ) , 'data' , filename ) } " "${ printer . printer } "`
71
+ : process . platform === 'darwin'
72
+ ? `lpr -P ${ printer . printer } -o raw ${ path . resolve ( process . cwd ( ) , 'data' , filename ) } `
73
+ : null ;
74
+ if ( command ) {
82
75
await new Promise ( ( resolve , reject ) => {
83
- exec ( `lpr -P ${ printer . printer } -o raw ${ path . resolve ( process . cwd ( ) , 'data' , filename ) } ` , ( err , stdout , stderr ) => {
76
+ exec ( command , ( err , stdout , stderr ) => {
84
77
if ( err ) {
85
78
logger . error ( err ) ;
86
79
reject ( err ) ;
0 commit comments