Skip to content

Commit ad17820

Browse files
feat(serverless): optionally wait for run task to exit
1 parent 2f47c00 commit ad17820

1 file changed

Lines changed: 31 additions & 3 deletions

File tree

  • plugins/serverless/src/tasks

plugins/serverless/src/tasks/run.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { Task, TaskRunContext } from '@dotcom-tool-kit/base'
22
import type ServerlessSchema from '../schema'
33
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, styles as s } 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({
@@ -17,7 +18,18 @@ const ServerlessRunSchema = z
1718
useDoppler: z
1819
.boolean()
1920
.default(true)
20-
.describe('run the application with environment variables from Doppler')
21+
.describe('run the application with environment variables from Doppler'),
22+
background: z
23+
.boolean()
24+
.optional()
25+
.transform((background) => ({
26+
isDefault: typeof background === 'undefined',
27+
value: typeof background === 'undefined' ? true : background
28+
}))
29+
.default(true) // will never be reached because of the transform; here for the documentation generator's benefit
30+
.describe(
31+
"run the `serverless oflfine` process in the background, i.e. don't wait for it to exit before continuing to other Tool Kit tasks. set to `false` to wait for the process to exit, useful for running [multiple Tool Kit tasks in parallel](../parallel)."
32+
)
2133
})
2234
.describe('Run serverless functions locally')
2335
export { ServerlessRunSchema as schema }
@@ -71,11 +83,27 @@ export default class ServerlessRun extends Task<{
7183
try {
7284
await waitPort({
7385
host: 'localhost',
74-
port: port
86+
port
7587
})
7688
} finally {
7789
unhook()
7890
}
91+
92+
writeState('local', { port })
93+
94+
if (this.options.background.isDefault) {
95+
this.logger.warn(
96+
`${s.task('ServerlessRun')} ${s.option(
97+
'options.background'
98+
)} is not set; falling back to the legacy behaviour of running the process in the background. This will be removed in a future major version of ${s.plugin(
99+
'@dotcom-tool-kit/serverless'
100+
)}.`
101+
)
102+
}
103+
104+
if (!this.options.background.value) {
105+
await waitOnExit('serverless', this.child)
106+
}
79107
}
80108

81109
async stop() {

0 commit comments

Comments
 (0)