UNPKG

944 BJavaScriptView Raw
1const fs = require('fs'),
2 shell = require('shelljs'),
3 logger = require('./logger'),
4 archiver = require('archiver');
5
6const prepareDestination = (path) => {
7 shell.mkdir('-p', 'tmp');
8 shell.rm('-rf', path);
9};
10const prepareArchive = (path, verbose = true) => {
11 prepareDestination(path);
12 const output = fs.createWriteStream(path);
13 const archive = archiver('zip', { zlib: { level: 6 } });
14
15 // listen for all archive data to be written
16 // 'close' event is fired only when a file descriptor is involved
17 output.on('close', () => {
18 if(verbose) {
19 const sizeInMB = archive.pointer() / 1024 / 1024;
20 logger.Info(`Archive size: ${sizeInMB.toFixed(2)} MB`);
21 }
22 });
23 archive.on('warning', err => {
24 if (err.code === 'ENOENT') {
25 logger.Error(err);
26 } else throw err;
27 });
28 archive.on('error', err => {
29 throw err;
30 });
31 archive.pipe(output);
32 return archive;
33};
34
35module.exports = prepareArchive;