bedrock-development
Version:
APIs for creating and editing files related to Minecraft Bedrock development.
78 lines (77 loc) • 3.52 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { Command } from 'commander';
import { printVersion } from './version.js';
/**
* @remarks A static class used to store information about commands, allowing for easy command copying.
*/
export class CommandMap {
/**
* @remarks Adds a command to the registry.
* @param name The name of the new command to add, note this is the internal registry name, not the display name.
* @param commandEntry The command data.
*/
static addCommand(name, commandEntry) {
var _a;
commandEntry.command = (_a = commandEntry.command) !== null && _a !== void 0 ? _a : new Command();
commandEntry.commandOptions(commandEntry.command);
if (commandEntry.commandAction) {
commandEntry.command.action(commandEntry.commandAction);
commandEntry.command.hook('postAction', printVersion);
}
;
if (commandEntry.parent) {
commandEntry.parent.addCommand(commandEntry.command);
}
this.entries[name] = commandEntry;
}
/**
* @remarks Gets command data.
* @param name The name of the command to return.
* @returns The data associated with the command or undefined if no command with that name exists.
*/
static getCommandEntry(name) {
return this.entries[name];
}
/**
* @remarks Copies a command in the registery, allows for cross-subcommand aliases.
* @param name The internal name of the new command.
* @param source The internal name of the command to copy.
* @param commandName The display name of the new command.
* @param parent The parent the new command should be anchored to.
*/
static copyCommand(name, source, commandName, parent) {
return __awaiter(this, void 0, void 0, function* () {
yield new Promise(resolve => setTimeout(resolve, 1)); // Wait to perform copy until all the base commands were added
const sourceEntry = this.getCommandEntry(source);
if (sourceEntry) {
const entry = {
command: new Command(),
parent: parent,
commandOptions: sourceEntry.commandOptions,
commandAction: sourceEntry.commandAction,
};
entry.commandOptions(entry.command);
entry.command.name(commandName);
if (entry.commandAction) {
entry.command.action(entry.commandAction);
entry.command.hook('postAction', printVersion);
}
;
if (parent) {
entry.parent = parent;
parent.addCommand(entry.command);
}
this.entries[name] = entry;
}
});
}
}
CommandMap.entries = {};