#!/usr/bin/env node
'use strict';

/**
 * Commander.js
 * https://www.npmjs.com/package/commander
 */
import { Command } from 'commander';
import { VERSION } from './types';
import * as fs from 'fs';

import { executeCommands } from './executor';

const config_file = process.env.HOME + '/dmtools.json';

function source_files(files: string) {
  return files.split(',');
}
const cmd = new Command();

let config = { project: {} };
if (fs.existsSync(config_file)) {
  config = JSON.parse(fs.readFileSync(config_file, 'utf8'));
}

cmd
  .name('dm')
  .version(
    `DevMentor CLI DevTools v${VERSION}`,
    '-v, --version',
    'Show current version',
  )
  .usage('<command> <project> [options...]\n\n     commands: new')
  .arguments('<command> <project>')
  .option('-t, --type <type>', 'project types: koa, node, pg, ts, web')
  .option('--e2e', 'end to end testing')
  .option('-w, --web', 'simple static Web setup')
  .option('--cpp [items]', 'C++ project', source_files)
  .option('-D, --debug', 'debug build')
  .option('--release', 'debug build')
  .action((command: string, project: string) => {
    Object.assign(options, { command, project });
  })
  .on('--help', () => {
    console.log(`\nDev Mentor DM-Tools
Create C++, TypeScript and JavaScript projects.\n
Website: https://www.npmjs.com/package/dm-tools
    `);
  })
  .parse(process.argv);

const cmd_opts = cmd.opts();

Object.assign(options, {
  type: cmd_opts.type,
  e2e: cmd_opts.e2e,
  web: cmd_opts.web,
  debug: cmd_opts.debug,
  release: cmd_opts.release,
});

//console.log(`DEBUG> ${JSON.stringify(options)}`);

switch (options['command']) {
  case 'new':
    executeCommands(options);
    break;
  default:
    console.log('Unknown Command, doing nothing!');
}
