UNPKG

materials-cli

Version:
87 lines (86 loc) 4.31 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); const listr_1 = __importDefault(require("listr")); const chalk_1 = __importDefault(require("chalk")); const inquirer_1 = require("./inquirer"); const open_1 = __importDefault(require("open")); const link_downloader_1 = require("./link-downloader"); class ConcurrentDownloader { constructor(materialsLegacy, course, folderPath) { this.tasks = []; this.materialsAPI = materialsLegacy; this.course = course; this.folderPath = folderPath; } scheduleDownloads(resources) { for (let i = 0; i < resources.length; i++) { let currentResource = resources[i]; const filePath = path_1.default.join(this.folderPath, this.course, currentResource.category, currentResource.title); if (!fs_1.default.existsSync(filePath)) { this.tasks.push({ title: "Downloading " + currentResource.title, task: () => __awaiter(this, void 0, void 0, function* () { yield this.materialsAPI.downloadFile(currentResource, currentResource.index, this.folderPath, this.course); }) }); } } } scheduleLinkDownloads(resources) { for (let i = 0; i < resources.length; i++) { let currentResource = resources[i]; const filePath = path_1.default.join(this.folderPath, this.course, currentResource.category, currentResource.title + ".pdf"); if (!fs_1.default.existsSync(filePath)) { this.tasks.push({ title: "Downloading " + currentResource.title, task: () => __awaiter(this, void 0, void 0, function* () { yield link_downloader_1.downloadURL(this.folderPath, this.course, currentResource); }) }); } } } executeDownloads(openFolder) { return __awaiter(this, void 0, void 0, function* () { const numToDownload = this.tasks.length; if (numToDownload !== 0) { const listr = new listr_1.default(this.tasks, { concurrent: true }); return yield listr.run().catch(err => { console.error(err); }).then(() => __awaiter(this, void 0, void 0, function* () { console.log(chalk_1.default.greenBright(`Downloaded ${numToDownload} new resources!`)); if (openFolder) { const openFolderResponse = yield inquirer_1.promptOpenFolder(); if (openFolderResponse.openFolder) { yield open_1.default(path_1.default.join(this.folderPath, this.course)); } } })); } else { console.log(chalk_1.default.greenBright("All resources already downloaded, no new to pull!")); if (openFolder) { const openFolderResponse = yield inquirer_1.promptOpenFolder(); if (openFolderResponse.openFolder) { yield open_1.default(path_1.default.join(this.folderPath, this.course)); } } } }); } } exports.default = ConcurrentDownloader;