Skip to content

Commit 571a995

Browse files
committedFeb 27, 2022
Add --qr to show QRCode for root url
This is useful for pulling the root url up on your phone
1 parent 74518b9 commit 571a995

File tree

3 files changed

+868
-9
lines changed

3 files changed

+868
-9
lines changed
 

‎bin/servez

+39-4
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,28 @@ const log = {
1919
},
2020
};
2121

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+
2236
const optionSpec = {
2337
options: [
2438
{ option: 'help', alias: 'h', type: 'Boolean', description: 'displays help' },
2539
{ option: 'port', alias: 'p', type: 'Int', description: 'port', default: '8080' },
2640
{ option: 'version', type: 'Boolean', description: 'print version' },
2741
{ option: 'scan', type: 'Boolean', description: 'scan for open port', default: 'true', },
2842
{ option: 'dirs', type: 'Boolean', description: 'show directory listing', default: 'true', },
43+
{ option: 'qr', type: 'Boolean', description: 'print QR Code for root url' },
2944
{ option: 'cors', type: 'Boolean', description: 'send CORS headers', default: 'true', },
3045
{ option: 'local', type: 'Boolean', description: 'local machine only', default: 'false', },
3146
{ option: 'index', type: 'Boolean', description: 'serve index.html for directories', default: 'true', },
@@ -76,6 +91,8 @@ if (args.version) {
7691

7792
const fs = require('fs');
7893
const path = require('path');
94+
const {QrCode, Ecc} = require('../lib/qrcodegen');
95+
const hosts = [];
7996

8097
const root = path.resolve(args._[0] || process.cwd());
8198
try {
@@ -97,11 +114,29 @@ process.stdin.destroy(); // this allows control-c to not print "Terminate Batch?
97114
process.title = `servez ${root.split(/\\|\//g).slice(-3).join(path.sep)}`;
98115

99116
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 = /\D0\.0\.0\.0.\D|\D127\.0\.0\.|\Wlocalhost\W/
125+
const [data] = args;
126+
const {root} = data;
127+
if (!localRE.test(root)) {
128+
hosts.push(root);
129+
}
102130
},
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.');
105140
},
106141
};
107142

‎lib/qrcodegen.js

+825
Large diffs are not rendered by default.

‎src/server-worker.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ function sendCmd(cmd, data) {
1919

2020
c.enabled = useColors;
2121
const logger = {
22-
log: (...args) => sendCmd('log', [...args]),
23-
error: (...args) => sendCmd('error', [...args]),
22+
log: (...args) => sendCmd('log', args),
23+
error: (...args) => sendCmd('error', args),
2424
c,
2525
};
2626

@@ -29,8 +29,7 @@ const server = new Servez(Object.assign({
2929
dataDir,
3030
logger,
3131
}, args));
32-
server.on('start', () => {
33-
logger.log('press CTRL-C to stop the server.');
34-
});
32+
server.on('host', (...args) => sendCmd('host', args));
33+
server.on('start', (...args) => sendCmd('start', args));
3534

3635

0 commit comments

Comments
 (0)
Please sign in to comment.