UNPKG

@roboplay/sage

Version:
98 lines (96 loc) 3.81 kB
import { Command } from 'commander'; import { writeFile } from 'node:fs/promises'; import { logger } from '../core/logger.js'; import path from 'node:path'; import { Compiler } from 'robo.js/dist/cli/utils/compiler.js'; const command = new Command("generate"); command.command("docs").description("generates a basic doc file for the project").action(generateDocAction); var generate_default = command; async function generateDocAction() { try { const manifest = await Compiler.useManifest(); const manifestCommands = manifest.commands; const manifestEvents = manifest.events; const manifestContextCommands = manifest.context; let table = ""; const displayOptions = (options, required) => { const str = []; if (options && options.length > 0) { options.forEach((option) => { if (required) { str.push(option.required.toString()); } else { str.push(option.name); } }); } return str.length > 0 ? str.join(",") : "no options"; }; const displayCommands = (command2, commandKey) => { const subCommandsNames = []; let subCommand = command2.subcommands; while (subCommand) { const s = Object.values(subCommand)[0]; if (s.subcommands === void 0) break; subCommandsNames.push(Object.keys(subCommand)[0]); subCommand = s.subcommands; } const cmds = subCommand ? Object.entries(subCommand) : []; if (cmds.length > 0) { cmds.forEach((command3) => { const desc = command3[1].description ? command3[1].description : "no description available"; table += `|${commandKey} ${subCommandsNames.join(" ")} ${command3[0]} |${displayOptions( command3[1].options )}|${displayOptions(command3[1].options, true)}|${desc}| `; }); } else { const desc = command2.description ? command2.description : "no description available"; table += `|${commandKey} ${subCommandsNames.join(" ")} |${displayOptions(command2.options)}|${displayOptions( command2.options, true )}|${desc}| `; } }; if (Object.entries(manifestCommands).length > 0) { table = `# Slash commands: | Name | Options | Required | Description | | ----------- | ----------- | ----------- | ----------- | `; for (const [commandKey, commandValue] of Object.entries(manifestCommands)) { if (!commandValue["__auto"]) { displayCommands(commandValue, commandKey); } } } for (const [contextKey, contextValue] of Object.entries(manifestContextCommands)) { if (Object.entries(contextValue).length > 0) { table += "\n\n# Context commands:\n|Commands | Context | Description |\n| ----------- | ----------- | ----------- |\n"; for (const [contextCommandKey, contextCommandValue] of Object.entries(contextValue)) { const desc = contextCommandValue.description ? contextCommandValue.description : "no description available"; table += `|${contextCommandKey}|${contextKey}|${desc}| `; } } } if (Object.entries(manifestEvents).length > 0) { table += "\n\n# Events used:\n| Name | Description |\n| ----------- | ----------- |\n"; for (const [eventKey, eventValue] of Object.entries(manifestEvents)) { const desc = eventValue.description ? eventValue.description : "no description available"; table += `|${eventKey}|${desc}| `; } } if (table.length > 0) { await writeFile(path.join(process.cwd(), "DOCUMENTATION.md"), table); } } catch (e) { logger.error(e); process.exit(1); } } export { generate_default as default }; //# sourceMappingURL=out.js.map //# sourceMappingURL=generate.js.map