2
2
3
3
const expandPath = require ( '@antora/expand-path-helper' )
4
4
const ospath = require ( 'path' )
5
- const resolvedCacheScanDirIndexJs = require . resolve ( '@springio/antora-extensions/cache-scandir' )
5
+ const resolvedCopyRecursiveJs = require . resolve ( '@springio/antora-extensions/cache-scandir' )
6
6
const { createHash } = require ( 'crypto' )
7
+ const archiver = require ( 'archiver' )
7
8
8
9
module . exports . register = function ( { playbook, config = { } } ) {
9
10
const logger = this . getLogger ( 'inject-collector-cache-config-extension' )
@@ -24,6 +25,7 @@ module.exports.register = function ({ playbook, config = {} }) {
24
25
if ( ! fs . existsSync ( outputDir ) ) {
25
26
fs . mkdirSync ( outputDir , { recursive : true } )
26
27
}
28
+ const zipInfo = [ ]
27
29
this . once ( 'contentAggregated' , async ( { playbook, contentAggregate } ) => {
28
30
for ( const { origins } of contentAggregate ) {
29
31
for ( const origin of origins ) {
@@ -73,18 +75,23 @@ module.exports.register = function ({ playbook, config = {} }) {
73
75
const { scan : scanConfig = [ ] } = collector
74
76
// cache the output of the build
75
77
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 )
80
80
normalizedCollectorConfig . push . apply ( normalizedCollectorConfig , cachedCollectorConfig )
81
- // add the zip of cache to be published
82
81
} )
82
+ // add the zip of cache to be published
83
+ zipInfo . push ( { cacheDir, zipCacheFile } )
83
84
}
84
85
}
85
86
}
86
87
}
87
88
} )
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
+ } )
88
95
}
89
96
90
97
function download ( get , url ) {
@@ -110,12 +117,61 @@ function generateWorktreeFolderName ({ url, gitdir, worktree }) {
110
117
return `${ url . substr ( url . lastIndexOf ( '/' ) + 1 ) } -${ createHash ( 'sha1' ) . update ( url ) . digest ( 'hex' ) } `
111
118
}
112
119
113
- function createCachedCollectorConfig ( scanDir , cacheDir , zipFileName , siteDir ) {
120
+ function createCachedCollectorConfig ( scanDir , cacheDir ) {
114
121
return [
115
122
{
116
123
run : {
117
- command : `node '${ resolvedCacheScanDirIndexJs } ' '${ scanDir } ' '${ cacheDir } ' ' ${ zipFileName } '` ,
124
+ command : `node '${ resolvedCopyRecursiveJs } ' '${ scanDir } ' '${ cacheDir } '` ,
118
125
} ,
119
126
} ,
120
127
]
121
128
}
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