Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions lib/deploy/stepFunctions/compileStateMachines.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ module.exports = {
let DependsOn;

if (stateMachineObj.definition) {
DefinitionString = JSON.stringify(stateMachineObj.definition);
if (typeof stateMachineObj.definition === 'string') {
DefinitionString = JSON.stringify(stateMachineObj.definition)
.replace(/\\n|\\r|\\n\\r/g, '');
} else {
DefinitionString = JSON.stringify(stateMachineObj.definition, undefined, 2);
}
} else {
const errorMessage = [
`Missing "definition" property in stateMachine ${stateMachineName}`,
Expand All @@ -25,7 +30,7 @@ module.exports = {
if (stateMachineObj.role) {
if (typeof stateMachineObj.role === 'string') {
if (stateMachineObj.role.startsWith('arn:aws')) {
RoleArn = `"${stateMachineObj.role}"`;
RoleArn = stateMachineObj.role;
} else {
const errorMessage = [
`role property in stateMachine "${stateMachineName}" is not ARN`,
Expand All @@ -43,29 +48,31 @@ module.exports = {
.Error(errorMessage);
}
} else {
RoleArn = '{ "Fn::GetAtt": ["IamRoleStateMachineExecution", "Arn"] }';
RoleArn = {
'Fn::GetAtt': [
'IamRoleStateMachineExecution',
'Arn',
],
};
DependsOn = 'IamRoleStateMachineExecution';
}

const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName,
stateMachineObj);
const stateMachineOutputLogicalId = this
.getStateMachineOutputLogicalId(stateMachineName, stateMachineObj);

const stateMachineTemplate = `
.getStateMachineOutputLogicalId(stateMachineName, stateMachineObj);
const stateMachineTemplate =
{
"Type": "AWS::StepFunctions::StateMachine",
"Properties": {
"DefinitionString": ${JSON.stringify(DefinitionString
.replace(/\\n|\\r|\\n\\r/g, ''))},
"RoleArn": ${RoleArn}
}
${DependsOn ? `,"DependsOn": "${DependsOn}"` : ''}
}
`;
Type: 'AWS::StepFunctions::StateMachine',
Properties: {
DefinitionString,
RoleArn,
},
DependsOn,
};

const newStateMachineObject = {
[stateMachineLogicalId]: JSON.parse(stateMachineTemplate),
[stateMachineLogicalId]: stateMachineTemplate,
};

if (stateMachineObj.name) {
Expand Down
36 changes: 36 additions & 0 deletions lib/deploy/stepFunctions/compileStateMachines.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,40 @@ describe('#compileStateMachines', () => {
.provider.compiledCloudFormationTemplate.Resources
).to.deep.equal({});
});

it('should print pretty JSON for the state machine definition', () => {
const definition = {
Comment: 'Hello World',
StartAt: 'HelloWorld',
States: {
HelloWorld: {
Type: 'Task',
Resource: 'arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:hello',
End: true,
},
},
};

serverless.service.stepFunctions = {
stateMachines: {
myStateMachine1: {
name: 'stateMachineBeta1',
definition,
},
},
};

serverlessStepFunctions.compileStateMachines();
const actual = serverlessStepFunctions
.serverless
.service
.provider
.compiledCloudFormationTemplate
.Resources
.StateMachineBeta1
.Properties
.DefinitionString;

expect(actual).to.equal(JSON.stringify(definition, undefined, 2));
});
});