All files / modules/download index.js

100% Statements 45/45
100% Branches 22/22
100% Functions 3/3
100% Lines 45/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88      3x 3x 3x 3x 3x 3x 3x   3x   3x   3x           3x 6x                 1x   5x 5x 5x 4x 4x       4x   5x 4x 4x   5x 4x 4x   5x 3x 3x 3x 3x   5x 3x 3x 3x 3x   5x 4x 4x 4x 4x 4x                 3x 4x 4x       3x        
/**
 * @module download
 */
const courseService = require("../course");
const videoService = require("../video");
const fs = require("fs");
const log = require("../../util/log");
const {Parser} = require('json2csv');
const AdmZip = require('adm-zip');
const rimraf = require('rimraf');
 
const fields = ['id', 'title', 'hyperlink', 'capabilityTitle', 'capabilityId',
    'categoryTitle', 'categoryId', 'competencyTitle', 'competencyId', 'phases'];
const opts = {fields};
 
fs.existsSync("./exports") || fs.mkdirSync("./exports");
 
/**
 * Export stored learning objects to various archive formats
 * if export files do not yet exist.
 */
const generateExportFiles = async () => {
    if (
        fs.existsSync("./exports/rdf-mapped-combined.json") &&
        fs.existsSync("./exports/rdf-mapped-courses.json") &&
        fs.existsSync("./exports/rdf-mapped-videos.json") &&
        fs.existsSync("./exports/rdf-mapped-courses.csv") &&
        fs.existsSync("./exports/rdf-mapped-videos.csv") &&
        fs.existsSync("./exports/rdf-mapped-combined.zip")
    ) {
        // all export files present
        return;
    }
    const courses = await courseService.fetchAllWithUniqueTitles();
    const videos = await videoService.fetchAllWithUniqueTitles();
    if (!fs.existsSync("./exports/rdf-mapped-combined.json")) {
        log.info("Generating JSON export of course & video data..");
        const exportObject = {
            courses,
            videos,
        };
        fs.writeFileSync("./exports/rdf-mapped-combined.json", JSON.stringify(exportObject));
    }
    if (!fs.existsSync("./exports/rdf-mapped-courses.json")) {
        log.info("Generating JSON export of course data..");
        fs.writeFileSync("./exports/rdf-mapped-courses.json", JSON.stringify(courses));
    }
    if (!fs.existsSync("./exports/rdf-mapped-videos.json")) {
        log.info("Generating JSON export of video data..");
        fs.writeFileSync("./exports/rdf-mapped-videos.json", JSON.stringify(videos));
    }
    if (!fs.existsSync("./exports/rdf-mapped-courses.csv")) {
        log.info("Generating CSV export of course data..");
        const parser = new Parser(opts);
        const csv = parser.parse(courses);
        fs.writeFileSync("./exports/rdf-mapped-courses.csv", csv);
    }
    if (!fs.existsSync("./exports/rdf-mapped-videos.csv")) {
        log.info("Generating CSV export of video data..");
        const parser = new Parser(opts);
        const csv = parser.parse(videos);
        fs.writeFileSync("./exports/rdf-mapped-videos.csv", csv);
    }
    if (!fs.existsSync("./exports/rdf-mapped-combined.zip")) {
        log.info("Generating zipped CSV export of course & video data..");
        const zip = new AdmZip();
        zip.addLocalFile("./exports/rdf-mapped-courses.csv");
        zip.addLocalFile("./exports/rdf-mapped-videos.csv");
        zip.writeZip("./exports/rdf-mapped-combined.zip");
    }
};
 
/**
 * Delete all archives, i.e. clear the export folder.
 * This is often called after database changes to ensure
 * archives are up to date.
 */
const deleteExportFiles = () => {
    rimraf("./exports/*", () => {
        log.info("Cleared exports folder");
    });
};
 
module.exports = {
    generateExportFiles,
    deleteExportFiles,
};