import * as fs from 'fs'; import { resolve as pathResolve } from 'path'; import { spawnSync } from 'child_process'; import { getBuildPath } from '@erect/core/paths'; import { config } from '@erect/core/config'; import { build } from './build.handler'; import { cliStartHook } from '../hooks'; cliStartHook.addAction('default', async () => { const clientBuildPath = pathResolve(getBuildPath(), config.bundles.server.buildOutputDir); if (!fs.existsSync(clientBuildPath)) { await build({ target: 'client', optimize: true }); } const compiledFiles: string[] = [ pathResolve( getBuildPath(), config.bundles.server.buildOutputDir, 'index.js', ), ]; const args = [ '-r', 'tsconfig-paths/register', ]; const files = compiledFiles.slice(0); while (files.length > 0) { const compiledFile = files.shift(); if (compiledFile != null) { if (files.length > 0) { args.push('-r'); } args.push(compiledFile); } } args.push('--color'); spawnSync('node', args, { stdio: 'inherit', env: { PATH: process.env.PATH, NODE_ENV: 'production', ...(process.env.ERECT_DIR != null && { ERECT_DIR: process.env.ERECT_DIR, }), ERECT_COMMAND: 'start', }, }); });