UNPKG

@lvce-editor/command

Version:

Command module

25 lines (21 loc) 531 B
class CommandNotFoundError extends Error { constructor(command) { super(`Command not found ${command}`); this.name = 'CommandNotFoundError'; } } const commands = Object.create(null); const register = commandMap => { Object.assign(commands, commandMap); }; const getCommand = key => { return commands[key]; }; const execute = (command, ...args) => { const fn = getCommand(command); if (!fn) { throw new CommandNotFoundError(command); } return fn(...args); }; export { execute, getCommand, register };