web-preview-qr
Version:
š± A simple CLI to preview local dev servers on your phone using QR codes ā like Expo Go, but for web.
46 lines (35 loc) ⢠1.19 kB
JavaScript
const os = require('os');
const { exec } = require('child_process');
const qrcode = require('qrcode-terminal');
const PORT = 5173; // Customize if needed
const DEV_COMMAND = 'vite --host';
// ā
Function to get your local IPv4 address
function getLocalIP() {
const interfaces = os.networkInterfaces();
for (const iface of Object.values(interfaces)) {
for (const details of iface) {
if (details.family === 'IPv4' && !details.internal) {
return details.address;
}
}
}
return null;
}
// š Main launcher
const ip = getLocalIP();
if (!ip) {
console.error(
"ā Could not detect local IP. Make sure you're connected to a network.",
);
process.exit(1);
}
const url = `http://${ip}:${PORT}`;
console.log('\nš Local server link for mobile access:');
console.log(`š± ${url}\n`);
console.log('š· Scan the QR code below to open the site on your phone:');
qrcode.generate(url, { small: true });
console.log('\nš Starting the dev server... (running: ' + DEV_COMMAND + ')');
const dev = exec(DEV_COMMAND);
dev.stdout.pipe(process.stdout);
dev.stderr.pipe(process.stderr);