UNPKG

@averagehelper/corde

Version:

A simple library for Discord bot tests. (Republished fork to demonstrate a bugfix)

80 lines (79 loc) 3.11 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.validate = void 0; const chalk_1 = __importDefault(require("chalk")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const errors_1 = require("../errors"); /** * Check if configs are valid. Throws a exception * if there is no parameter or if any required property is * missing. * * @version 1.0 * * @param configs Config parameter that will be validated * * @throws Error if any config is invalid. */ function validate(configs) { if (!configs) { throw new errors_1.FileError(chalk_1.default.red("● configs not informed.")); } const errors = []; addToErrorsIfPropertyIsMissing(configs.botPrefix, errors, "bot prefix"); addToErrorsIfPropertyIsMissing(configs.botTestId, errors, "bot test ID"); addToErrorsIfPropertyIsMissing(configs.channelId, errors, "channel ID"); addToErrorsIfPropertyIsMissing(configs.cordeTestToken, errors, "corde token"); addToErrorsIfPropertyIsMissing(configs.guildId, errors, "guild ID"); addToErrorsIfPropertyIsMissing(configs.botTestToken, errors, "bot test token"); validatePaths(configs.testFiles, errors); let errorsString = ""; if (errors.length === 1) { errorsString = chalk_1.default.red("\n● An required property is missing in config file:"); buildMissingPropertiesErrorAndThrow(errorsString, errors); } if (errors.length > 1) { errorsString = chalk_1.default.red("\n● Some required properties are missing in config file:"); buildMissingPropertiesErrorAndThrow(errorsString, errors); } } exports.validate = validate; function validatePaths(pathsDir, errors) { if (!pathsDir || pathsDir.length === 0) { errors.push("No test files informed"); return; } for (const pathDir of pathsDir) { const pathResolved = path_1.default.resolve(process.cwd(), pathDir); if (fs_1.default.existsSync(pathResolved)) { const stats = fs_1.default.lstatSync(pathResolved); if (stats.isDirectory()) { const files = fs_1.default.readdirSync(pathResolved); const filesResolve = []; for (const file of files) { filesResolve.push(path_1.default.resolve(pathResolved, file)); } validatePaths(filesResolve, errors); } } else { errors.push(`path: ${pathDir} does not exists`); } } } function addToErrorsIfPropertyIsMissing(value, errors, message) { if (!isStringValid(value)) { errors.push(message); } } function isStringValid(value) { return value && value.trim() !== ""; } function buildMissingPropertiesErrorAndThrow(errorString, erros) { erros.forEach((error) => (errorString += `\n${chalk_1.default.red(`- ${error}`)}`)); throw new errors_1.PropertyError(errorString); }