From 702acdc56236430534028c731a1154b996996c2a Mon Sep 17 00:00:00 2001 From: Yan Cui Date: Thu, 23 Aug 2018 16:04:50 +0100 Subject: [PATCH 1/2] - JSON prettify when the state definition is a JSON object instead of raw string --- .../stepFunctions/compileStateMachines.js | 38 +++++++++++-------- .../compileStateMachines.test.js | 36 ++++++++++++++++++ 2 files changed, 58 insertions(+), 16 deletions(-) diff --git a/lib/deploy/stepFunctions/compileStateMachines.js b/lib/deploy/stepFunctions/compileStateMachines.js index 165f5daa..0e6e4b55 100644 --- a/lib/deploy/stepFunctions/compileStateMachines.js +++ b/lib/deploy/stepFunctions/compileStateMachines.js @@ -12,7 +12,11 @@ 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}`, @@ -25,7 +29,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`, @@ -43,29 +47,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) { diff --git a/lib/deploy/stepFunctions/compileStateMachines.test.js b/lib/deploy/stepFunctions/compileStateMachines.test.js index 14662e96..3630f18f 100644 --- a/lib/deploy/stepFunctions/compileStateMachines.test.js +++ b/lib/deploy/stepFunctions/compileStateMachines.test.js @@ -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)); + }); }); From 0540e26a18efb47c864110bf38197b88673f2733 Mon Sep 17 00:00:00 2001 From: Yan Cui Date: Thu, 23 Aug 2018 16:10:01 +0100 Subject: [PATCH 2/2] - fixed linting error --- lib/deploy/stepFunctions/compileStateMachines.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/deploy/stepFunctions/compileStateMachines.js b/lib/deploy/stepFunctions/compileStateMachines.js index 0e6e4b55..0b63986d 100644 --- a/lib/deploy/stepFunctions/compileStateMachines.js +++ b/lib/deploy/stepFunctions/compileStateMachines.js @@ -13,7 +13,8 @@ module.exports = { if (stateMachineObj.definition) { if (typeof stateMachineObj.definition === 'string') { - DefinitionString = JSON.stringify(stateMachineObj.definition).replace(/\\n|\\r|\\n\\r/g, ''); + DefinitionString = JSON.stringify(stateMachineObj.definition) + .replace(/\\n|\\r|\\n\\r/g, ''); } else { DefinitionString = JSON.stringify(stateMachineObj.definition, undefined, 2); }