Skip to content

Commit 702acdc

Browse files
- JSON prettify when the state definition is a JSON object instead of raw string
1 parent 475dfe3 commit 702acdc

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

lib/deploy/stepFunctions/compileStateMachines.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ module.exports = {
1212
let DependsOn;
1313

1414
if (stateMachineObj.definition) {
15-
DefinitionString = JSON.stringify(stateMachineObj.definition);
15+
if (typeof stateMachineObj.definition === 'string') {
16+
DefinitionString = JSON.stringify(stateMachineObj.definition).replace(/\\n|\\r|\\n\\r/g, '');
17+
} else {
18+
DefinitionString = JSON.stringify(stateMachineObj.definition, undefined, 2);
19+
}
1620
} else {
1721
const errorMessage = [
1822
`Missing "definition" property in stateMachine ${stateMachineName}`,
@@ -25,7 +29,7 @@ module.exports = {
2529
if (stateMachineObj.role) {
2630
if (typeof stateMachineObj.role === 'string') {
2731
if (stateMachineObj.role.startsWith('arn:aws')) {
28-
RoleArn = `"${stateMachineObj.role}"`;
32+
RoleArn = stateMachineObj.role;
2933
} else {
3034
const errorMessage = [
3135
`role property in stateMachine "${stateMachineName}" is not ARN`,
@@ -43,29 +47,31 @@ module.exports = {
4347
.Error(errorMessage);
4448
}
4549
} else {
46-
RoleArn = '{ "Fn::GetAtt": ["IamRoleStateMachineExecution", "Arn"] }';
50+
RoleArn = {
51+
'Fn::GetAtt': [
52+
'IamRoleStateMachineExecution',
53+
'Arn',
54+
],
55+
};
4756
DependsOn = 'IamRoleStateMachineExecution';
4857
}
4958

5059
const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName,
5160
stateMachineObj);
5261
const stateMachineOutputLogicalId = this
53-
.getStateMachineOutputLogicalId(stateMachineName, stateMachineObj);
54-
55-
const stateMachineTemplate = `
62+
.getStateMachineOutputLogicalId(stateMachineName, stateMachineObj);
63+
const stateMachineTemplate =
5664
{
57-
"Type": "AWS::StepFunctions::StateMachine",
58-
"Properties": {
59-
"DefinitionString": ${JSON.stringify(DefinitionString
60-
.replace(/\\n|\\r|\\n\\r/g, ''))},
61-
"RoleArn": ${RoleArn}
62-
}
63-
${DependsOn ? `,"DependsOn": "${DependsOn}"` : ''}
64-
}
65-
`;
65+
Type: 'AWS::StepFunctions::StateMachine',
66+
Properties: {
67+
DefinitionString,
68+
RoleArn,
69+
},
70+
DependsOn,
71+
};
6672

6773
const newStateMachineObject = {
68-
[stateMachineLogicalId]: JSON.parse(stateMachineTemplate),
74+
[stateMachineLogicalId]: stateMachineTemplate,
6975
};
7076

7177
if (stateMachineObj.name) {

lib/deploy/stepFunctions/compileStateMachines.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,40 @@ describe('#compileStateMachines', () => {
330330
.provider.compiledCloudFormationTemplate.Resources
331331
).to.deep.equal({});
332332
});
333+
334+
it('should print pretty JSON for the state machine definition', () => {
335+
const definition = {
336+
Comment: 'Hello World',
337+
StartAt: 'HelloWorld',
338+
States: {
339+
HelloWorld: {
340+
Type: 'Task',
341+
Resource: 'arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:hello',
342+
End: true,
343+
},
344+
},
345+
};
346+
347+
serverless.service.stepFunctions = {
348+
stateMachines: {
349+
myStateMachine1: {
350+
name: 'stateMachineBeta1',
351+
definition,
352+
},
353+
},
354+
};
355+
356+
serverlessStepFunctions.compileStateMachines();
357+
const actual = serverlessStepFunctions
358+
.serverless
359+
.service
360+
.provider
361+
.compiledCloudFormationTemplate
362+
.Resources
363+
.StateMachineBeta1
364+
.Properties
365+
.DefinitionString;
366+
367+
expect(actual).to.equal(JSON.stringify(definition, undefined, 2));
368+
});
333369
});

0 commit comments

Comments
 (0)