#! /usr/bin/env node

'use strict';

const commandLineArgs = require('command-line-args');
const path = require('path');
const rootPath = __dirname.split('node_modules')[0];
const pkg = require(path.join(rootPath, 'package.json'));
const promptForBucket = require('./functions/promptForBucket');
const confirmDestination = require('./functions/confirmDestination');
const syncDist = require('./functions/syncDist');

let bucket;

// Parse inline arguments
const optionDefinitions = [
  { name: 'bucket', alias: 'b', type: String },
  { name: 'dev', alias: 'd', type: Boolean },
  { name: 'prefix', alias: 'p', type: String },
  { name: 'root', alias: 'r', type: String },
  { name: 'target', alias: 't', type: String },
  { name: 'version', alias: 'v', type: String },
];
const options = commandLineArgs(optionDefinitions);
const destination = options.dev ? `${pkg.config.destinationDev}${pkg.version}` : pkg.config.destination;
// const version = options.dev ? pkg.version_dev : pkg.version; //unused

function runBuild() {
  confirmDestination(bucket, destination).then(function (response) {
    if (response.confirm) {
      syncDist(pkg, bucket, destination, 'dist/');
    }
  });
}

promptForBucket(options['bucket'], pkg.config.aws.buckets)
  .then(selectedBucket => bucket = selectedBucket)
  .then(runBuild);

