Skip to content

Commit a65c3dc

Browse files
authored
Merge pull request #144 from theburningmonk/feature/json_prettify
JSON prettify when the state definition is a JSON object instead of raw string
2 parents 475dfe3 + 0540e26 commit a65c3dc

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

lib/deploy/stepFunctions/compileStateMachines.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ 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)
17+
.replace(/\\n|\\r|\\n\\r/g, '');
18+
} else {
19+
DefinitionString = JSON.stringify(stateMachineObj.definition, undefined, 2);
20+
}
1621
} else {
1722
const errorMessage = [
1823
`Missing "definition" property in stateMachine ${stateMachineName}`,
@@ -25,7 +30,7 @@ module.exports = {
2530
if (stateMachineObj.role) {
2631
if (typeof stateMachineObj.role === 'string') {
2732
if (stateMachineObj.role.startsWith('arn:aws')) {
28-
RoleArn = `"${stateMachineObj.role}"`;
33+
RoleArn = stateMachineObj.role;
2934
} else {
3035
const errorMessage = [
3136
`role property in stateMachine "${stateMachineName}" is not ARN`,
@@ -43,29 +48,31 @@ module.exports = {
4348
.Error(errorMessage);
4449
}
4550
} else {
46-
RoleArn = '{ "Fn::GetAtt": ["IamRoleStateMachineExecution", "Arn"] }';
51+
RoleArn = {
52+
'Fn::GetAtt': [
53+
'IamRoleStateMachineExecution',
54+
'Arn',
55+
],
56+
};
4757
DependsOn = 'IamRoleStateMachineExecution';
4858
}
4959

5060
const stateMachineLogicalId = this.getStateMachineLogicalId(stateMachineName,
5161
stateMachineObj);
5262
const stateMachineOutputLogicalId = this
53-
.getStateMachineOutputLogicalId(stateMachineName, stateMachineObj);
54-
55-
const stateMachineTemplate = `
63+
.getStateMachineOutputLogicalId(stateMachineName, stateMachineObj);
64+
const stateMachineTemplate =
5665
{
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-
`;
66+
Type: 'AWS::StepFunctions::StateMachine',
67+
Properties: {
68+
DefinitionString,
69+
RoleArn,
70+
},
71+
DependsOn,
72+
};
6673

6774
const newStateMachineObject = {
68-
[stateMachineLogicalId]: JSON.parse(stateMachineTemplate),
75+
[stateMachineLogicalId]: stateMachineTemplate,
6976
};
7077

7178
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)