UNPKG

acyort

Version:

A Node.js static website framework

64 lines (60 loc) 2.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class C { constructor() { this.commands = []; this.options = []; } register(type, context) { if (type !== 'command' && type !== 'option') { throw new Error(`not supports type: ${type}`); } if (type === 'option') { const { name, alias } = context; if (!name.startsWith('--') || !alias.startsWith('-')) { throw new Error(`option error: ${name}, ${alias}`); } const optionExist = this.options.find((o) => o.name === name || o.alias === alias); if (optionExist) { throw new Error(`option exists: ${name}, ${alias}`); } this.options.push(context); } if (type === 'command') { const commandExist = this.commands.find((c) => c.name === context.name); if (commandExist) { throw new Error(`command exists: ${context.name}`); } this.commands.push(context); } } getOption(nameOrAlias) { return this.options .find((o) => o.alias.slice(1) === nameOrAlias || o.name.slice(2) === nameOrAlias); } getCommand(name) { return this.commands.find((c) => c.name === name); } getHelp() { const { commands, options } = this; const width = 24; let help = ` AcyOrt, A Node.js extensible framework Commands:`; commands.forEach(({ name, description }) => { help += ` ${name}${new Array(width - name.length).fill(' ').join('')}${description}`; }); help += ` Options:`; options.forEach(({ name, alias, description }) => { help += ` ${name}, ${alias}${new Array(width - 2 - name.length - alias.length).fill(' ').join('')}${description}`; }); help += ` For more information, check the docs: https://acyort.js.org `; return help; } } exports.default = new C();