Open History to see the last commit
1import { execFileSync } from 'node:child_process';
2import { mkdir, readFile, writeFile } from 'node:fs/promises';
3import { generateKeyPairSync, randomBytes } from 'node:crypto';
4import { resolve } from 'node:path';
5import { parse } from 'dotenv';
6
7const root = resolve(import.meta.dirname, '..');
8process.env.PATH = `${resolve(root, 'node_modules/.bin')}:${process.env.PATH}`;
9const orgId = process.argv[2];
10if (!orgId) throw new Error('Usage: pnpm exec tsx scripts/setup.ts <personal-org-id>');
11const cli = (args: string[]) => JSON.parse(execFileSync('neon', [...args, '--output', 'json'], { cwd: root, encoding: 'utf8', maxBuffer: 4_000_000 }));
12await mkdir(resolve(root, '.local'), { recursive: true });
13let state: Record<string, any> = {};
14try { state = JSON.parse(await readFile(resolve(root, '.local/setup.json'), 'utf8')); } catch {}
15if (state.orgId && state.orgId !== orgId) throw new Error('This directory is already bound to another organization');
16state.orgId = orgId;
17if (!state.project) {
18 state.project = cli(['projects', 'create', '--org-id', orgId, '--name', 'neon-glow-app', '--region-id', 'aws-eu-central-1', '--cu', '0.25-1']);
19 await writeFile(resolve(root, '.local/setup.json'), JSON.stringify(state), { mode: 0o600 });
20}
21if (!state.apiKey) {
22 state.apiKey = cli(['api-keys', 'create', '--org-id', orgId, '--name', 'Neon Glow development fleet']);
23 await writeFile(resolve(root, '.local/setup.json'), JSON.stringify(state), { mode: 0o600 });
24}
25const project = state.project.project ?? state.project;
26execFileSync('neon', ['link', '--project-id', project.id, '--branch', 'main'], { cwd: root, stdio: 'pipe' });
27execFileSync('neon', ['deploy'], { cwd: root, stdio: 'pipe', timeout: 180_000 });
28let env: Record<string, string> = {};
29try { env = parse(await readFile(resolve(root, '.env.local'))); } catch {}
30const keyResult = state.apiKey;
31const apiKey = keyResult.key ?? keyResult.api_key?.key ?? keyResult.api_key ?? keyResult.token;
32if (typeof apiKey !== 'string') throw new Error(`API key response needs review: ${Object.keys(keyResult).join(', ')}`);
33env.NEON_ORG_ID = orgId; env.NEON_API_KEY = apiKey;
34const managedAuth = cli(['neon-auth', 'status']);
35env.NEON_AUTH_BASE_URL = managedAuth.base_url;
36env.NEON_AUTH_JWKS_URL = managedAuth.jwks_url;
37env.GLOW_SIGNIN_DOMAINS ??= 'databricks.com';
38env.GLOW_API_URL ??= 'http://localhost:3001'; env.GLOW_WEB_URL ??= 'http://localhost:3000'; env.GLOW_WORKER_URL ??= 'http://localhost:3002';
39env.GLOW_MODEL ??= 'claude-sonnet-4-6';
40env.GLOW_SWEEP_SECRET ??= randomBytes(32).toString('base64url');
41env.GLOW_SIGNUP_ALLOWLIST ??= 'you@example.com,friend@example.com';
42if (!env.GLOW_SIGNING_KEY) {
43 const keys = generateKeyPairSync('ed25519');
44 env.GLOW_SIGNING_KEY = keys.privateKey.export({ type: 'pkcs8', format: 'pem' }).toString();
45 env.GLOW_VERIFY_KEY = keys.publicKey.export({ type: 'spki', format: 'pem' }).toString();
46}
47await writeFile(resolve(root, '.env.local'), Object.entries(env).map(([key, value]) => `${key}=${JSON.stringify(value)}`).join('\n') + '\n', { mode: 0o600 });
48console.log(`Configured App project ${project.id}. Credentials were saved only in ignored local files.`);