@@ -351,11 +351,10 @@ const fixJSON = (data, options = {}) => {
351351 fixTargetInPlace ( target ) ;
352352 }
353353
354- let stage ;
355354 const allStages = targets . filter ( ( target ) => target . isStage ) ;
356355 if ( allStages . length === 0 ) {
357356 log ( 'stage is missing; adding an empty one' ) ;
358- stage = {
357+ targets . unshift ( {
359358 isStage : true ,
360359 name : 'Stage' ,
361360 variables : { } ,
@@ -380,22 +379,31 @@ const fixJSON = (data, options = {}) => {
380379 videoTransparency : 50 ,
381380 videoState : "on" ,
382381 textToSpeechLanguage : null
383- } ;
384- targets . unshift ( stage ) ;
385- } else if ( allStages . length === 1 ) {
386- const stageIndex = targets . findIndex ( ( target ) => target . isStage ) ;
387- // stageIndex guaranteed to not be -1 by earlier filter check
388- stage = targets [ stageIndex ] ;
389- // stage must be the first target
390- if ( stageIndex !== 0 ) {
391- log ( ` stage was at wrong index: ${ stageIndex } ` ) ;
392- targets . splice ( stageIndex , 1 ) ;
382+ } ) ;
383+ } else {
384+ // We will accept the first stage in targets as the real stage
385+ const firstStageIndex = targets . findIndex ( ( target ) => target . isStage ) ;
386+
387+ // Stage must be the first target
388+ if ( firstStageIndex !== 0 ) {
389+ log ( `stage was at wrong index: ${ firstStageIndex } ` ) ;
390+ const stage = targets [ firstStageIndex ] ;
391+ targets . splice ( firstStageIndex , 1 ) ;
393392 targets . unshift ( stage ) ;
394393 }
395- } else {
396- throw new Error ( `wrong number of stages: ${ allStages . length } ` ) ;
394+
395+ // Remove all the other stages
396+ for ( let i = targets . length - 1 ; i > 0 ; i -- ) {
397+ if ( targets [ i ] . isStage ) {
398+ log ( `removing extra stage at index ${ i } ` ) ;
399+ targets . splice ( i , 1 ) ;
400+ }
401+ }
397402 }
398403
404+ // Above checks ensure this invariant holds
405+ const stage = targets [ 0 ] ;
406+
399407 // stage's name must match exactly
400408 if ( stage . name !== 'Stage' ) {
401409 log ( `stage had wrong name: ${ stage . name } ` ) ;
0 commit comments