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