|
| 1 | +const path = require('path'); |
| 2 | +const StepFunctionsLocal = require('stepfunctions-localhost'); |
| 3 | +const AWS = require('aws-sdk'); |
| 4 | +const tcpPortUsed = require('tcp-port-used'); |
| 5 | + |
| 6 | +class ServerlessStepFunctionsLocal { |
| 7 | + constructor(serverless, options) { |
| 8 | + this.serverless = serverless; |
| 9 | + this.service = serverless.service; |
| 10 | + this.options = options; |
| 11 | + |
| 12 | + this.log = serverless.cli.log.bind(serverless.cli); |
| 13 | + this.config = (this.service.custom && this.service.custom.stepFunctionsLocal) || {}; |
| 14 | + |
| 15 | + // Check config |
| 16 | + if (!this.config.accountId) { |
| 17 | + throw new Error('Step Functions Local: missing accountId'); |
| 18 | + } |
| 19 | + |
| 20 | + if (!this.config.region) { |
| 21 | + throw new Error('Step Functions Local: missing region'); |
| 22 | + } |
| 23 | + |
| 24 | + if (!this.config.lambdaEndpoint) { |
| 25 | + this.config.lambdaEndpoint = 'http://localhost:4000'; |
| 26 | + } |
| 27 | + |
| 28 | + if (!this.config.path) { |
| 29 | + this.config.path = './.step-functions-local'; |
| 30 | + } |
| 31 | + |
| 32 | + this.stepfunctionsServer = new StepFunctionsLocal(this.config); |
| 33 | + |
| 34 | + this.stepfunctionsAPI = new AWS.StepFunctions({endpoint: 'http://localhost:8083', region: this.config.region}); |
| 35 | + |
| 36 | + this.hooks = { |
| 37 | + 'offline:start:init': async () => { |
| 38 | + await this.installStepFunctions(); |
| 39 | + await this.startStepFunctions(); |
| 40 | + await this.getStepFunctionsFromConfig(); |
| 41 | + await this.createEndpoints(); |
| 42 | + }, |
| 43 | + 'before:offline:start:end': async () => { |
| 44 | + await this.stopStepFunctions(); |
| 45 | + } |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + installStepFunctions() { |
| 50 | + return this.stepfunctionsServer.install(); |
| 51 | + } |
| 52 | + |
| 53 | + async startStepFunctions() { |
| 54 | + this.stepfunctionsServer.start({ |
| 55 | + account: this.config.accountId.toString(), |
| 56 | + lambdaEndpoint: this.config.lambdaEndpoint |
| 57 | + }); |
| 58 | + |
| 59 | + // Wait for server to start |
| 60 | + await tcpPortUsed.waitUntilUsed(8083, 200, 10000); |
| 61 | + } |
| 62 | + |
| 63 | + stopStepFunctions() { |
| 64 | + return this.stepfunctionsServer.stop(); |
| 65 | + } |
| 66 | + |
| 67 | + async getStepFunctionsFromConfig() { |
| 68 | + const {servicePath} = this.serverless.config; |
| 69 | + |
| 70 | + if (!servicePath) { |
| 71 | + throw new Error('service path not found'); |
| 72 | + } |
| 73 | + |
| 74 | + const configPath = path.join(servicePath, 'serverless.yml'); |
| 75 | + |
| 76 | + const parsed = await this.serverless.yamlParser.parse(configPath); |
| 77 | + |
| 78 | + this.stateMachines = parsed.stepFunctions.stateMachines; |
| 79 | + } |
| 80 | + |
| 81 | + createEndpoints() { |
| 82 | + return Promise.all(Object.keys(this.stateMachines).map(stateMachineName => this.stepfunctionsAPI.createStateMachine({ |
| 83 | + definition: JSON.stringify(this.stateMachines[stateMachineName].definition), |
| 84 | + name: stateMachineName, |
| 85 | + roleArn: `arn:aws:iam::${this.config.accountId}:role/DummyRole` |
| 86 | + }).promise() |
| 87 | + )); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +module.exports = ServerlessStepFunctionsLocal; |
0 commit comments