#!/usr/bin/env node

/**
 * Entry point for commander
 */
"use strict";

var _process = _interopRequireDefault(require("process"));

var _commander = _interopRequireDefault(require("commander"));

var _commands = require("./commands");

var _utils = require("./commands/utils");

var _cli = require("./cli");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

_commander.default.version(`v${(0, _utils.getPkgInfo)().version}`, '-v, --version').usage('<command> <args> [options]');

_commander.default.command('download').description('Fetch course/Nanodegree data from Udacity and save them locally as JSON files.').arguments('[courseid...]').option('-t, --targetdir <targetdir>', 'Target directory to save downloaded course JSON data').action(async (courseids, options) => {
  await (0, _cli.preCli)();

  const targetdir = options.targetdir || _process.default.cwd();

  (0, _commands.download)(courseids, targetdir);
}).on('--help', () => {
  console.log('');
  console.log('Examples:');
  console.log('');
  console.log(' - Download a single course/Nanodegree');
  console.log('   $ udacimak download nd019');
  console.log(' - Download multiple courses/Nanodegrees');
  console.log('   $ udacimak download nd019 st101 ud611');
  console.log(' - Download multiple courses/Nanodegrees to a specified target directory');
  console.log('   $ udacimak download -t C:/downloads nd019 st101 ud611');
});

_commander.default.command('listnd').description('List user\'s enrolled and graduated Nanodegree').action(async () => {
  await (0, _cli.preCli)();
  (0, _commands.listNanodegrees)();
});

_commander.default.command('render').description('Render downloaded json course content into HTML by downloading all videos, creating text content, etc.').arguments('<path>').option('-t, --targetdir <targetdir>', '(Optional) Target directory to save rendered course contents').option('-v, --verbose', '(Optional) Force youtube-dl to log debugging information').option('--userquizanswer', '(Optional) Force rendering user\'s Programming Question code answer').action(async (path, options) => {
  await (0, _cli.preCli)();

  const targetdir = options.targetdir || _process.default.cwd();

  global.ytVerbose = options.verbose;
  global.optRenderUserQuizAnswer = options.userquizanswer;
  (0, _commands.render)(path, targetdir);
}).on('--help', () => {
  console.log('');
  console.log('Examples:');
  console.log('');
  console.log(' - Render a course/Nanodegree to the current working directory by not specifying -t or --targetdir');
  console.log('   $ udacimak render C:/React');
  console.log(' - Render a course/Nanodegree to a specified target directory');
  console.log('   $ udacimak render -t C:/Udacity C:/React');
});

_commander.default.command('renderdir').description('Render a whole directory of downloaded json course contents').arguments('<path>').option('-t, --targetdir <targetdir>', '(Optional) Target directory to save rendered course contents').option('-v, --verbose', '(Optional) Force youtube-dl to log debugging information').option('--userquizanswer', '(Optional) Force rendering user\'s Programming Question code answer').action(async (path, options) => {
  await (0, _cli.preCli)();

  const targetdir = options.targetdir || _process.default.cwd();

  global.ytVerbose = options.verbose;
  global.optRenderUserQuizAnswer = options.userquizanswer;
  (0, _commands.renderdir)(path, targetdir);
});

_commander.default.command('settoken').description('Save Udacity authentication token locally.').arguments('<token>').action(async token => {
  await (0, _cli.preCli)();
  (0, _commands.setToken)(token);
}).on('--help', () => {
  console.log('');
  console.log('NOTE: Log in to Udacity website and find _jwt key in your browser\'s cookies to get the token');
});

_commander.default.parse(_process.default.argv); // handle uncaught exceptions and uncaught promise rejections


_process.default.on('uncaughtException', err => {
  const errorMsg = (0, _utils.getFullErrorMessage)(err);

  _utils.logger.error(`Uncaught Exception:\n${errorMsg}`);

  _process.default.exit(1);
});

_process.default.on('unhandledRejection', err => {
  const errorMsg = (0, _utils.getFullErrorMessage)(err);

  _utils.logger.error(`Unhandled Promise Rejection:\n${errorMsg}`);

  _process.default.exit(1);
});