Something went wrong. Try again.
Monorepo for Aesthetic.Computer aesthetic.computer
Something went wrong. Try again.
JavaScript
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192// import { networkInterfaces } from "os";import qrcode from "qrcode-terminal";import got from "got";
// Detect if we're running in GitHub Codespacesconst isCodespaces = process.env.CODESPACES === "true";const codespaceName = process.env.CODESPACE_NAME;const codespacesDomain = process.env.GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN;
let bootUps = 0;const bootUpTimer = setInterval(() => { process.stdout.write("\x1Bc"); // Clear terminal. const message = bootUps % 2 === 0 ? "Starting..." : "Starting. . ."; console.log(`\n๐ ${message}\n`); bootUps += 1;}, 250);
async function constructUrl() { // Determine the URL based on environment let checkUrl, displayUrl, localUrl; if (isCodespaces && codespaceName && codespacesDomain) { // In Codespaces: netlify dev runs on HTTP, but Codespaces proxies it to HTTPS checkUrl = "http://localhost:8888"; displayUrl = `https://${codespaceName}-8888.${codespacesDomain}`; localUrl = displayUrl; // In Codespaces, "local" URL is the forwarded HTTPS URL } else { // On local laptop: netlify dev runs with SSL certificates checkUrl = "https://localhost:8888"; displayUrl = `https://${process.env.HOST_IP}:8888`; localUrl = "https://localhost:8888"; }
// Generate QR code in the terminal once a 200 is received from `checkUrl`. let attempts = 0; const maxAttempts = 120; // 60 seconds max wait time try { while (attempts < maxAttempts) { try { const response = await got.get(checkUrl, { https: { rejectUnauthorized: false }, timeout: { request: 2000 // 2 second timeout per request }, throwHttpErrors: false // Don't throw on 404, we just want to know server is up }); // Accept any response code (200, 404, etc.) as proof the server is running if (response.statusCode) { clearInterval(bootUpTimer); setTimeout(() => { process.stdout.write("\x1Bc"); // Clear terminal. console.log(''); // Blank line. if (isCodespaces) { console.log(`๐ Codespaces URL: ${displayUrl}`); console.log(`๐ Local Check: ${checkUrl}`); console.log(`๐ก World: https://prompt.ac\n`); } else { qrcode.generate(displayUrl, { small: true }); console.log(`๐ป LAN ${displayUrl}`); console.log(`๐ Local ${localUrl}`); console.log(`๐ก World https://prompt.ac\n`); } }, 100); break; } } catch (error) { // Debug: uncomment to see what's happening // console.error(`Attempt ${attempts + 1}: ${error.message}`); } attempts++; await new Promise((res) => setTimeout(res, 500)); // Hang out half a sec. } if (attempts >= maxAttempts) { clearInterval(bootUpTimer); process.stdout.write("\x1Bc"); // Clear terminal. console.log("โ Server failed to start after 60 seconds"); console.log(`๐ง Checked: ${checkUrl}`); console.log("๏ฟฝ Try running 'npm run site' manually to debug"); process.exit(1); } } finally { if (!isCodespaces) { console.log("๐ช Tunneling..."); } }}
constructUrl();