text: 'use strict';
  
  const archiver = require('archiver');
  const BbPromise = require('bluebird');
  const path = require('path');
  const fs = require('fs');
  const glob = require('glob-all');
  
  module.exports = {
    zipDirectory(servicePath, exclude, zipFileName) {
      const patterns = ['**'];
      exclude.forEach((pattern) => {
        if (pattern.charAt(0) !== '!') {
          patterns.push(`!${pattern}`);
        } else {
          patterns.push(pattern.substring(1));
        }
      });
  
      patterns.push('!.serverless/**');
  
      const zip = archiver.create('zip');
  
      const artifactFilePath = path.join(
        servicePath,
        '.serverless',
        zipFileName
      );
  
      this.serverless.utils.writeFileDir(artifactFilePath);
  
      const output = fs.createWriteStream(artifactFilePath);
  
      output.on('open', () => {
        zip.pipe(output);
  
        const files = glob.sync(patterns, {
          cwd: servicePath,
          dot: true,
          silent: true,
          follow: true,
        });
  
        files.forEach((filePath) => {
          const fullPath = path.resolve(
            servicePath,
            filePath
          );
  
          const stats = fs.statSync(fullPath);
  
          if (!stats.isDirectory(fullPath)) {
            zip.append(fs.readFileSync(fullPath), {
              name: filePath,
              mode: stats.mode,
            });
          }
        });
  
        zip.finalize();
      });
  
      return new BbPromise((resolve, reject) => {
        output.on('close', () => resolve(artifactFilePath));
        zip.on('error', (err) => reject(err));
      });
    },
  };
  