1
1
const fs = require ( 'fs-extra' ) ;
2
- const { resolve : pathResolve } = require ( 'path' ) ;
3
- const pkg = require ( '../package.json' ) ;
2
+ const { resolve : pathResolve , join : pathJoin } = require ( 'path' ) ;
4
3
const sqlite3 = require ( 'sqlite3' ) ;
4
+ const archiver = require ( 'archiver' ) ;
5
+ const pkg = require ( '../package.json' ) ;
5
6
6
7
const DATA_DIR = pathResolve ( __dirname , '../assets/' ) ;
7
8
const INDEX_JSON_PATH = pathResolve ( __dirname , '../dist/data.json' ) ;
@@ -97,6 +98,46 @@ async function buildApi(dbPath) {
97
98
await createDatabase ( arr , dbPath ) ;
98
99
}
99
100
101
+ function compressing ( ) {
102
+ new Promise ( ( resolve , reject ) => {
103
+ const outputPaht = pathJoin ( process . cwd ( ) , '.deploy' , 'linux-command.docset.zip' ) ;
104
+ // create a file to stream archive data to.
105
+ const output = fs . createWriteStream ( outputPaht ) ;
106
+ const archive = archiver ( 'zip' , {
107
+ zlib : { level : 9 } // Sets the compression level.
108
+ } ) ;
109
+
110
+ // listen for all archive data to be written
111
+ // 'close' event is fired only when a file descriptor is involved
112
+ output . on ( 'close' , ( ) => {
113
+ console . log ( archive . pointer ( ) + ' total bytes' ) ;
114
+ console . log ( 'archiver has been finalized and the output file descriptor has closed.' ) ;
115
+ resolve ( ) ;
116
+ } ) ;
117
+
118
+ // good practice to catch warnings (ie stat failures and other non-blocking errors)
119
+ archive . on ( 'warning' , ( err ) => {
120
+ if ( err . code === 'ENOENT' ) {
121
+ console . log ( 'warning:::' , err )
122
+ // log warning
123
+ } else {
124
+ // throw error
125
+ throw err ;
126
+ }
127
+ } ) ;
128
+
129
+ // good practice to catch this error explicitly
130
+ archive . on ( 'error' , function ( err ) {
131
+ reject ( err ) ;
132
+ } ) ;
133
+
134
+ // pipe archive data to the file
135
+ archive . pipe ( output ) ;
136
+ archive . directory ( pathJoin ( process . cwd ( ) , '.deploy' , 'linux-command.docset' ) , false ) ;
137
+ archive . finalize ( ) ;
138
+ } )
139
+ }
140
+
100
141
async function build ( ) {
101
142
console . log ( `mkdir -p ${ RESOURCES_DIR } ` ) ;
102
143
await clean ( ) ;
@@ -107,6 +148,9 @@ async function build() {
107
148
108
149
console . info ( 'build documents' ) ;
109
150
await buildApi ( DB_PATH ) ;
151
+
152
+ console . info ( 'compressing zip' ) ;
153
+ await compressing ( ) ;
110
154
}
111
155
112
156
build ( )
0 commit comments