Skip to content

Commit 397c1c3

Browse files
feat(serverless)!: wait for run task to exit
1 parent fc479a8 commit 397c1c3

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

  • plugins/serverless/src/tasks

plugins/serverless/src/tasks/run.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Task, TaskRunContext } from '@dotcom-tool-kit/base'
22
import type ServerlessSchema from '../schema'
3-
import { spawn } from 'child_process'
3+
import { ChildProcess, spawn } from 'child_process'
44
import { 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'
66
import getPort from 'get-port'
77
import waitPort from 'wait-port'
88
import * as z from 'zod'
9+
import { writeState } from '@dotcom-tool-kit/state'
910

1011
const 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

Comments
 (0)