{"size": 700, "ext": "js", "max_stars_repo_path": "util/upload-directory.js", "max_stars_repo_name": "vdemedes/gitghost", "max_stars_repo_stars_event_min_datetime": "2015-09-23T17:03:24.000Z", "max_stars_repo_stars_event_max_datetime": "2016-11-28T02:55:33.000Z", "max_issues_repo_path": "util/upload-directory.js", "max_issues_repo_name": "vdemedes/gitghost", "max_issues_repo_issues_event_min_datetime": "2015-09-23T17:03:00.000Z", "max_issues_repo_issues_event_max_datetime": "2015-09-23T17:03:05.000Z", "max_forks_repo_path": "util/upload-directory.js", "max_forks_repo_name": "vadimdemedes/gitghost", "max_forks_repo_forks_event_min_datetime": "2015-10-08T20:48:56.000Z", "max_forks_repo_forks_event_max_datetime": "2016-10-09T17:24:03.000Z"}
{"max_stars_count": 27, "max_issues_count": 2, "max_forks_count": 4, "avg_line_length": 17.5, "max_line_length": 56, "alphanum_fraction": 0.6457142857}
'use strict';

/**
 * Dependencies
 */

const promisify = require('pify');
const tempfile = require('tempfile');
const zlib = require('zlib');
const tar = require('tar-fs');
const fs = require('fs');

const s3 = require('../lib/s3');


/**
 * Expose fn
 */

module.exports = promisify(uploadDirectory);


/**
 * Archive and upload directory to S3
 */

function uploadDirectory (localPath, remotePath, done) {
  let tmpPath = tempfile();
  let tmpStream = fs.createWriteStream(tmpPath);
  let gzipStream = zlib.createGzip();

  tar.pack(localPath)
    .pipe(gzipStream)
    .pipe(tmpStream)
    .on('error', done)
    .on('finish', function () {
      s3.putFile(tmpPath, remotePath, done);
    });
}
