|
1 | 1 | 'use strict'; |
| 2 | +const BbPromise = require('bluebird'); |
| 3 | +const path = require('path'); |
| 4 | +const _ = require('lodash'); |
2 | 5 |
|
3 | 6 | class AwsStepFunctionsDeploy { |
4 | 7 | constructor(serverless, options) { |
5 | 8 | this.serverless = serverless; |
6 | 9 | this.options = options; |
| 10 | + this.provider = this.serverless.getProvider('aws'); |
| 11 | + this.awsStateLanguage = {}; |
7 | 12 |
|
8 | 13 | this.hooks = { |
9 | | - 'deploy:deploy': this.deploy.bind(this), |
| 14 | + 'before:deploy:deploy': this.action.bind(this), |
10 | 15 | }; |
11 | 16 | } |
12 | 17 |
|
13 | | - deploy() { |
| 18 | + action() { |
14 | 19 | this.serverless.cli.consoleLog('Start Deploy Step Functions'); |
| 20 | + BbPromise.bind(this) |
| 21 | + .then(this.yamlParse) |
| 22 | + .then(this.compile) |
| 23 | + .then(this.deploy); |
15 | 24 | } |
| 25 | + |
| 26 | + yamlParse() { |
| 27 | + const servicePath = this.serverless.config.servicePath; |
| 28 | + |
| 29 | + if (!servicePath) { |
| 30 | + return BbPromise.resolve(); |
| 31 | + } |
| 32 | + |
| 33 | + let serverlessYmlPath = path.join(servicePath, 'serverless.yml'); |
| 34 | + if (!this.serverless.utils.fileExistsSync(serverlessYmlPath)) { |
| 35 | + serverlessYmlPath = path |
| 36 | + .join(this.serverless.config.servicePath, 'serverless.yaml'); |
| 37 | + } |
| 38 | + |
| 39 | + return this.serverless.yamlParser |
| 40 | + .parse(serverlessYmlPath) |
| 41 | + .then((serverlessFileParam) => { |
| 42 | + this.stepFunctions = serverlessFileParam.stepFunctions; |
| 43 | + return BbPromise.resolve(); |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + compile() { |
| 48 | + if (!this.stepFunctions) { |
| 49 | + return BbPromise.resolve(); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + _.forEach(this.stepFunctions, function (stateMachine, stateMachineName) { |
| 54 | + if (!stateMachine.start) { |
| 55 | + const errorMessage = [ |
| 56 | + ' Please set `start` in stepFunctions statements in serverless.yml .', |
| 57 | + ].join(''); |
| 58 | + throw new this.serverless.classes |
| 59 | + .Error(errorMessage); |
| 60 | + } |
| 61 | + |
| 62 | + //@todo error handling to not stateMachine.states |
| 63 | + |
| 64 | + const StartAt = stateMachine.start; |
| 65 | + const Comment = stateMachine.comment; |
| 66 | + this.awsStateLanguage[stateMachineName] = { |
| 67 | + Comment: Comment || 'Step Functions Generated by Serverless Step Functions.', |
| 68 | + StartAt, |
| 69 | + States: {}, |
| 70 | + }; |
| 71 | + |
| 72 | + _.forEach(stateMachine.states, function (state) { |
| 73 | + //@todo error handling to not type and resource |
| 74 | + const Type = state.type; |
| 75 | + const Resource = state.resource; |
| 76 | + this.awsStateLanguage[stateMachineName].States = { |
| 77 | + Type, |
| 78 | + Resource, |
| 79 | + End: 'true' |
| 80 | + } |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + return BbPromise.resolve(); |
| 85 | + } |
| 86 | + |
| 87 | + deploy() { |
| 88 | + return this.provider.request('StepFunctions', |
| 89 | + 'createStateMachine', |
| 90 | + { |
| 91 | + definition: 'STRING_VALUE', |
| 92 | + name: 'STRING_VALUE', |
| 93 | + roleArn: 'STRING_VALUE' |
| 94 | + }, |
| 95 | + this.options.stage, |
| 96 | + this.options.region); |
| 97 | + |
| 98 | + } |
| 99 | + |
16 | 100 | } |
17 | 101 | module.exports = AwsStepFunctionsDeploy; |
0 commit comments