"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = renderdir;

var _directoryTree = _interopRequireDefault(require("directory-tree"));

var _functions = require("../render/functions");

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

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

/**
 * Iterate through all directories in a source directory and render all courses
 * @param {string} sourceDir directory that contains sub-directories of
 * downloaded course JSON data from Udacity API
 * @param {string} targetDir target directory to save rendered courses
 */
async function renderdir(sourceDir, targetDir) {
  if (sourceDir === targetDir) {
    _utils.logger.error('Target directory must not be the same with source directory. Please change the target directory.');

    return;
  }

  const dirTreeSource = (0, _directoryTree.default)(sourceDir);

  if (!dirTreeSource) {
    _utils.logger.error('Path to downloaded Udacity course/Nanodegree doesn\'t exist.');

    return;
  }

  try {
    for (let i = 0, len = dirTreeSource.children.length; i < len; i += 1) {
      const child = dirTreeSource.children[i];

      if (child.type === 'directory') {
        const dirCourse = child.path;
        await (0, _functions.renderCourse)(dirCourse, targetDir);
      }
    }

    _utils.logger.info('Completed downloading all courses/Nanodegrees');
  } catch (error) {
    throw error;
  }
}