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

/**
 *
 * @param {*} bucket
 * @param {*} prefix
 */
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;

// const AWS = require("aws-sdk");
// const fs = require("fs");
// const path = require("path");

// // configuration
// const config = {
//   s3BucketName: 'your.s3.bucket.name',
//   folderPath: '../dist' // path relative script's location
// };

// // initialize S3 client
// const s3 = new AWS.S3({ signatureVersion: 'v4' });

// // resolve full folder path
// const distFolderPath = path.join(__dirname, config.folderPath);

// // get of list of files from 'dist' directory
// fs.readdir(distFolderPath, (err, files) => {

//   if(!files || files.length === 0) {
//     console.log(`provided folder '${distFolderPath}' is empty or does not exist.`);
//     console.log('Make sure your project was compiled!');
//     return;
//   }

//   // for each file in the directory
//   for (const fileName of files) {

//     // get the full path of the file
//     const filePath = path.join(distFolderPath, fileName);
    
//     // ignore if directory
//     if (fs.lstatSync(filePath).isDirectory()) {
//       continue;
//     }

//     // read file contents
//     fs.readFile(filePath, (error, fileContent) => {
//       // if unable to read file contents, throw exception
//       if (error) { throw error; }

//       // upload file to S3
//       s3.putObject({
//         Bucket: config.s3BucketName,
//         Key: fileName,
//         Prefix: 
//         Body: fileContent
//       }, (res) => {
//         console.log(`Successfully uploaded '${fileName}'!`);
//       });

//     });
//   }
// });