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

/**
 * Uploads entire contents of localDir directory to an S3 bucket with a prefix, while taking into consideration the
 * options defined in package.json (pkg).
 * @param {Object} pkg
 * @param {string} bucket
 * @param {string} prefix
 * @param {string} localDir
 */
function syncDir(pkg, bucket, prefix, localDir) {
  if (prefix.startsWith('/')) {
    prefix = prefix.substr(1);
  }

  const s3Client = createS3Client(pkg);

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

module.exports = syncDir;