@@ -46,6 +46,7 @@ type DiffActionOptions = {
46
46
severity : 'info' | 'warn' | 'error' ;
47
47
lastChange : boolean ;
48
48
generated ?: boolean ;
49
+ out ?: string ;
49
50
} ;
50
51
51
52
const description = 'Run a diff between two API specs' ;
@@ -118,6 +119,10 @@ export const registerDiff = (
118
119
. option ( '-c, --check' , 'Enable checks' , false )
119
120
. option ( '-u, --upload' , 'Upload run to cloud' , false )
120
121
. option ( '-w, --web' , 'View the diff in the Optic changelog web view' , false )
122
+ . option (
123
+ '-o, --out <file>' ,
124
+ 'write a self-contained HTML diff. use with --web'
125
+ )
121
126
. option ( '--json' , 'Output as json' , false )
122
127
. option ( '--last-change' , 'Find the last change for this spec' , false )
123
128
. option (
@@ -368,6 +373,8 @@ const getDiffAction =
368
373
369
374
const diffResult = await runDiff ( parsedFiles , config , options , file1 ) ;
370
375
let maybeChangelogUrl : string | null = null ;
376
+ let indexPath : string | null = null ;
377
+ let compressedData : string | null = null ;
371
378
let specUrl : string | null = null ;
372
379
373
380
let [ baseParseResult , headParseResult , specDetails ] = parsedFiles ;
@@ -447,15 +454,14 @@ const getDiffAction =
447
454
base : options . base ,
448
455
} ;
449
456
450
- const compressedData = compressDataV2 (
457
+ compressedData = compressDataV2 (
451
458
baseParseResult ,
452
459
headParseResult ,
453
460
diffResult . specResults ,
454
461
meta ,
455
462
diffResult . changelogData
456
463
) ;
457
464
analyticsData . compressedDataLength = compressedData . length ;
458
- logger . info ( 'Opening up diff in web view' ) ;
459
465
460
466
const copyPath = path . join ( os . tmpdir ( ) , 'optic-changelog' ) ;
461
467
@@ -466,13 +472,31 @@ const getDiffAction =
466
472
{ errorOnExist : false , overwrite : false }
467
473
) ;
468
474
469
- const baseHtml = path . resolve ( path . join ( copyPath , 'index.html' ) ) ;
475
+ indexPath = path . join (
476
+ path . resolve ( path . join ( __dirname , '../../../web/build' ) ) ,
477
+ 'index.html'
478
+ ) ;
479
+ const baseHtml = path . resolve ( indexPath ) ;
470
480
471
481
maybeChangelogUrl = `${ baseHtml } #${ compressedData } ` ;
472
482
await flushEvents ( ) ;
473
483
}
474
484
trackEvent ( 'optic.diff.view_web' , analyticsData ) ;
475
- await openUrl ( maybeChangelogUrl ) ;
485
+ if ( options . out && indexPath ) {
486
+ const indexContents = ( await fs . readFile ( indexPath ) ) . toString ( ) ;
487
+ const newContents = indexContents . replace (
488
+ `<div id="root"></div>` ,
489
+ `<div id="root"></div><script>window.diffData ='${ compressedData } '</script>`
490
+ ) ;
491
+
492
+ const out = path . resolve ( process . cwd ( ) , options . out ) ;
493
+ logger . info ( 'Diff HTML written to ' + out ) ;
494
+
495
+ await fs . writeFile ( out , newContents ) ;
496
+ } else {
497
+ logger . info ( 'Opening up diff in web view' ) ;
498
+ await openUrl ( maybeChangelogUrl ) ;
499
+ }
476
500
}
477
501
}
478
502
0 commit comments