Files
lifeforge/tools/src/commands/dev/functions/startServices.ts

74 lines
1.8 KiB
TypeScript

import chalk from 'chalk'
import concurrently from 'concurrently'
import { PROJECTS } from '@/commands/project/constants/projects'
import executeCommand from '@/utils/commands'
import { getEnvVars } from '@/utils/helpers'
import logger from '@/utils/logger'
import { SERVICE_COMMANDS } from '../config/commands'
import getConcurrentServices from './getConcurrentServices'
/**
* Starts a single service based on its configuration
*/
export async function startSingleService(
service: string,
extraArgs: string[] = []
): Promise<void> {
// Handle core services
if (service in SERVICE_COMMANDS) {
const config = SERVICE_COMMANDS[service]
if (config.requiresEnv) {
getEnvVars(config.requiresEnv)
}
const command =
config.command instanceof Function
? await config.command()
: config.command
const cwd = config.cwd instanceof Function ? config.cwd() : config.cwd
logger.debug(`Current Working Directory: ${chalk.blue(cwd)}`)
executeCommand(command, { cwd, stdio: 'inherit' }, extraArgs)
return
}
// Handle tool services
if (service in PROJECTS) {
const projectPath = PROJECTS[service as keyof typeof PROJECTS]
executeCommand(`cd ${projectPath} && bun run dev`, {}, extraArgs)
return
}
throw new Error(`Unknown service: ${service}`)
}
/**
* Starts all development services concurrently
*/
export async function startAllServices(): Promise<void> {
try {
const concurrentServices = await getConcurrentServices()
const { result } = concurrently(concurrentServices, {
killOthersOn: ['failure', 'success'],
restartTries: 0,
prefix: 'name',
prefixColors: ['cyan', 'green', 'magenta']
})
await result
} catch (error) {
logger.error('Failed to start all services')
logger.debug(`Error details: ${error}`)
process.exit(1)
}
}