import { Config } from './config';
import { clientTasks } from './tasks/clientTasks';
import { schemaTask } from './tasks/schemaTask';
import Listr from 'listr';

export function generate(config: Config): Promise<void> {
  if (!config.output) {
    throw new Error('`output` must be defined in the config');
  }

  return new Listr(
    [
      {
        title: `generating the client in \`${config.output}\``,
        task: () => new Listr([schemaTask(config), ...clientTasks(config)]),
      },
    ],
    { renderer: config.verbose ? 'verbose' : 'silent', exitOnError: false },
  )
    .run()
    .catch((e) => {
      // console.log(e)
      throw e?.errors?.[0];
    });
}
