-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
50 lines (41 loc) · 1.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const fs = require('fs');
const archiver = require('archiver');
function zipFolderPromise(dirname, outputFile, format = 'zip', subDirectory = '') {
return new Promise((resolve, reject) => {
try {
if (!fs.existsSync(dirname)) {
throw new Error(`Error: '${dirname}' does not exist`);
}
let archiveOpts;
switch (format) {
case 'zip':
archiveOpts = {
zlib: { level: 9 },
};
break;
case 'tar':
archiveOpts = {
gzip: true,
gzipOptions: { level: 9 },
};
break;
default:
throw new Error("Error: Only 'zip' and 'tar' formats are supported.");
}
const output = fs.createWriteStream(outputFile);
const archive = archiver(format, archiveOpts);
output.on('close', () => {
resolve(`${archive.pointer()} bytes written`);
});
archive.on('error', (err) => {
throw err;
});
archive.pipe(output);
archive.directory(dirname, subDirectory || false);
archive.finalize();
} catch (err) {
reject(err);
}
});
}
module.exports = zipFolderPromise;