11import { Task , TaskRunContext } from '@dotcom-tool-kit/base'
22import type ServerlessSchema from '../schema'
3- import { spawn } from 'child_process'
3+ import { ChildProcess , spawn } from 'child_process'
44import { DopplerEnvVars } from '@dotcom-tool-kit/doppler'
5- import { hookConsole , hookFork } from '@dotcom-tool-kit/logger'
5+ import { hookConsole , hookFork , waitOnExit } from '@dotcom-tool-kit/logger'
66import getPort from 'get-port'
77import waitPort from 'wait-port'
88import * as z from 'zod'
9+ import { writeState } from '@dotcom-tool-kit/state'
910
1011const ServerlessRunSchema = z
1112 . object ( {
@@ -26,6 +27,8 @@ export default class ServerlessRun extends Task<{
2627 task : typeof ServerlessRunSchema
2728 plugin : typeof ServerlessSchema
2829} > {
30+ child ?: ChildProcess
31+
2932 async run ( { cwd, config } : TaskRunContext ) : Promise < void > {
3033 const { useDoppler, ports } = this . options
3134 const { configPath } = this . pluginOptions
@@ -54,7 +57,7 @@ export default class ServerlessRun extends Task<{
5457 args . push ( '--config' , './serverless.yml' )
5558 }
5659
57- const child = spawn ( 'serverless' , args , {
60+ this . child = spawn ( 'serverless' , args , {
5861 env : {
5962 ...dopplerEnv ,
6063 PORT : port . toString ( ) ,
@@ -63,17 +66,21 @@ export default class ServerlessRun extends Task<{
6366 cwd
6467 } )
6568
66- hookFork ( this . logger , 'serverless' , child )
69+ hookFork ( this . logger , 'serverless' , this . child )
6770
6871 const unhook = hookConsole ( this . logger , 'wait-port' )
6972 try {
7073 await waitPort ( {
7174 host : 'localhost' ,
72- port : port
75+ port
7376 } )
7477 } finally {
7578 unhook ( )
7679 }
80+
81+ writeState ( 'local' , { port } )
82+
83+ await waitOnExit ( 'serverless' , this . child )
7784 }
7885
7986 async stop ( ) {
0 commit comments