UNPKG

@roboplay/sage

Version:
125 lines (123 loc) 4.92 kB
import { color } from 'robo.js'; import { Compiler } from 'robo.js/dist/cli/utils/compiler.js'; import { Command } from 'commander'; import { logger } from '../core/logger.js'; import { checkSageUpdates } from '../core/utils.js'; const command = new Command("why").arguments("[entities...]").description( "Find out why a command, event, permission, or scope is in your Robo. e.g. /ping, @ready, %ADMINISTRATOR, +applications.commands" ).option("-ns --no-self-check", "do not check for updates to Sage CLI").option("-s --silent", "do not print anything").option("-v --verbose", "print more information for debugging").action(whyAction); var why_default = command; const validPrefixes = [ { symbol: "/", full: "command:", symbolMinLength: 2, fullMinLength: 9 }, { symbol: "@", full: "event:", symbolMinLength: 2, fullMinLength: 7 }, { symbol: "%", full: "permission:", symbolMinLength: 2, fullMinLength: 12 }, { symbol: "+", full: "scope:", symbolMinLength: 2, fullMinLength: 7 } ]; async function whyAction(entities, options) { logger({ enabled: !options.silent, level: options.verbose ? "debug" : "info" }); logger.debug(`CLI Options:`, options); if (options.selfCheck) { await checkSageUpdates(); } const text = entities[0]; logger.debug(`> "${text}"`); if (!text) { logger.error("Please provide a command, event, permission, or scope."); process.exit(1); } let prefixType = null; for (const prefix of validPrefixes) { if (text.startsWith(prefix.symbol) || text.startsWith(prefix.full)) { if (text.startsWith(prefix.symbol) && text.length >= prefix.symbolMinLength || text.startsWith(prefix.full) && text.length >= prefix.fullMinLength) { prefixType = prefix; break; } else { logger.error(`Please provide a ${prefix.full.slice(0, -1)} name.`); process.exit(1); } } } if (!prefixType) { logger.error("Please provide a command, event, permission, or scope."); process.exit(1); } const entity = text.startsWith(prefixType.full) ? text.slice(prefixType.full.length) : text.slice(prefixType.symbol.length); logger.debug(`Searching for ${prefixType.full.replace(":", "")} ${color.blue(entity)}...`); const manifest = await Compiler.useManifest(); logger.debug(`Manifest: ${JSON.stringify(manifest, null, 2)}`); if (prefixType.full === "command:") { const command2 = manifest.commands[entity]; if (!command2) { logger.info("This command does not exist."); } else if (command2.__plugin) { logger.info(`This command is provided by the ${color.blue(command2.__plugin.name)} plugin.`); } else if (command2.__auto) { logger.info( "This is a default command. You can override it by creating a command with the same name or disable it in your config file." ); } else { logger.info( `This command exists in your Robo because you created it: ${color.blue("/src/commands/" + command2.__path)}` ); } } else if (prefixType.full === "event:") { const event = manifest.events[entity]; if (!event) { logger.info("This event does not exist."); return; } const events = Array.isArray(event) ? event : [event]; const plugins = []; const defaults = []; const custom = []; const files = []; for (const event2 of events) { if (event2.__plugin) { plugins.push(event2); files.push(event2.__plugin.name + " (" + event2.__path + ")"); } else if (event2.__auto) { defaults.push(event2); files.push("default " + event2.__path); } else { custom.push(event2); files.push("/src/events/" + event2.__path); } } logger.info(`This event is being handled by the following: `); if (plugins.length) { logger.log(" " + color.bold(`Plugins`)); plugins.forEach((e) => { logger.log(` ${color.blue(e.__plugin.name) + ":"}`, e.__path); }); logger.log(""); } if (defaults.length) { logger.log(" " + color.bold(`Default config`)); defaults.forEach((e) => { logger.log(` ${color.blue("\u0394")}`, e.__path); }); logger.log(""); } if (custom.length) { logger.log(" " + color.bold(`Files`)); custom.forEach((e) => { logger.log(` ${color.blue("/src/events/" + e.__path)}`); }); logger.log(""); } } else if (prefixType.full === "permission:") ; else if (prefixType.full === "scope:") ; else { const entityData = manifest[prefixType.full.slice(0, -1)]?.[entity]; logger.debug(`Entity data:`, entityData); if (!entityData) { logger.error(`Could not find ${prefixType.full.replace(":", "")} ${color.blue(entity)}.`); process.exit(1); } } } export { why_default as default }; //# sourceMappingURL=out.js.map //# sourceMappingURL=why.js.map