1
1
/* eslint-disable no-await-in-loop */
2
2
import path from 'path' ;
3
- // https://www.sumatrapdfreader.org//dl//rel/3.1.2/SumatraPDF-3.1.2.zip
4
- import winPrint from '@myteril/node-win-printer' ;
5
3
import superagent from 'superagent' ;
6
- import { getPrinters , print } from 'unix-print' ;
7
4
import { config , saveConfig } from '../config' ;
8
- import { fs , Logger , sleep } from '../utils' ;
5
+ import {
6
+ fs , getPrinters , initWinPrinter , Logger , print , sleep ,
7
+ } from '../utils' ;
9
8
import { createTypstCompiler , generateTypst } from './typst' ;
10
9
11
10
let compiler ;
@@ -14,41 +13,6 @@ const logger = new Logger('printer');
14
13
15
14
let timer = null ;
16
15
17
- async function fetchTask ( c ) {
18
- if ( timer ) clearTimeout ( timer ) ;
19
- logger . info ( 'Fetching Task from tools server...' ) ;
20
- try {
21
- const printersInfo = await getPrinters ( ) ;
22
- const { body } = await superagent . post ( `${ c . server } /client/${ c . token } /print` )
23
- . send ( {
24
- printers : config . printers ,
25
- printersInfo : JSON . stringify ( printersInfo . map ( ( p ) => ( {
26
- printer : p . printer ,
27
- status : p . status ,
28
- description : p . description ,
29
- } ) ) ) ,
30
- } ) ;
31
- if ( body . setPrinter ) {
32
- config . printers = body . setPrinter ;
33
- saveConfig ( ) ;
34
- logger . info ( `Printer set to ${ config . printers } ` ) ;
35
- }
36
- if ( body . doc ) {
37
- logger . info ( `Print task ${ body . doc . tid } #${ body . doc . _id } ...` ) ;
38
- await printFile ( body . doc ) ;
39
- await superagent . post ( `${ c . server } /client/${ c . token } /doneprint/${ body . doc . _id } ` ) ;
40
- logger . info ( `Print task ${ body . doc . tid } #${ body . doc . _id } completed.` ) ;
41
- } else {
42
- logger . info ( 'No print task, sleeping...' ) ;
43
- await sleep ( 5000 ) ;
44
- }
45
- } catch ( e ) {
46
- logger . error ( e ) ;
47
- await sleep ( 5000 ) ;
48
- }
49
- timer = setTimeout ( ( ) => fetchTask ( c ) , 3000 ) ;
50
- }
51
-
52
16
export async function ConvertCodeToPDF ( code , lang , filename , team , location ) {
53
17
compiler ||= await createTypstCompiler ( ) ;
54
18
const typst = generateTypst ( team , location , filename , lang ) ;
@@ -77,14 +41,14 @@ export async function printFile(doc) {
77
41
const randomP = printers [ Math . floor ( Math . random ( ) * printers . length ) ] ;
78
42
if ( randomP . status === 'idle' ) {
79
43
logger . info ( `Printing ${ _id } on ${ randomP . printer } ` ) ;
80
- await print ( path . resolve ( process . cwd ( ) , `data/${ tid } #${ _id } .pdf` ) , randomP . printer , [ '-P' , '1-5' ] ) ;
44
+ await print ( path . resolve ( process . cwd ( ) , `data/${ tid } #${ _id } .pdf` ) , randomP . printer , 1 , 5 ) ;
81
45
return ;
82
46
}
83
47
for ( const printer of printers . filter ( ( p ) => p . printer !== randomP . printer ) ) {
84
48
logger . info ( `Checking ${ printer . printer } ${ printer . status } ` ) ;
85
49
if ( printer . status === 'idle' ) {
86
50
logger . info ( `Printing ${ _id } on ${ printer . printer } ` ) ;
87
- await print ( path . resolve ( process . cwd ( ) , `data/${ tid } #${ _id } .pdf` ) , printer . printer , [ '-P' , '1-5' ] ) ;
51
+ await print ( path . resolve ( process . cwd ( ) , `data/${ tid } #${ _id } .pdf` ) , printer . printer , 1 , 5 ) ;
88
52
return ;
89
53
}
90
54
}
@@ -97,7 +61,50 @@ export async function printFile(doc) {
97
61
}
98
62
}
99
63
64
+ async function fetchTask ( c ) {
65
+ if ( timer ) clearTimeout ( timer ) ;
66
+ logger . info ( 'Fetching Task from tools server...' ) ;
67
+ try {
68
+ const printersInfo = await getPrinters ( ) ;
69
+ const { body } = await superagent . post ( `${ c . server } /client/${ c . token } /print` )
70
+ . send ( {
71
+ printers : config . printers ,
72
+ printersInfo : JSON . stringify ( printersInfo . map ( ( p ) => ( {
73
+ printer : p . printer ,
74
+ status : p . status ,
75
+ description : p . description ,
76
+ } ) ) ) ,
77
+ } ) ;
78
+ if ( body . setPrinter ) {
79
+ config . printers = body . setPrinter ;
80
+ saveConfig ( ) ;
81
+ logger . info ( `Printer set to ${ config . printers } ` ) ;
82
+ }
83
+ if ( body . doc ) {
84
+ logger . info ( `Print task ${ body . doc . tid } #${ body . doc . _id } ...` ) ;
85
+ await printFile ( body . doc ) ;
86
+ await superagent . post ( `${ c . server } /client/${ c . token } /doneprint/${ body . doc . _id } ` ) ;
87
+ logger . info ( `Print task ${ body . doc . tid } #${ body . doc . _id } completed.` ) ;
88
+ } else {
89
+ logger . info ( 'No print task, sleeping...' ) ;
90
+ await sleep ( 5000 ) ;
91
+ }
92
+ } catch ( e ) {
93
+ logger . error ( e ) ;
94
+ await sleep ( 5000 ) ;
95
+ }
96
+ timer = setTimeout ( ( ) => fetchTask ( c ) , 3000 ) ;
97
+ }
98
+
100
99
export async function apply ( ) {
101
100
compiler = await createTypstCompiler ( ) ;
101
+ if ( process . platform === 'win32' ) {
102
+ try {
103
+ initWinPrinter ( ) ;
104
+ } catch ( e ) {
105
+ logger . error ( e ) ;
106
+ process . exit ( 1 ) ;
107
+ }
108
+ }
102
109
if ( config . token && config . server && config . printers ?. length ) await fetchTask ( config ) ;
103
110
}
0 commit comments