Skip to content

Commit 91631e8

Browse files
committed
Read json file when the statemachine invoke
1 parent b6e61de commit 91631e8

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ class ServerlessStepFunctions {
110110
usage: 'String data to be passed as an event to your step function',
111111
shortcut: 'd',
112112
},
113+
path: {
114+
usage: 'The path to a json file with input data to be passed to the invoked step function',
115+
shortcut: 'p',
116+
},
113117
stage: {
114118
usage: 'Stage of the service',
115119
shortcut: 's',
@@ -159,6 +163,7 @@ class ServerlessStepFunctions {
159163

160164
invoke() {
161165
return BbPromise.bind(this)
166+
.then(this.parseInputdate)
162167
.then(this.getStateMachineArn)
163168
.then(this.startExecution)
164169
.then(this.describeExecution);
@@ -332,9 +337,21 @@ class ServerlessStepFunctions {
332337
});
333338
}
334339

340+
parseInputdate() {
341+
if (!this.options.data && this.options.path) {
342+
const absolutePath = path.isAbsolute(this.options.path) ?
343+
this.options.path :
344+
path.join(this.serverless.config.servicePath, this.options.path);
345+
if (!this.serverless.utils.fileExistsSync(absolutePath)) {
346+
throw new this.serverless.classes.Error('The file you provided does not exist.');
347+
}
348+
this.options.data = JSON.stringify(this.serverless.utils.readFileSync(absolutePath));
349+
}
350+
return BbPromise.resolve();
351+
}
352+
335353
startExecution() {
336354
this.serverless.cli.log(`Start function ${this.options.state}...`);
337-
338355
return this.provider.request('StepFunctions',
339356
'startExecution',
340357
{

index.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,45 @@ describe('ServerlessStepFunctions', () => {
407407
);
408408
});
409409

410+
describe('#parseInputdate()', () => {
411+
let fileExistsSyncStub;
412+
let readFileSyncStub;
413+
beforeEach(() => {
414+
serverlessStepFunctions.serverless.config.servicePath = 'servicePath';
415+
fileExistsSyncStub = sinon.stub(serverlessStepFunctions.serverless.utils, 'fileExistsSync')
416+
.returns(true);
417+
readFileSyncStub = sinon.stub(serverlessStepFunctions.serverless.utils, 'readFileSync')
418+
.returns({ foo: 'var' });
419+
serverlessStepFunctions.options.data = null;
420+
serverlessStepFunctions.options.path = 'data.json';
421+
});
422+
423+
it('should throw error if file does not exists', () => {
424+
serverlessStepFunctions.serverless.utils.fileExistsSync.restore();
425+
fileExistsSyncStub = sinon.stub(serverlessStepFunctions.serverless.utils, 'fileExistsSync')
426+
.returns(false);
427+
expect(() => serverlessStepFunctions.parseInputdate()).to.throw(Error);
428+
serverlessStepFunctions.serverless.utils.readFileSync.restore();
429+
});
430+
431+
it('should parse file if path param is provided', () => {
432+
return serverlessStepFunctions.parseInputdate().then(() => {
433+
expect(serverlessStepFunctions.options.data).to.deep.equal('{"foo":"var"}');
434+
serverlessStepFunctions.serverless.utils.fileExistsSync.restore();
435+
serverlessStepFunctions.serverless.utils.readFileSync.restore();
436+
});
437+
});
438+
439+
it('should return resolve if path param is not provided', () => {
440+
serverlessStepFunctions.options.path = null;
441+
return serverlessStepFunctions.parseInputdate().then(() => {
442+
expect(serverlessStepFunctions.options.data).to.deep.equal(null);
443+
serverlessStepFunctions.serverless.utils.fileExistsSync.restore();
444+
serverlessStepFunctions.serverless.utils.readFileSync.restore();
445+
});
446+
});
447+
});
448+
410449
describe('#startExecution()', () => {
411450
let startExecutionStub;
412451
beforeEach(() => {

0 commit comments

Comments
 (0)