/** * Dev script that generates src/environments/environment.local.ts from * .env.local (or process.env) before starting `ng serve`. * * Angular's esbuild-based builder doesn't read .env files like Vite does, * so we generate a TypeScript environment file and use Angular's * fileReplacements to swap it in during the development build. */ import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs'; import { spawn } from 'child_process'; /** Parse a .env file into a key-value object. */ function parseEnvFile(filePath) { const vars = {}; if (!existsSync(filePath)) return vars; for (const line of readFileSync(filePath, 'utf-8').split('\n')) { const trimmed = line.trim(); if (!trimmed || trimmed.startsWith('#')) continue; const eqIdx = trimmed.indexOf('='); if (eqIdx === -1) continue; vars[trimmed.slice(0, eqIdx)] = trimmed.slice(eqIdx + 1); } return vars; } const envFile = parseEnvFile('.env.local'); // Resolve each variable: prefer process.env (set by `spacetime dev`), // then .env.local, then fall back to the defaults in environment.ts. const dbName = process.env.SPACETIMEDB_DB_NAME || envFile.VITE_SPACETIMEDB_DB_NAME || 'angular-ts'; const host = process.env.SPACETIMEDB_HOST || envFile.VITE_SPACETIMEDB_HOST || 'https://maincloud.spacetimedb.com'; mkdirSync('src/environments', { recursive: true }); writeFileSync( 'src/environments/environment.local.ts', [ '// Auto-generated by scripts/dev.mjs — do not edit manually.', 'export const environment = {', ` SPACETIMEDB_HOST: '${host}',`, ` SPACETIMEDB_DB_NAME: '${dbName}',`, '};', '', ].join('\n') ); const child = spawn('npx', ['ng', 'serve'], { stdio: 'inherit', shell: true, }); child.on('exit', (code) => process.exit(code ?? 0));