77 CUSTOM_OPTION ,
88 defaultOptions ,
99 SERVER_SHUTDOWN_TIMEOUT ,
10+ supportedRuntimesOnlyWithDocker ,
1011} from './config/index.js'
1112import pkg from '../package.json'
1213
@@ -16,6 +17,7 @@ export default class ServerlessOffline {
1617 this . _schedule = null
1718 this . _webSocket = null
1819 this . _lambda = null
20+ this . _docker = null
1921
2022 this . _cliOptions = cliOptions
2123 this . _serverless = serverless
@@ -68,6 +70,10 @@ export default class ServerlessOffline {
6870 webSocketEvents,
6971 } = this . _getEvents ( )
7072
73+ if ( this . _checkDockerIsRequired ( lambdas ) ) {
74+ await this . _createDocker ( lambdas )
75+ }
76+
7177 // if (lambdas.length > 0) {
7278 await this . _createLambda ( lambdas )
7379 // }
@@ -122,6 +128,10 @@ export default class ServerlessOffline {
122128
123129 await Promise . all ( eventModules )
124130
131+ if ( this . _docker ) {
132+ await this . _docker . cleanup ( )
133+ }
134+
125135 if ( ! skipExit ) {
126136 process . exit ( 0 )
127137 }
@@ -152,10 +162,18 @@ export default class ServerlessOffline {
152162 serverlessLog ( `Got ${ command } signal. Offline Halting...` )
153163 }
154164
165+ async _createDocker ( lambdas ) {
166+ const { default : Docker } = await import ( './docker/index.js' )
167+
168+ this . _docker = new Docker ( this . _serverless , this . _options , lambdas )
169+
170+ return this . _docker . initialize ( )
171+ }
172+
155173 async _createLambda ( lambdas , skipStart ) {
156174 const { default : Lambda } = await import ( './lambda/index.js' )
157175
158- this . _lambda = new Lambda ( this . _serverless , this . _options )
176+ this . _lambda = new Lambda ( this . _serverless , this . _options , this . _docker )
159177
160178 lambdas . forEach ( ( { functionKey, functionDefinition } ) => {
161179 this . _lambda . add ( functionKey , functionDefinition )
@@ -343,4 +361,19 @@ export default class ServerlessOffline {
343361 )
344362 }
345363 }
364+
365+ _checkDockerIsRequired ( lambdas ) {
366+ if ( this . _options . useDocker ) {
367+ return true
368+ }
369+
370+ const { service } = this . _serverless
371+ if ( supportedRuntimesOnlyWithDocker . has ( service . provider . runtime ) ) {
372+ return true
373+ }
374+
375+ return lambdas . some ( ( { functionDefinition } ) => {
376+ return supportedRuntimesOnlyWithDocker . has ( functionDefinition . runtime )
377+ } )
378+ }
346379}
0 commit comments