#!/usr/bin/env node

const chalk = require('chalk');
const tools = require('@eui/tools/scripts/utils/tools');
const figlet = require('figlet');
const path = require('path');
const fs = require('fs');
const generators = require('../lib/generators');
const config = require('../lib/config');

// fetching cli arguments
const args = tools.getArgs();

// Print header and version
console.info(
  '\n\n',
  chalk.yellow(figlet.textSync('eUI CLI', { horizontalLayout: 'full' })),
  chalk.green(`\n  v${config.version}\n`)
);

// remapping config if passed as argument - automated mode
let inputConfig = config.parseInputConfig(args.config);

// Detecting targetPath and create it if present as argument - local and automated mode
let targetPath;

if (args.targetPath) {
  targetPath = path.join(config.targetPath, args.targetPath);

  if (!tools.isDirExists(targetPath)) {
    tools.mkdir(targetPath);
  }

// otherwise taking the default config targetPath defined
} else {
  targetPath = config.targetPath;
}

if (args.v) {
  console.info('Current eUI CLI version installed : ' + config.version);

} else {
  return generators.appGenerate({
    inputConfig: inputConfig,
    targetPath: targetPath
  });
}
