@@ -9,6 +9,7 @@ const { version } = require('../package.json');
9
9
const { exception } = require ( 'console' ) ;
10
10
const svgDim = require ( 'svg-dimensions' ) ;
11
11
const YAML = require ( 'yaml' ) ;
12
+ const crypto = require ( 'crypto' ) ;
12
13
13
14
const path = require ( 'path' ) ;
14
15
@@ -420,6 +421,8 @@ if (! script) {
420
421
throw "Please specify .smte file e.g. -i demo.smte"
421
422
}
422
423
424
+ const htmlHashed = { } ;
425
+ const reuseFrames = { } ;
423
426
424
427
const runGeneration = async ( lang ) => {
425
428
initVariables ( ) ;
@@ -435,17 +438,22 @@ const runGeneration = async (lang) => {
435
438
436
439
const doFrame = async ( ) => {
437
440
const html = genHtml ( parts ) ;
441
+ const hash = crypto . createHash ( 'sha1' ) . update ( html ) . digest ( 'base64' ) ;
438
442
if ( cntr < skipFrames || ( globalLastFrame && cntr > globalLastFrame ) ) {
439
443
cntr += 1 ;
440
444
return ;
441
445
}
442
446
totalFramesCount += 1 ;
443
-
444
- await fs . writeFile ( `${ FRAMES_DIR } /_index${ ( '' + cntr ) . padStart ( MAX_FILENAME_DIGS , '0' ) } .html` , html , function ( err ) {
445
- if ( err ) {
446
- return console . log ( err ) ;
447
- }
448
- } ) ;
447
+ if ( ! htmlHashed [ hash ] ) {
448
+ htmlHashed [ hash ] = cntr ;
449
+ await fs . writeFile ( `${ FRAMES_DIR } /_index${ ( '' + cntr ) . padStart ( MAX_FILENAME_DIGS , '0' ) } .html` , html , function ( err ) {
450
+ if ( err ) {
451
+ return console . log ( err ) ;
452
+ }
453
+ } ) ;
454
+ } else {
455
+ reuseFrames [ cntr ] = htmlHashed [ hash ] ;
456
+ }
449
457
450
458
cntr += 1 ;
451
459
log ( `HTML pages gen: ${ ( cntr * 100.0 / ( totalFrames + 1 ) ) . toFixed ( 2 ) } %` , '\033[F' ) ;
@@ -666,27 +674,44 @@ const runGeneration = async (lang) => {
666
674
667
675
async function genScreenshots ( index ) {
668
676
await new Promise ( ( resolve ) => {
669
- const proc = spawn ( 'node' , [ path . resolve ( __dirname , 'puWorker.js' ) , pageW , pageH , index , totalFramesCount , FRAMES_DIR , FORMAT , QUALITY ] , { shell : true } ) ;
670
- proc . stdout . on ( 'data' , ( data ) => {
671
- // console.log(`NodeOUT: ${data}`);
672
- } ) ;
673
-
674
- proc . stderr . on ( 'data' , ( data ) => {
675
- console . error ( `NodeERR: ${ data } ` ) ;
676
- } ) ;
677
-
678
- proc . on ( 'close' , ( code ) => {
677
+ if ( ! reuseFrames [ index ] ) {
678
+
679
+ const proc = spawn ( 'node' , [ path . resolve ( __dirname , 'puWorker.js' ) , pageW , pageH , index , totalFramesCount , FRAMES_DIR , FORMAT , QUALITY , skipFrames || 0 ] , { shell : true } ) ;
680
+ proc . stdout . on ( 'data' , ( data ) => {
681
+ // console.log(`NodeOUT: ${data}`);
682
+ } ) ;
683
+ proc . stderr . on ( 'data' , ( data ) => {
684
+ console . error ( `NodeERR: ${ data } ` ) ;
685
+ } ) ;
686
+ proc . on ( 'close' , ( code ) => {
687
+ totalGenCntr += 1 ;
688
+ log ( `Frames gen: ${ ( totalGenCntr * 100.0 / totalFramesCount ) . toFixed ( 2 ) } %` , '\033[F' ) ;
689
+ if ( code !== 0 ) {
690
+ log ( '🔴 node failed' )
691
+ }
692
+ resolve ( ) ;
693
+ } ) ;
694
+ } else {
679
695
totalGenCntr += 1 ;
680
- log ( `Frames gen: ${ ( totalGenCntr * 100.0 / totalFramesCount ) . toFixed ( 2 ) } %` , '\033[F' ) ;
696
+ resolve ( ) ;
697
+ }
698
+ } )
699
+ }
681
700
682
- if ( code !== 0 ) {
683
- log ( '🔴 node failed' )
701
+ async function copyReusedScreenshots ( index ) {
702
+ if ( reuseFrames [ index ] ) {
703
+ const reuseIndex = reuseFrames [ index ] ;
704
+ const srcFile = `${ FRAMES_DIR } /${ ( '' + ( reuseIndex - skipFrames ) ) . padStart ( MAX_FILENAME_DIGS , '0' ) } .${ FORMAT } ` ;
705
+ const dstFile = `${ FRAMES_DIR } /${ ( '' + ( index - skipFrames ) ) . padStart ( MAX_FILENAME_DIGS , '0' ) } .${ FORMAT } ` ;
706
+ fsExtra . copyFile ( srcFile , dstFile , ( err ) => {
707
+ if ( err ) {
708
+ log ( `🔴 failed to copy frame ${ srcFile } to ${ dstFile } ` , )
709
+ throw err ;
684
710
}
685
- resolve ( ) ;
686
711
} ) ;
687
- } )
712
+ }
688
713
}
689
-
714
+
690
715
async function genScreenshotsForChunk ( indexesChunk ) {
691
716
for ( let i = 0 ; i < indexesChunk . length ; i += 1 ) {
692
717
await genScreenshots ( indexesChunk [ i ] ) ;
@@ -697,6 +722,11 @@ const runGeneration = async (lang) => {
697
722
698
723
await Promise . all ( arrayChunks ( indexes , Math . round ( ( indexes . length ) / THREADS ) ) . map ( async ( indexesChunk ) => await genScreenshotsForChunk ( indexesChunk ) ) )
699
724
725
+ // another run to copy all duplicate files
726
+ indexes . forEach ( copyReusedScreenshots ) ;
727
+
728
+
729
+
700
730
701
731
log ( '✅ [3/4] Frames generation done' )
702
732
0 commit comments