@@ -25,6 +25,9 @@ const CACHE_COMPRESS_LEVEL = 4;
2525
2626function taskFinishHandler ( data ) {
2727 if ( data . failedTasks . length === 0 ) {
28+ console . log (
29+ `💰 Worker[${ data . id } ]: Cache hits: ${ data . cacheHits } (${ Math . round ( ( data . cacheHits / data . success ) * 100 ) } %)`
30+ ) ;
2831 console . log ( `✅ Worker[${ data . id } ]: ${ data . success } files successfully.` ) ;
2932 return false ;
3033 }
@@ -45,19 +48,21 @@ async function createWork() {
4548 const INPUT_DIR = path . join ( root , '.next' , 'server' , 'app' ) ;
4649 const OUTPUT_DIR = path . join ( root , 'public' , 'md-exports' ) ;
4750
48- const CACHE_DIR = path . join ( root , '.next' , 'cache' , 'md-exports' ) ;
49- const noCache = ! existsSync ( CACHE_DIR ) ;
50- if ( noCache ) {
51- await mkdir ( CACHE_DIR , { recursive : true } ) ;
52- }
53-
5451 console . log ( `🚀 Starting markdown generation from: ${ INPUT_DIR } ` ) ;
5552 console . log ( `📁 Output directory: ${ OUTPUT_DIR } ` ) ;
5653
5754 // Clear output directory
5855 await rm ( OUTPUT_DIR , { recursive : true , force : true } ) ;
5956 await mkdir ( OUTPUT_DIR , { recursive : true } ) ;
6057
58+ const CACHE_DIR = path . join ( root , '.next' , 'cache' , 'md-exports' ) ;
59+ console . log ( `💰 Cache directory: ${ CACHE_DIR } ` ) ;
60+ const noCache = ! existsSync ( CACHE_DIR ) ;
61+ if ( noCache ) {
62+ console . log ( `ℹ️ No cache directory found, this will take a while...` ) ;
63+ await mkdir ( CACHE_DIR , { recursive : true } ) ;
64+ }
65+
6166 // On a 16-core machine, 8 workers were optimal (and slightly faster than 16)
6267 const numWorkers = Math . max ( Math . floor ( cpus ( ) . length / 2 ) , 2 ) ;
6368 const workerTasks = new Array ( numWorkers ) . fill ( null ) . map ( ( ) => [ ] ) ;
@@ -150,7 +155,7 @@ async function genMDFromHTML(source, target, {cacheDir, noCache}) {
150155 reader . resume ( ) ;
151156
152157 await promise ;
153- return ;
158+ return true ;
154159 } catch {
155160 // pass
156161 }
@@ -209,21 +214,23 @@ async function genMDFromHTML(source, target, {cacheDir, noCache}) {
209214 reader . resume ( ) ;
210215
211216 await promise ;
217+ return false ;
212218}
213219
214220async function processTaskList ( { id, tasks, cacheDir, noCache} ) {
215221 const failedTasks = [ ] ;
222+ let cacheHits = 0 ;
216223 for ( const { sourcePath, targetPath} of tasks ) {
217224 try {
218- await genMDFromHTML ( sourcePath , targetPath , {
225+ cacheHits += await genMDFromHTML ( sourcePath , targetPath , {
219226 cacheDir,
220227 noCache,
221228 } ) ;
222229 } catch ( error ) {
223230 failedTasks . push ( { sourcePath, targetPath, error} ) ;
224231 }
225232 }
226- return { id, success : tasks . length - failedTasks . length , failedTasks} ;
233+ return { id, success : tasks . length - failedTasks . length , failedTasks, cacheHits } ;
227234}
228235
229236async function doWork ( work ) {
0 commit comments