'use strict';

const BbPromise = require('bluebird');
const path = require('path');
const fse = require('fs-extra');

module.exports = {
  cleanup() {
    const webpackOutputPath = this.webpackOutputPath;

    const moveArtifact = new BbPromise((resolve, reject) => {
      if (this.originalServicePath) {
        this.serverless.config.servicePath = this.originalServicePath;
        fse.copy(
          path.join(webpackOutputPath, '.serverless'),
          path.join(this.serverless.config.servicePath, '.serverless'),
          (err) => {
            if (err) {
              reject(err);
            } else {
              this.serverless.service.package.artifact = path.join(
                this.serverless.config.servicePath,
                '.serverless',
                path.basename(this.serverless.service.package.artifact)
              );
              resolve();
            }
          }
        );
      } else {
        resolve();
      }
    });

    return moveArtifact
      .then(() => {
        if (this.serverless.utils.dirExistsSync(webpackOutputPath)) {
          fse.removeSync(webpackOutputPath);
        }
      })
      .then(() => BbPromise.resolve());
  },
};

@@ -599,32 +599,597 @@
        %7D else %7B%0A
+              if (this.serverless.service.package.individually) %7B%0A                const functionNames = this.serverless.service.getAllFunctions();%0A                functionNames.forEach(name =%3E %7B%0A                  this.serverless.service.functions%5Bname%5D.artifact = path.join(%0A                      this.serverless.config.servicePath,%0A                      '.serverless',%0A                      path.basename(this.serverless.service.functions%5Bname%5D.artifact)%0A                  );%0A                %7D);%0A                resolve();%0A                return;%0A              %7D%0A%0A
               th

fix for deploying individual packages.