22
33const expandPath = require ( '@antora/expand-path-helper' )
44const ospath = require ( 'path' )
5- const resolvedCacheScanDirIndexJs = require . resolve ( '@springio/antora-extensions/cache-scandir' )
5+ const resolvedCopyRecursiveJs = require . resolve ( '@springio/antora-extensions/cache-scandir' )
66const { createHash } = require ( 'crypto' )
7+ const archiver = require ( 'archiver' )
78
89module . exports . register = function ( { playbook, config = { } } ) {
910 const logger = this . getLogger ( 'inject-collector-cache-config-extension' )
@@ -24,6 +25,7 @@ module.exports.register = function ({ playbook, config = {} }) {
2425 if ( ! fs . existsSync ( outputDir ) ) {
2526 fs . mkdirSync ( outputDir , { recursive : true } )
2627 }
28+ const zipInfo = [ ]
2729 this . once ( 'contentAggregated' , async ( { playbook, contentAggregate } ) => {
2830 for ( const { origins } of contentAggregate ) {
2931 for ( const origin of origins ) {
@@ -73,18 +75,23 @@ module.exports.register = function ({ playbook, config = {} }) {
7375 const { scan : scanConfig = [ ] } = collector
7476 // cache the output of the build
7577 const scanDir = expandPath ( scanConfig . dir , expandPathContext )
76- logger . info (
77- `Configuring collector to cache '${ scanDir } ' at '${ cacheDir } ' and zip the results at '${ zipCacheFile } '`
78- )
79- const cachedCollectorConfig = createCachedCollectorConfig ( scanDir , cacheDir , zipCacheFile )
78+ logger . info ( `Configuring collector to cache '${ scanDir } ' at '${ cacheDir } '` )
79+ const cachedCollectorConfig = createCachedCollectorConfig ( scanDir , cacheDir )
8080 normalizedCollectorConfig . push . apply ( normalizedCollectorConfig , cachedCollectorConfig )
81- // add the zip of cache to be published
8281 } )
82+ // add the zip of cache to be published
83+ zipInfo . push ( { cacheDir, zipCacheFile } )
8384 }
8485 }
8586 }
8687 }
8788 } )
89+ this . once ( 'beforePublish' , async ( ) => {
90+ for ( const info of zipInfo ) {
91+ console . log ( JSON . stringify ( info ) )
92+ await zip ( fs , info . cacheDir , info . zipCacheFile )
93+ }
94+ } )
8895}
8996
9097function download ( get , url ) {
@@ -110,12 +117,61 @@ function generateWorktreeFolderName ({ url, gitdir, worktree }) {
110117 return `${ url . substr ( url . lastIndexOf ( '/' ) + 1 ) } -${ createHash ( 'sha1' ) . update ( url ) . digest ( 'hex' ) } `
111118}
112119
113- function createCachedCollectorConfig ( scanDir , cacheDir , zipFileName , siteDir ) {
120+ function createCachedCollectorConfig ( scanDir , cacheDir ) {
114121 return [
115122 {
116123 run : {
117- command : `node '${ resolvedCacheScanDirIndexJs } ' '${ scanDir } ' '${ cacheDir } ' ' ${ zipFileName } '` ,
124+ command : `node '${ resolvedCopyRecursiveJs } ' '${ scanDir } ' '${ cacheDir } '` ,
118125 } ,
119126 } ,
120127 ]
121128}
129+
130+ const zip = async function ( fs , src , destination ) {
131+ const path = require ( 'path' )
132+ const destParent = path . dirname ( destination )
133+ if ( ! fs . existsSync ( destParent ) ) {
134+ fs . mkdirs ( destParent , { recursive : true } )
135+ }
136+ const output = fs . createWriteStream ( destination )
137+ const archive = archiver ( 'zip' , {
138+ zlib : { level : 9 } , // Sets the compression level.
139+ } )
140+ // listen for all archive data to be written
141+ // 'close' event is fired only when a file descriptor is involved
142+ output . on ( 'close' , function ( ) {
143+ console . log ( archive . pointer ( ) + ' total bytes' )
144+ console . log ( 'archiver has been finalized and the output file descriptor has closed.' )
145+ } )
146+
147+ // This event is fired when the data source is drained no matter what was the data source.
148+ // It is not part of this library but rather from the NodeJS Stream API.
149+ // @see : https://nodejs.org/api/stream.html#stream_event_end
150+ output . on ( 'end' , function ( ) {
151+ console . log ( 'Data has been drained' )
152+ } )
153+
154+ // good practice to catch warnings (ie stat failures and other non-blocking errors)
155+ archive . on ( 'warning' , function ( err ) {
156+ if ( err . code === 'ENOENT' ) {
157+ // log warning
158+ } else {
159+ // throw error
160+ throw err
161+ }
162+ } )
163+
164+ // good practice to catch this error explicitly
165+ archive . on ( 'error' , function ( err ) {
166+ throw err
167+ } )
168+
169+ // pipe archive data to the file
170+ archive . pipe ( output )
171+
172+ archive . directory ( src , false )
173+
174+ await archive . finalize ( )
175+
176+ console . log ( `Saving ${ src } into ${ destination } ` )
177+ }
0 commit comments