Something went wrong. Try again.
Atproto AMA app
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485#!/usr/bin/env tsx
/** * Auto-update .env with dev network URLs by parsing dev-network output. * * Usage: * pnpm run dev:network | pnpm exec tsx scripts/update-env.ts * * Or use the combined script: * pnpm run dev:network:auto */
import { readFileSync, writeFileSync } from "fs";import { join } from "path";
const ENV_FILE = join(process.cwd(), ".env");
let envContent = "";try { envContent = readFileSync(ENV_FILE, "utf8");} catch (err) { console.error("Failed to read .env file:", err); process.exit(1);}
// Listen for stdin to parse dev-network outputlet buffer = "";let updated = false;process.stdin.setEncoding("utf8");
process.stdin.on("data", (chunk) => { buffer += chunk; process.stdout.write(chunk); // Pass through output
if (updated) return;
const pdsMatch = buffer.match(/DEV_PDS_URL=(http:\/\/(?:localhost|127\.0\.0\.1):\d+)/); const plcMatch = buffer.match(/DEV_PLC_URL=(http:\/\/(?:localhost|127\.0\.0\.1):\d+)/); const jetstreamMatch = buffer.match(/JETSTREAM_URL=(ws:\/\/127\.0\.0\.1:\d+\/subscribe)/);
if (pdsMatch && plcMatch && jetstreamMatch) { updated = true; const newPdsUrl = pdsMatch[1]; const newPlcUrl = plcMatch[1]; const newJetstreamUrl = jetstreamMatch[1];
const alreadySet = envContent.includes(`DEV_PDS_URL=${newPdsUrl}`) && envContent.includes(`DEV_PLC_URL=${newPlcUrl}`) && envContent.includes(`JETSTREAM_URL=${newJetstreamUrl}`);
if (alreadySet) { console.log( `\n✅ .env already has correct URLs (no restart needed):\n` + ` DEV_PDS_URL=${newPdsUrl}\n` + ` DEV_PLC_URL=${newPlcUrl}\n` + ` JETSTREAM_URL=${newJetstreamUrl}\n`, ); return; }
let updatedContent = envContent; updatedContent = updatedContent.replace(/DEV_PDS_URL=.*/, `DEV_PDS_URL=${newPdsUrl}`); updatedContent = updatedContent.replace(/DEV_PLC_URL=.*/, `DEV_PLC_URL=${newPlcUrl}`); if (/JETSTREAM_URL=.*/.test(updatedContent)) { updatedContent = updatedContent.replace(/JETSTREAM_URL=.*/, `JETSTREAM_URL=${newJetstreamUrl}`); } else { updatedContent += `\nJETSTREAM_URL=${newJetstreamUrl}\n`; } if (!/DEV_JETSTREAM_DELAY_MS=.*/.test(updatedContent)) { updatedContent += `DEV_JETSTREAM_DELAY_MS=1500\n`; }
try { writeFileSync(ENV_FILE, updatedContent); console.log(`\n🔄 Updated .env with new URLs:`); console.log(` DEV_PDS_URL=${newPdsUrl}`); console.log(` DEV_PLC_URL=${newPlcUrl}`); console.log(` JETSTREAM_URL=${newJetstreamUrl}\n`); } catch (err) { console.error("Failed to update .env:", err); } }});