|
| 1 | +// dev: starts functions and job-service as local Node processes |
| 2 | +// with env vars pointing to Docker infrastructure services. |
| 3 | +// |
| 4 | +// Usage: |
| 5 | +// node --experimental-strip-types scripts/dev.ts |
| 6 | +// node --experimental-strip-types scripts/dev.ts --only=send-email-link |
| 7 | + |
| 8 | +const path = require('path') as typeof import('path'); |
| 9 | +const { spawn } = require('child_process') as typeof import('child_process'); |
| 10 | +const { ChildProcess } = require('child_process') as typeof import('child_process'); |
| 11 | + |
| 12 | +const ROOT: string = process.cwd(); |
| 13 | + |
| 14 | +// --- Shared env vars for all processes --- |
| 15 | + |
| 16 | +const sharedEnv: Record<string, string> = { |
| 17 | + ...process.env as Record<string, string>, |
| 18 | + NODE_ENV: 'development', |
| 19 | + LOG_LEVEL: 'info', |
| 20 | + // Postgres (matches docker-compose postgres service) |
| 21 | + PGHOST: 'localhost', |
| 22 | + PGPORT: '5432', |
| 23 | + PGUSER: 'postgres', |
| 24 | + PGPASSWORD: 'password', |
| 25 | + PGDATABASE: 'constructive', |
| 26 | + // GraphQL (matches docker-compose graphql-server on port 3002) |
| 27 | + GRAPHQL_URL: 'http://localhost:3002/graphql', |
| 28 | + META_GRAPHQL_URL: 'http://localhost:3002/graphql', |
| 29 | + GRAPHQL_API_NAME: 'private', |
| 30 | + DEFAULT_DATABASE_ID: 'dbe', |
| 31 | + // Mailpit SMTP (matches docker-compose mailpit on port 1025) |
| 32 | + MAILGUN_API_KEY: 'test-key', |
| 33 | + MAILGUN_KEY: 'test-key', |
| 34 | + MAILGUN_DOMAIN: 'mg.constructive.io', |
| 35 | + MAILGUN_FROM: 'no-reply@mg.constructive.io', |
| 36 | + MAILGUN_REPLY: 'info@mg.constructive.io', |
| 37 | + SMTP_HOST: 'localhost', |
| 38 | + SMTP_PORT: '1025', |
| 39 | + LOCAL_APP_PORT: '3000', |
| 40 | + SEND_EMAIL_LINK_DRY_RUN: 'true', |
| 41 | +}; |
| 42 | + |
| 43 | +// --- Process definitions --- |
| 44 | + |
| 45 | +interface ProcessDef { |
| 46 | + name: string; |
| 47 | + script: string; |
| 48 | + port: number; |
| 49 | +} |
| 50 | + |
| 51 | +const allProcesses: ProcessDef[] = [ |
| 52 | + { |
| 53 | + name: 'job-service', |
| 54 | + script: path.resolve(ROOT, 'job/service/dist/run.js'), |
| 55 | + port: 8080, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: 'simple-email', |
| 59 | + script: path.resolve(ROOT, 'generated/simple-email/dist/index.js'), |
| 60 | + port: 8081, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: 'send-email-link', |
| 64 | + script: path.resolve(ROOT, 'generated/send-email-link/dist/index.js'), |
| 65 | + port: 8082, |
| 66 | + }, |
| 67 | +]; |
| 68 | + |
| 69 | +// --- CLI args --- |
| 70 | + |
| 71 | +const args = process.argv.slice(2); |
| 72 | +const onlyArg = args.find((a: string) => a.startsWith('--only=')); |
| 73 | +const onlyName: string | undefined = onlyArg?.split('=')[1]; |
| 74 | + |
| 75 | +// --- Job-service specific env --- |
| 76 | + |
| 77 | +function getJobServiceEnv(): Record<string, string> { |
| 78 | + return { |
| 79 | + JOBS_SCHEMA: 'app_jobs', |
| 80 | + JOBS_SUPPORT_ANY: 'false', |
| 81 | + JOBS_SUPPORTED: 'send-email-link', |
| 82 | + HOSTNAME: 'knative-job-service-local', |
| 83 | + INTERNAL_JOBS_CALLBACK_PORT: '8080', |
| 84 | + INTERNAL_JOBS_CALLBACK_URL: 'http://localhost:8080/callback', |
| 85 | + JOBS_CALLBACK_HOST: 'localhost', |
| 86 | + INTERNAL_GATEWAY_URL: 'http://localhost:8082', |
| 87 | + INTERNAL_GATEWAY_DEVELOPMENT_MAP: JSON.stringify({ |
| 88 | + 'send-email-link': 'http://localhost:8082', |
| 89 | + 'simple-email': 'http://localhost:8081', |
| 90 | + }), |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +// --- Main --- |
| 95 | + |
| 96 | +const children: ReturnType<typeof spawn>[] = []; |
| 97 | + |
| 98 | +function startProcess(def: ProcessDef): void { |
| 99 | + const extraEnv = def.name === 'job-service' ? getJobServiceEnv() : {}; |
| 100 | + const env = { ...sharedEnv, ...extraEnv, PORT: String(def.port) }; |
| 101 | + |
| 102 | + console.log(`Starting ${def.name} on port ${def.port}...`); |
| 103 | + |
| 104 | + const child = spawn('node', [def.script], { |
| 105 | + env, |
| 106 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 107 | + cwd: ROOT, |
| 108 | + }); |
| 109 | + |
| 110 | + child.stdout?.on('data', (data: Buffer) => { |
| 111 | + for (const line of data.toString().trimEnd().split('\n')) { |
| 112 | + console.log(`[${def.name}] ${line}`); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + child.stderr?.on('data', (data: Buffer) => { |
| 117 | + for (const line of data.toString().trimEnd().split('\n')) { |
| 118 | + console.error(`[${def.name}] ${line}`); |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + child.on('exit', (code: number | null) => { |
| 123 | + console.log(`[${def.name}] exited with code ${code}`); |
| 124 | + }); |
| 125 | + |
| 126 | + children.push(child); |
| 127 | +} |
| 128 | + |
| 129 | +function shutdown(): void { |
| 130 | + console.log('\nShutting down...'); |
| 131 | + for (const child of children) { |
| 132 | + child.kill('SIGTERM'); |
| 133 | + } |
| 134 | + process.exit(0); |
| 135 | +} |
| 136 | + |
| 137 | +process.on('SIGINT', shutdown); |
| 138 | +process.on('SIGTERM', shutdown); |
| 139 | + |
| 140 | +const processes = onlyName |
| 141 | + ? allProcesses.filter((p) => p.name === onlyName) |
| 142 | + : allProcesses; |
| 143 | + |
| 144 | +if (processes.length === 0) { |
| 145 | + console.error(onlyName ? `Unknown process "${onlyName}".` : 'No processes to start.'); |
| 146 | + console.log('Available:', allProcesses.map((p) => p.name).join(', ')); |
| 147 | + process.exit(1); |
| 148 | +} |
| 149 | + |
| 150 | +console.log(`Starting: ${processes.map((p) => `${p.name} (:${p.port})`).join(', ')}`); |
| 151 | + |
| 152 | +for (const def of processes) { |
| 153 | + startProcess(def); |
| 154 | +} |
0 commit comments