#! /usr/bin/env node

'use strict';

const commandLineArgs = require('command-line-args');
const sh = require('shelljs');
const path = require('path');
const rootPath = __dirname.split('node_modules')[0];
const loadCertSettings = require('./functions/loadCertSettings');
const verifyNodeVersion = require('./functions/verifyNodeVersion');
const pkg = require(path.join(rootPath, 'package.json'));
// Parse inline arguments
const optionDefinitions = [
  { name: 'port', alias: 'p',  type: Number },
];
const options = commandLineArgs(optionDefinitions);

function runServe() {
  const devCert = loadCertSettings(rootPath);
  let command = `ng serve --port=${options.port} --ssl true --host localhost.enplug.in --ssl-cert "${devCert.cert}" --ssl-key "${devCert.key}"`;

  sh.exec(command);
}

verifyNodeVersion(pkg).then(
  () => runServe(),
  err => console.log(err)
);
