"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const exercise_1 = require("../managers/config/exercise");
const console_1 = require("../utils/console");
const audit_1 = require("../utils/audit");
const SessionCommand_1 = require("../utils/SessionCommand");
const path = require("path");
const command_1 = require("@oclif/command");
const sidebarGenerator_1 = require("../utils/sidebarGenerator");
// eslint-disable-next-line
const fetch = require("node-fetch");
class AuditCommand extends SessionCommand_1.default {
    async init() {
        const { flags } = this.parse(AuditCommand);
        await this.initSession(flags);
    }
    async run() {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
        const { flags } = this.parse(AuditCommand);
        const strict = flags.strict;
        console_1.default.log("Running command audit...");
        // Get configuration object.
        let config = (_a = this.configManager) === null || _a === void 0 ? void 0 : _a.get();
        if (config) {
            const errors = [];
            const warnings = [];
            if (((_b = config === null || config === void 0 ? void 0 : config.config) === null || _b === void 0 ? void 0 : _b.projectType) === "tutorial") {
                const counter = {
                    images: {
                        error: 0,
                        total: 0,
                    },
                    links: {
                        error: 0,
                        total: 0,
                    },
                    exercises: 0,
                    readmeFiles: 0,
                };
                // Checks if learnpack clean has been run
                audit_1.default.checkLearnpackClean(config, errors);
                // Build exercises if they are not built yet.
                (_c = this.configManager) === null || _c === void 0 ? void 0 : _c.buildIndex();
                config = (_d = this.configManager) === null || _d === void 0 ? void 0 : _d.get();
                // Check if the exercises folder has some files within any ./exercise
                const exercisesPath = config.config.exercisesPath;
                fs.readdir(exercisesPath, (err, files) => {
                    if (err) {
                        return console.log("Unable to scan directory: " + err);
                    }
                    // listing all files using forEach
                    for (const file of files) {
                        // Do whatever you want to do with the file
                        const filePath = path.join(exercisesPath, file);
                        if (fs.statSync(filePath).isFile())
                            warnings.push({
                                exercise: file,
                                msg: "This file is not inside any exercise folder.",
                            });
                    }
                });
                // This function is being created because the find method doesn't work with promises.
                const find = async (file, lang, exercise) => {
                    if (file.name === lang) {
                        await audit_1.default.checkUrl(config, file.path, file.name, exercise, errors, warnings, counter);
                        return true;
                    }
                    return false;
                };
                console_1.default.debug("config", config);
                console_1.default.info(" Checking if the config file is fine...");
                // These two lines check if the 'slug' property is inside the configuration object.
                console_1.default.debug("Checking if the slug property is inside the configuration object...");
                // check if the slug property is in the configuration object
                if (!((_e = config.config) === null || _e === void 0 ? void 0 : _e.slug))
                    errors.push({
                        exercise: undefined,
                        msg: "The slug property is not in the configuration object",
                    });
                if (((_f = config.config) === null || _f === void 0 ? void 0 : _f.slug) && !audit_1.default.checkSlug((_g = config.config) === null || _g === void 0 ? void 0 : _g.slug)) {
                    errors.push({
                        exercise: undefined,
                        msg: "The slug property is not a valid slug. It must start and end with a letter and be less than 50 characters.",
                    });
                }
                console_1.default.info("Checking if the sidebar.json file is fine...");
                const isSidebarFine = await (0, sidebarGenerator_1.checkAndFixSidebar)(config, false);
                if (!isSidebarFine) {
                    if (strict) {
                        errors.push({
                            exercise: undefined,
                            msg: "The sidebar.json file has errors, please fill all missing translations",
                        });
                    }
                    else {
                        warnings.push({
                            exercise: undefined,
                            msg: "The sidebar.json file has errors, please fill all missing translations",
                        });
                    }
                }
                // check if the duration property is in the configuration object
                if (!((_h = config.config) === null || _h === void 0 ? void 0 : _h.duration))
                    warnings.push({
                        exercise: undefined,
                        msg: "The duration property is not in the configuration object",
                    });
                // check if the difficulty property is in the configuration object
                if (!((_j = config.config) === null || _j === void 0 ? void 0 : _j.difficulty))
                    warnings.push({
                        exercise: undefined,
                        msg: "The difficulty property is not in the configuration object",
                    });
                // check if the bugs_link property is in the configuration object
                if (!((_k = config.config) === null || _k === void 0 ? void 0 : _k.bugsLink)) {
                    if (strict)
                        errors.push({
                            exercise: undefined,
                            msg: "The bugsLink property is not in the configuration object",
                        });
                    else
                        warnings.push({
                            exercise: undefined,
                            msg: "The bugsLink property is not in the configuration object",
                        });
                }
                // check if the video_solutions  property is in the configuration object
                if (((_l = config.config) === null || _l === void 0 ? void 0 : _l.videoSolutions) === undefined)
                    warnings.push({
                        exercise: undefined,
                        msg: "The videoSolutions property is not in the configuration object",
                    });
                // These two lines check if the 'repository' property is inside the configuration object.
                console_1.default.debug("Checking if the repository property is inside the configuration object...");
                if (!((_m = config.config) === null || _m === void 0 ? void 0 : _m.repository)) {
                    if (strict)
                        errors.push({
                            exercise: undefined,
                            msg: "The repository property is not in the configuration object",
                        });
                    else
                        warnings.push({
                            exercise: undefined,
                            msg: "The repository property is not in the configuration object",
                        });
                }
                else
                    audit_1.default.isUrl((_o = config.config) === null || _o === void 0 ? void 0 : _o.repository, errors, counter);
                // These two lines check if the 'description' property is inside the configuration object.
                console_1.default.debug("Checking if the description property is inside the configuration object...");
                if (!((_p = config.config) === null || _p === void 0 ? void 0 : _p.description))
                    errors.push({
                        exercise: undefined,
                        msg: "The description property is not in the configuration object",
                    });
                if (!((_q = config.config) === null || _q === void 0 ? void 0 : _q.projectType))
                    errors.push({
                        exercise: undefined,
                        msg: "The projectType property is mandatory in the configuration object",
                    });
                if (errors.length === 0)
                    console_1.default.log("The config file is ok");
                // Validates if images and links are working at every README file.
                const exercises = config.exercises;
                const readmeFiles = [];
                if (exercises && exercises.length > 0) {
                    console_1.default.info(" Checking if the images are working...");
                    for (const index in exercises) {
                        if (Object.prototype.hasOwnProperty.call(exercises, index)) {
                            const exercise = exercises[index];
                            if (!(0, exercise_1.validateExerciseDirectoryName)(exercise.title))
                                errors.push({
                                    exercise: exercise.title,
                                    msg: `The exercise ${exercise.title} has an invalid name.`,
                                });
                            let readmeFilesCount = { exercise: exercise.title, count: 0 };
                            if (Object.keys(exercise.translations).length === 0)
                                errors.push({
                                    exercise: exercise.title,
                                    msg: `The exercise ${exercise.title} doesn't have a README.md file.`,
                                });
                            if (exercise.language === "python3" ||
                                exercise.language === "python") {
                                for (const f of exercise.files.map(f => f)) {
                                    if (f.path.includes("test.py") ||
                                        f.path.includes("tests.py")) {
                                        const content = fs.readFileSync(f.path).toString();
                                        const isEmpty = audit_1.default.checkForEmptySpaces(content);
                                        if (isEmpty || !content)
                                            errors.push({
                                                exercise: exercise.title,
                                                msg: `This file (${f.name}) doesn't have any content inside.`,
                                            });
                                    }
                                }
                            }
                            else {
                                for (const f of exercise.files.map(f => f)) {
                                    if (f.path.includes("test.js") ||
                                        f.path.includes("tests.js")) {
                                        const content = fs.readFileSync(f.path).toString();
                                        const isEmpty = audit_1.default.checkForEmptySpaces(content);
                                        if (isEmpty || !content)
                                            errors.push({
                                                exercise: exercise.title,
                                                msg: `This file (${f.name}) doesn't have any content inside.`,
                                            });
                                    }
                                }
                            }
                            for (const lang in exercise.translations) {
                                if (Object.prototype.hasOwnProperty.call(exercise.translations, lang)) {
                                    const files = [];
                                    const findResultPromises = [];
                                    for (const file of exercise.files) {
                                        const found = find(file, exercise.translations[lang], exercise);
                                        findResultPromises.push(found);
                                    }
                                    // eslint-disable-next-line
                                    let findResults = await Promise.all(findResultPromises);
                                    for (const found of findResults) {
                                        if (found) {
                                            readmeFilesCount = Object.assign(Object.assign({}, readmeFilesCount), { count: readmeFilesCount.count + 1 });
                                            files.push(found);
                                        }
                                    }
                                    if (!files.includes(true))
                                        errors.push({
                                            exercise: exercise.title,
                                            msg: "This exercise doesn't have a README.md file.",
                                        });
                                }
                            }
                            readmeFiles.push(readmeFilesCount);
                        }
                    }
                }
                else
                    errors.push({
                        exercise: undefined,
                        msg: "The exercises array is empty.",
                    });
                console_1.default.log(`${counter.images.total - counter.images.error} images ok from ${counter.images.total}`);
                console_1.default.info(" Checking if important files are missing... (README's, translations, gitignore...)");
                // Check if all the exercises has the same ammount of README's, this way we can check if they have the same ammount of translations.
                const files = [];
                let count = 0;
                for (const item of readmeFiles) {
                    if (count < item.count)
                        count = item.count;
                }
                for (const item of readmeFiles) {
                    if (item.count !== count)
                        files.push(` ${item.exercise}`);
                }
                if (files.length > 0) {
                    const filesString = files.join(",");
                    warnings.push({
                        exercise: undefined,
                        msg: files.length === 1 ?
                            `This exercise is missing translations:${filesString}` :
                            `These exercises are missing translations:${filesString}`,
                    });
                }
                // Checks if the .gitignore file exists.
                if (!fs.existsSync(".gitignore"))
                    warnings.push({
                        exercise: undefined,
                        msg: ".gitignore file doesn't exist",
                    });
                counter.exercises = exercises.length;
                for (const readme of readmeFiles) {
                    counter.readmeFiles += readme.count;
                }
            }
            else {
                // This is the audit code for Projects
                // Checking the "learn.json" file:
                let learnjson;
                try {
                    learnjson = JSON.parse(fs.readFileSync("./learn.json").toString());
                }
                catch (_error) {
                    console_1.default.error("Invalid JSON syntax in learn.json file:", _error.message);
                    process.exit(1);
                }
                if (!learnjson) {
                    console_1.default.error("There is no learn.json file located in the root of the project.");
                    process.exit(1);
                }
                // Checking the README.md files and possible translations.
                let readmeFiles = [];
                const translations = [];
                const translationRegex = /README\.([a-z]{2,3})\.md/;
                try {
                    const data = await fs.promises.readdir("./");
                    readmeFiles = data.filter(file => file.includes("README"));
                    if (readmeFiles.length === 0)
                        errors.push({
                            exercise: undefined,
                            msg: `There is no README file in the repository.`,
                        });
                }
                catch (error) {
                    if (error)
                        console_1.default.error("There was an error getting the directory files", error);
                }
                for (const readmeFile of readmeFiles) {
                    // Checking the language of each README file.
                    if (readmeFile === "README.md")
                        translations.push("en");
                    else {
                        const regexGroups = translationRegex.exec(readmeFile);
                        if (regexGroups)
                            translations.push(regexGroups[1]);
                    }
                    const readme = fs.readFileSync(path.resolve(readmeFile)).toString();
                    const isEmpty = audit_1.default.checkForEmptySpaces(readme);
                    if (isEmpty || !readme) {
                        errors.push({
                            exercise: undefined,
                            msg: `This file "${readmeFile}" doesn't have any content inside.`,
                        });
                        continue;
                    }
                    if (readme.length < 800)
                        errors.push({
                            exercise: undefined,
                            msg: `The "${readmeFile}" file should have at least 800 characters (It currently have: ${readme.length}).`,
                        });
                    // eslint-disable-next-line
                    await audit_1.default.checkUrl(config, path.resolve(readmeFile), readmeFile, undefined, errors, warnings, 
                    // eslint-disable-next-line
                    undefined);
                }
                // Adding the translations to the learn.json
                learnjson.translations = translations;
                // Checking if the preview image (from the learn.json) is OK.
                try {
                    const res = await fetch(learnjson.preview, { method: "HEAD" });
                    if (res.status > 399 && res.status < 500) {
                        errors.push({
                            exercise: undefined,
                            msg: `The link of the "preview" is broken: ${learnjson.preview}`,
                        });
                    }
                }
                catch (_r) {
                    errors.push({
                        exercise: undefined,
                        msg: `The link of the "preview" is broken: ${learnjson.preview}`,
                    });
                }
                const date = new Date();
                learnjson.validationAt = date.getTime();
                if (errors.length > 0)
                    learnjson.validationStatus = "error";
                else if (warnings.length > 0)
                    learnjson.validationStatus = "warning";
                else
                    learnjson.validationStatus = "success";
                // Writes the "learn.json" file with all the new properties
                await fs.promises.writeFile("./learn.json", JSON.stringify(learnjson));
            }
            await audit_1.default.showWarnings(warnings);
            // eslint-disable-next-line
            await audit_1.default.showErrors(errors, undefined);
        }
    }
}
AuditCommand.description = `learnpack audit is the command in charge of creating an auditory of the repository
...
learnpack audit checks for the following information in a repository:
    1. The configuration object has slug, repository and description. (Error)
    2. The command learnpack clean has been run. (Error)
    3. If a markdown or test file doesn't have any content. (Error)
    4. The links are accessing to valid servers. (Error)
    5. The relative images are working (If they have the shortest path to the image or if the images exists in the assets). (Error)
    6. The external images are working (If they are pointing to a valid server). (Error)
    7. The exercises directory names are valid. (Error)
    8. If an exercise doesn't have a README file. (Error)
    9. The exercises array (Of the config file) has content. (Error)
    10. The exercses have the same translations. (Warning)
    11. The .gitignore file exists. (Warning)
    12. If there is a file within the exercises folder but not inside of any particular exercise's folder. (Warning)
`;
AuditCommand.flags = {
    strict: command_1.flags.boolean({
        char: "s",
        description: "strict mode",
        default: false,
    }),
};
exports.default = AuditCommand;
