-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.js
137 lines (121 loc) · 4.79 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict';
const parse = require('./parse');
const build = require('./build');
const _ = require('lodash');
const path = require('path');
class StepFunctionsOfflinePlugin {
constructor(serverless, options) {
this.location = process.cwd();
this.serverless = serverless;
this.options = options;
this.stateMachine = this.options.stateMachine;
this.detailedLog = this.options.detailedLog || this.options.l;
this.eventFile = this.options.event || this.options.e;
this.functions = this.serverless.service.functions;
this.variables = this.serverless.service.custom.stepFunctionsOffline;
this.cliLog = this.serverless.cli.log.bind(this.serverless.cli);
Object.assign(this,
parse,
build
);
this.commands = {
'step-functions-offline': {
usage: 'Will run your step function locally',
lifecycleEvents: [
'checkVariableInYML',
'start',
'isInstalledPluginSLSStepFunctions',
'findFunctionsPathAndHandler',
'findState',
'loadEventFile',
'loadEnvVariables',
'buildStepWorkFlow'
],
options: {
stateMachine: {
usage: 'The stage used to execute.',
required: true
},
event: {
usage: 'File where is values for execution in JSON format',
shortcut: 'e'
},
detailedLog: {
usage: 'Option which enables detailed logs',
shortcut: 'l'
}
}
}
};
this.hooks = {
'step-functions-offline:start': this.start.bind(this),
'step-functions-offline:isInstalledPluginSLSStepFunctions': this.isInstalledPluginSLSStepFunctions.bind(this),
'step-functions-offline:findState': this.findState.bind(this),
'step-functions-offline:loadEventFile': this.loadEventFile.bind(this),
'step-functions-offline:loadEnvVariables': this.loadEnvVariables.bind(this),
'step-functions-offline:buildStepWorkFlow': this.buildStepWorkFlow.bind(this)
};
}
// Entry point for the plugin (sls step offline)
start() {
this.cliLog('Preparing....');
this._getLocation();
this._checkVersion();
this._checkVariableInYML();
}
_getLocation() {
if (this.options.location) {
this.location = path.join(process.cwd(), this.options.location);
}
if (this.variables && this.variables.location) {
this.location = path.join(process.cwd(), this.variables.location);
}
}
_checkVersion() {
const version = this.serverless.version;
if (!version.startsWith('1.')) {
throw new this.serverless.classes.Error(`Serverless step offline requires Serverless v1.x.x but found ${version}`);
}
}
_checkVariableInYML() {
if (!_.has(this.serverless.service, 'custom.stepFunctionsOffline')) {
throw new this.serverless.classes.Error('Please add ENV_VARIABLES to section "custom"');
}
return;
}
isInstalledPluginSLSStepFunctions() {
const plugins = this.serverless.service.plugins;
if (plugins.indexOf('serverless-step-functions') < 0) {
const error = 'Error: Please install plugin "serverless-step-functions". Package does not work without it';
throw new this.serverless.classes.Error(error);
}
}
loadEventFile() {
if (!this.eventFile) {
return this.eventFile = {};
}
try {
this.eventFile = path.isAbsolute(this.eventFile) ? require(this.eventFile) :
require(path.join(process.cwd(), this.eventFile));
} catch (err) {
throw err;
}
}
loadEnvVariables() {
this.environment = this.serverless.service.provider.environment;
process.env.STEP_IS_OFFLINE = true;
process.env = _.extend(process.env, this.environment);
this.environmentVariables = Object.assign({}, process.env); //store global env variables;
return;
}
findState() {
this.cliLog(`Trying to find state "${this.stateMachine}" in serverless manifest`);
return this.parseConfig()
.then(() => {
this.stateDefinition = this.getStateMachine(this.stateMachine).definition;
}).catch(err => {
throw new this.serverless.classes.Error(err);
});
}
}
module.exports = StepFunctionsOfflinePlugin;