Skip to content

Commit c114ccc

Browse files
committed
Support docker
1 parent 64ac5df commit c114ccc

20 files changed

Lines changed: 612 additions & 80 deletions

package-lock.json

Lines changed: 70 additions & 61 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,18 @@
151151
"chalk": "^3.0.0",
152152
"cuid": "^2.1.6",
153153
"execa": "^3.4.0",
154+
"fs-extra": "^8.1.0",
154155
"hapi-swagger": "^11.0.1",
155156
"js-string-escape": "^1.0.1",
156157
"jsonpath-plus": "^2.0.0",
157158
"jsonwebtoken": "^8.5.1",
159+
"jszip": "^3.2.2",
158160
"luxon": "^1.21.3",
159161
"node-fetch": "^2.6.0",
160162
"node-schedule": "^1.3.2",
161163
"object.fromentries": "^2.0.1",
162164
"please-upgrade-node": "^3.2.0",
165+
"portfinder": "^1.0.25",
163166
"semver": "^6.3.0",
164167
"update-notifier": "^3.0.1",
165168
"velocityjs": "^2.0.0",

src/ServerlessOffline.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
CUSTOM_OPTION,
88
defaultOptions,
99
SERVER_SHUTDOWN_TIMEOUT,
10+
supportedRuntimesOnlyWithDocker,
1011
} from './config/index.js'
1112
import 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

Comments
 (0)