|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { Command, Flags } from '@oclif/core'; |
| 4 | +import { spawn } from 'child_process'; |
| 5 | +import fs from 'fs'; |
| 6 | +import path from 'path'; |
| 7 | +import { printHeader, printKV, printStep, printError } from '../utils/format.js'; |
| 8 | + |
| 9 | +export default class Start extends Command { |
| 10 | + static override description = 'Serve the pre-compiled artifact in production mode (requires objectstack build first)'; |
| 11 | + |
| 12 | + static override flags = { |
| 13 | + ui: Flags.boolean({ description: 'Enable Studio UI at /_studio/' }), |
| 14 | + verbose: Flags.boolean({ char: 'v', description: 'Verbose output' }), |
| 15 | + port: Flags.integer({ char: 'p', description: 'Port to listen on' }), |
| 16 | + }; |
| 17 | + |
| 18 | + async run(): Promise<void> { |
| 19 | + const { flags } = await this.parse(Start); |
| 20 | + |
| 21 | + printHeader('Production Mode'); |
| 22 | + |
| 23 | + const artifactPath = process.env.OBJECTSTACK_ARTIFACT_PATH |
| 24 | + ?? path.resolve(process.cwd(), 'dist/objectstack.json'); |
| 25 | + |
| 26 | + if (!fs.existsSync(artifactPath)) { |
| 27 | + printError(`Artifact not found: ${path.relative(process.cwd(), artifactPath)}`); |
| 28 | + console.error(' Run \x1b[33mobjectstack build\x1b[0m to compile your configuration first.'); |
| 29 | + process.exit(1); |
| 30 | + } |
| 31 | + |
| 32 | + printKV('Artifact', path.relative(process.cwd(), artifactPath), '📦'); |
| 33 | + printStep('Starting server (production mode)...'); |
| 34 | + |
| 35 | + const localEnv: NodeJS.ProcessEnv = { |
| 36 | + ...process.env, |
| 37 | + NODE_ENV: 'production', |
| 38 | + OBJECTSTACK_PROJECT_ID: process.env.OBJECTSTACK_PROJECT_ID ?? 'proj_local', |
| 39 | + OBJECTSTACK_ARTIFACT_PATH: process.env.OBJECTSTACK_ARTIFACT_PATH ?? artifactPath, |
| 40 | + ...(flags.port ? { PORT: String(flags.port) } : {}), |
| 41 | + }; |
| 42 | + |
| 43 | + if (!process.env.OBJECTSTACK_CLOUD_URL) { |
| 44 | + delete localEnv.OBJECTSTACK_CLOUD_URL; |
| 45 | + } |
| 46 | + |
| 47 | + printKV('Project ID', localEnv.OBJECTSTACK_PROJECT_ID!, '🎯'); |
| 48 | + |
| 49 | + const binPath = process.argv[1]; |
| 50 | + spawn( |
| 51 | + process.execPath, |
| 52 | + [ |
| 53 | + binPath, |
| 54 | + 'serve', |
| 55 | + '--prebuilt', |
| 56 | + ...(flags.ui ? ['--ui'] : []), |
| 57 | + ...(flags.verbose ? ['--verbose'] : []), |
| 58 | + ], |
| 59 | + { stdio: 'inherit', env: localEnv }, |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
0 commit comments