@@ -19,13 +19,28 @@ const log = {
19
19
} ,
20
20
} ;
21
21
22
+ function genQRCode ( s ) {
23
+ const qr = QrCode . encodeText ( s , Ecc . MEDIUM ) ;
24
+
25
+ const lines = [ ] ;
26
+ for ( let y = 0 ; y < qr . size ; y ++ ) {
27
+ const line = [ ] ;
28
+ for ( let x = 0 ; x < qr . size ; x ++ ) {
29
+ line . push ( c [ qr . getModule ( x , y ) ? 'bgBlack' : 'bgWhite' ] ( ' ' ) ) ;
30
+ }
31
+ lines . push ( line . join ( '' ) ) ;
32
+ }
33
+ return lines . join ( '\n' ) ;
34
+ }
35
+
22
36
const optionSpec = {
23
37
options : [
24
38
{ option : 'help' , alias : 'h' , type : 'Boolean' , description : 'displays help' } ,
25
39
{ option : 'port' , alias : 'p' , type : 'Int' , description : 'port' , default : '8080' } ,
26
40
{ option : 'version' , type : 'Boolean' , description : 'print version' } ,
27
41
{ option : 'scan' , type : 'Boolean' , description : 'scan for open port' , default : 'true' , } ,
28
42
{ option : 'dirs' , type : 'Boolean' , description : 'show directory listing' , default : 'true' , } ,
43
+ { option : 'qr' , type : 'Boolean' , description : 'print QR Code for root url' } ,
29
44
{ option : 'cors' , type : 'Boolean' , description : 'send CORS headers' , default : 'true' , } ,
30
45
{ option : 'local' , type : 'Boolean' , description : 'local machine only' , default : 'false' , } ,
31
46
{ option : 'index' , type : 'Boolean' , description : 'serve index.html for directories' , default : 'true' , } ,
@@ -76,6 +91,8 @@ if (args.version) {
76
91
77
92
const fs = require ( 'fs' ) ;
78
93
const path = require ( 'path' ) ;
94
+ const { QrCode, Ecc} = require ( '../lib/qrcodegen' ) ;
95
+ const hosts = [ ] ;
79
96
80
97
const root = path . resolve ( args . _ [ 0 ] || process . cwd ( ) ) ;
81
98
try {
@@ -97,11 +114,29 @@ process.stdin.destroy(); // this allows control-c to not print "Terminate Batch?
97
114
process . title = `servez ${ root . split ( / \\ | \/ / g) . slice ( - 3 ) . join ( path . sep ) } ` ;
98
115
99
116
const commands = {
100
- log ( data ) {
101
- console . log ( ...data ) ;
117
+ log ( args ) {
118
+ console . log ( ...args ) ;
119
+ } ,
120
+ error ( args ) {
121
+ console . error ( c . red ( args . join ( ' ' ) ) ) ;
122
+ } ,
123
+ host ( args ) {
124
+ const localRE = / \D 0 \. 0 \. 0 \. 0 .\D | \D 1 2 7 \. 0 \. 0 \. | \W l o c a l h o s t \W /
125
+ const [ data ] = args ;
126
+ const { root} = data ;
127
+ if ( ! localRE . test ( root ) ) {
128
+ hosts . push ( root ) ;
129
+ }
102
130
} ,
103
- error ( data ) {
104
- console . error ( c . red ( [ ...data ] . join ( ' ' ) ) ) ;
131
+ start ( data ) {
132
+ if ( args . qr ) {
133
+ for ( const host of hosts ) {
134
+ log . info ( `--------------\nQR code for: ${ host } ` ) ;
135
+ log . info ( genQRCode ( host ) ) ;
136
+ log . info ( '' ) ;
137
+ }
138
+ }
139
+ log . info ( 'press CTRL-C to stop the server.' ) ;
105
140
} ,
106
141
} ;
107
142
0 commit comments