Skip to content

Commit 38e96b8

Browse files
committed
rewrite lp command
1 parent 0be1163 commit 38e96b8

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

packages/server/utils/receipt.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,13 @@ export async function checkReceiptStatus(printer) {
4848
logger.info('Printer changed:', printer.printer, printer.info);
4949
const usbDevices = fs.readdirSync('/dev/usb');
5050
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;
5958
}
6059
}
6160
if (oldPrinter.info !== printer.info) throw Error('Printer not found, please check the printer connection.');
@@ -65,22 +64,16 @@ export async function checkReceiptStatus(printer) {
6564
export async function receiptPrint(printer, text, printCommand = '') {
6665
const filename = `balloon-${Date.now()}.txt`;
6766
await fs.writeFile(path.resolve(process.cwd(), 'data', filename), text);
68-
if (printCommand) exec(printCommand.replace(/\{file\}/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(/\{file\}/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) {
8275
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) => {
8477
if (err) {
8578
logger.error(err);
8679
reject(err);

0 commit comments

Comments
 (0)