const yauzl = require('yauzl');
const yazl = require('yazl');
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const glob = require('glob');

function zip(sourceDir, targetFileName, cb) {
  sourceDir = sourceDir.replace(/\/?$/, '/');
  glob(`${sourceDir}**/*`, { nodir: true, dot: true }, function (err, files) {
    if (err) return cb && cb(err);
    const zipfile = new yazl.ZipFile();
    (files || []).forEach(function (file) {
      const relativeFileName = path.relative(sourceDir, file);
      zipfile.addFile(file, relativeFileName);
    });
    const writeStream = fs.createWriteStream(targetFileName);
    writeStream.on('error', err => cb && cb(err));
    writeStream.on('finish', () => cb && cb());
    zipfile.outputStream.pipe(writeStream);
    zipfile.end();
  });
}

function unzip(sourceFileName, targetDir, cb) {
  yauzl.open(sourceFileName, { lazyEntries: true }, function (err, zipfile) {
    if (err) return cb && cb(err);
    zipfile
      .on('entry', function (entry) {
        const unzippedFileName = path.join(targetDir, entry.fileName);
        if (/\/$/.test(unzippedFileName)) {
          // directory file names end with '/'
          mkdirp(unzippedFileName, function (err) {
            if (err) return cb && cb(err);
            zipfile.readEntry();
          });
        } else {
          // file entry
          zipfile.openReadStream(entry, function (err, readStream) {
            if (err) return cb && cb(err);
            // ensure parent directory exists
            mkdirp(path.dirname(unzippedFileName), function (err) {
              if (err) return cb && cb(err);
              readStream.pipe(fs.createWriteStream(unzippedFileName));
              readStream.on('end', () => zipfile.readEntry());
              readStream.on('error', err => cb && cb(err));
            });
          });
        }
      })
      .on('error', function (err) {
        zipfile.close();
        return cb && cb(err);
      })
      .on('end', function () {
        zipfile.close();
        return cb && cb();
      });
    zipfile.readEntry();
  });
}

module.exports = { zip, unzip };

@@ -1227,81 +1227,33 @@
 Name
-, function (err) %7B%0A            if (err) return cb && cb(err);%0A           
+)%0A            .then(() =%3E
  zip
@@ -1268,18 +1268,19 @@
 dEntry()
-;%0A
+)%0A 
         
@@ -1273,33 +1273,60 @@
 y())%0A           
-%7D
+ .catch(err =%3E cb && cb(err)
 );%0A        %7D els
@@ -1406,16 +1406,17 @@
 ion (err
+1
 , readSt
@@ -1434,32 +1434,33 @@
          if (err
+1
 ) return cb && c
@@ -1456,32 +1456,33 @@
 urn cb && cb(err
+1
 );%0A            /
@@ -1567,72 +1567,40 @@
 ame)
-, function (err) %7B%0A              if (err) return cb && cb(err);%0A
+)%0A              .then(() =%3E %7B%0A  
     
@@ -1672,32 +1672,34 @@
 ;%0A              
+  
 readStream.on('e
@@ -1737,32 +1737,34 @@
 ;%0A              
+ 
+ 
 readStream.on('e
@@ -1765,32 +1765,33 @@
 .on('error', err
+2
  =%3E cb && cb(err
@@ -1782,32 +1782,33 @@
  =%3E cb && cb(err
+2
 ));%0A            
@@ -1799,33 +1799,80 @@
 ));%0A            
+ 
-%7D
+ %7D)%0A              .catch(err3 =%3E cb && cb(err3)
 );%0A          %7D);

Fix error from breaking change in mkdirp