"use strict";

var _https = _interopRequireDefault(require("https"));

var _http = _interopRequireDefault(require("http"));

var _path = _interopRequireDefault(require("path"));

var _fs = _interopRequireDefault(require("fs"));

var _getOptions = _interopRequireDefault(require("../utils/getOptions"));

var _urlConcat = require("../utils/urlConcat");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const {
  sslCertURL
} = (0, _getOptions.default)();

const CRET_FOLDER_PATH = _path.default.join(__dirname, '..', '..', 'cert');

console.log({
  CRET_FOLDER_PATH,
  sslCertURL
});
const CERT_PATH = 'cert.pem';
const KEY_PATH = 'key.pem';
const PASSPHRASE_PATH = 'passphrase.pem';

function updateSSlFile(sslCertURL, filename) {
  let url = (0, _urlConcat.urlConcat)(sslCertURL, filename);
  console.log({
    url
  });
  (url.startsWith('https://') ? _https.default : _http.default).get(url, resp => {
    let fpath = _path.default.join(CRET_FOLDER_PATH, filename);

    let data = ''; // A chunk of data has been received.

    resp.on('data', chunk => {
      data += chunk;
    }); // The whole response has been received. Print out the result.

    resp.on('end', () => {
      _fs.default.writeFileSync(fpath, data);
    }); // return;
  }).on('error', err => {
    console.log(`Error: ${err.message}`);
  });
}

if (!sslCertURL) {
  throw 'sslCertURL not given';
}

updateSSlFile(sslCertURL, PASSPHRASE_PATH);
updateSSlFile(sslCertURL, CERT_PATH);
updateSSlFile(sslCertURL, KEY_PATH);