const createS3Client = require('./createS3Client');
const releaseToS3 = require('./releaseToS3');

/**
 *
 * @param {*} bucket
 * @param {*} prefix
 */
function syncDist(pkg, bucket, prefix, localDir) {
  if (prefix.startsWith('/')) {
    prefix = prefix.substr(1);
  }

  let uploadOptions = {
    localDir,
    deleteRemoved: false, // keep older versions
    s3Params: {
      Bucket: bucket,
      Prefix: prefix,
      // can add options for putObject command
      // see: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
    }
  };

  const s3Client = createS3Client(pkg);

  return s3Client
    ? releaseToS3(
      s3Client,
      uploadOptions,
      bucket,
      prefix
    )
    : Promise.resolve();
}

module.exports = syncDist;