UNPKG

corde

Version:

A simple library for Discord bot tests

157 lines (120 loc) 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true, }); exports.Config = void 0; const tslib_1 = require("tslib"); const path_1 = (0, tslib_1.__importDefault)(require("path")); const consts_1 = require("../consts"); const utils_1 = require("../utils"); class Config { constructor() { this.setConfigs(consts_1.DEFAULT_CONFIG, true); } get cordeBotToken() { return this._cordeBotToken; } get botTestId() { return this._botTestId; } get channelId() { return this._channelId; } get guildId() { return this._guildId; } get timeout() { return this._timeout; } get botPrefix() { return this._botPrefix; } get testMatches() { return this._testMatches; } get modulePathIgnorePatterns() { return this._modulePathIgnorePatterns; } get project() { return this._project; } get exitOnFileReadingError() { return this._exitOnFileReadingError; } get extensions() { return this._extensions; } get rootDir() { return this._rootDir; } get(configName) { return this[configName]; } setConfigs(config, forceUpdate) { if (config.rootDir && (!this.rootDir || forceUpdate)) { this._rootDir = path_1.default.resolve(process.cwd(), config.rootDir); } if (config.cordeBotToken && (!this.cordeBotToken || forceUpdate)) { this._cordeBotToken = config.cordeBotToken; } if (config.botPrefix && (!this.botPrefix || forceUpdate)) { this._botPrefix = config.botPrefix; } if (config.project && (!this.project || forceUpdate)) { this._project = path_1.default.normalize( (0, utils_1.replaceAll)(config.project, consts_1.ROOT_DIR, this.rootDir), ); } if (config.botTestId && (!this.botTestId || forceUpdate)) { this._botTestId = config.botTestId; } if (config.extensions && (!this.extensions || forceUpdate)) { this._extensions = config.extensions; } if ( typeof config.exitOnFileReadingError !== "undefined" && (!this.exitOnFileReadingError || forceUpdate) ) { this._exitOnFileReadingError = config.exitOnFileReadingError; } if (config.channelId && (!this.channelId || forceUpdate)) { this._channelId = config.channelId; } if (config.guildId && (!this.guildId || forceUpdate)) { this._guildId = config.guildId; } if ((0, utils_1.isNumber)(config.timeout) && (!this.timeout || forceUpdate)) { this._timeout = +config.timeout; } if (config.botPrefix && (!this.botPrefix || forceUpdate)) { this._botPrefix = config.botPrefix; } if (config.modulePathIgnorePatterns && (!this.modulePathIgnorePatterns || forceUpdate)) { this._modulePathIgnorePatterns = config.modulePathIgnorePatterns; this._modulePathIgnorePatterns = this.clearArray(this._modulePathIgnorePatterns); } if (config.testMatches && (!this.testMatches || this.testMatches.length === 0 || forceUpdate)) { if (Array.isArray(config.testMatches)) { this._testMatches = this.getArrayWithRootReplaced(config.testMatches); this._testMatches = this.clearArray(this._testMatches); } else { this._testMatches = []; } } } clearArray(array) { const cleaned = array.filter((val) => val); return [...new Set(cleaned)]; } getArrayWithRootReplaced(array) { return array.map((val) => { if (val.includes(consts_1.ROOT_DIR)) { return path_1.default.normalize( (0, utils_1.replaceAll)(val, consts_1.ROOT_DIR, this._rootDir), ); } return val; }); } } exports.Config = Config;