@@ -30,6 +30,8 @@ import {
3030import enumList from './enum' ;
3131
3232const delay = time => new Promise ( resolve => setTimeout ( resolve , time * 1000 ) ) ;
33+ const isString = < T > ( item : string | T ) : item is string => typeof item == 'string' ;
34+ const fileReferenceRegex = / \$ \{ f i l e \( ( .+ ) \) \} $ / ;
3335
3436export default class StepFunctionsOfflinePlugin implements Plugin {
3537 private location : string ;
@@ -304,13 +306,35 @@ export default class StepFunctionsOfflinePlugin implements Plugin {
304306 return { handler : handlerName , filePath } ;
305307 }
306308
309+ async _loadStates ( states : StateMachine [ 'States' ] | string ) : Promise < StateMachine [ 'States' ] > {
310+ if ( isString ( states ) ) {
311+ const serverlessPath = this . serverless . config . servicePath ;
312+ if ( ! serverlessPath ) {
313+ throw new this . serverless . classes . Error ( 'Could not find serverless manifest' ) ;
314+ }
315+ const match = states . match ( fileReferenceRegex ) ;
316+ if ( ! match ) {
317+ throw new this . serverless . classes . Error ( `couldn't understand string of States: ${ states } ` ) ;
318+ }
319+ const fileName = match [ 1 ] ;
320+ if ( ! this . serverlessFileExists ( fileName ) ) {
321+ throw new this . serverless . classes . Error ( `Unable to find ${ fileName } in serverless path` ) ;
322+ }
323+ return this . parseYaml < StateMachine [ 'States' ] > ( path . join ( serverlessPath , fileName ) ) ;
324+ }
325+ return states ;
326+ }
327+
307328 async buildStepWorkFlow ( ) : Promise < ReturnType < StepFunctionsOfflinePlugin [ 'process' ] > > {
308329 this . cliLog ( 'Building StepWorkFlow' ) ;
309330 if ( ! this . stateDefinition ) throw new Error ( 'Missing state definition' ) ;
310331 const event = this . loadedEventFile ?? { } ;
311332 if ( ! this . stateDefinition ?. StartAt ) {
312333 throw new Error ( 'Missing `startAt` in definition' ) ;
313334 }
335+ if ( typeof this . stateDefinition . States === 'string' ) {
336+ this . stateDefinition . States = await this . _loadStates ( this . stateDefinition . States ) ;
337+ }
314338 this . addContextObject ( this . stateDefinition . States , this . stateDefinition . StartAt , event ) ;
315339 this . states = this . stateDefinition . States ;
316340 return this . process ( this . states [ this . stateDefinition . StartAt ] , this . stateDefinition . StartAt , event ) ;
@@ -321,6 +345,10 @@ export default class StepFunctionsOfflinePlugin implements Plugin {
321345 event : Event
322346 ) : Promise < ReturnType < StepFunctionsOfflinePlugin [ 'process' ] > > {
323347 this . cliLog ( 'Building Iterator StepWorkFlow' ) ;
348+
349+ if ( typeof stateDefinition . States === 'string' ) {
350+ stateDefinition . States = await this . _loadStates ( stateDefinition . States ) ;
351+ }
324352 this . addContextObject ( stateDefinition . States , stateDefinition . StartAt , event ) ;
325353
326354 if ( ! stateDefinition . States ) return ;
0 commit comments