#! /usr/bin/env node

const colors = require('picocolors');

const app = require('./app');
const extractCommand = require('./helpers/extract-command');

const [commandName, options, restOfCommands] = extractCommand();
const command = app[commandName];

(async () => {
  if (typeof command === 'function') {
    try {
      await command(
        {
          ...options,
          fromCli: true,
        },
        restOfCommands,
      );
    } catch (e) {
      console.log(
        '\n ',
        colors.bgRed(' Error '),
        colors.red(e.toString().replace(/^Error: /, '')),
        '\n',
      );
      process.exit(1);
    }
  } else {
    app.notFound({ name: commandName });
  }
})();
