UNPKG

@megaorm/cli

Version:

This package allows you to communicate with MegaORM via commands directly from the command line interface (CLI).

127 lines 6.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.execute = exports.register = exports.MegaExecutor = exports.MegaExecutorError = void 0; const MegaCommand_1 = require("./MegaCommand"); // Commands const AddCommandCommand_1 = require("./commands/AddCommandCommand"); const AddGeneratorCommand_1 = require("./commands/AddGeneratorCommand"); const AddModelCommand_1 = require("./commands/AddModelCommand"); const AddSeederCommand_1 = require("./commands/AddSeederCommand"); const ClearCommand_1 = require("./commands/ClearCommand"); const FetchCommand_1 = require("./commands/FetchCommand"); const GenerateCommand_1 = require("./commands/GenerateCommand"); const RemoveCommandCommand_1 = require("./commands/RemoveCommandCommand"); const RemoveGeneratorCommand_1 = require("./commands/RemoveGeneratorCommand"); const RemoveModelCommand_1 = require("./commands/RemoveModelCommand"); const RemoveSeederCommand_1 = require("./commands/RemoveSeederCommand"); const ResetCommand_1 = require("./commands/ResetCommand"); const RollbackCommand_1 = require("./commands/RollbackCommand"); const SeedCommand_1 = require("./commands/SeedCommand"); const AddForCommand_1 = require("./commands/AddForCommand"); const RemoveForCommand_1 = require("./commands/RemoveForCommand"); const VersionCommand_1 = require("./commands/VersionCommand"); const test_1 = require("@megaorm/test"); /** * Custom error class for errors related to the MegaExecutor. */ class MegaExecutorError extends Error { } exports.MegaExecutorError = MegaExecutorError; /** * MegaExecutor is a class responsible for registering and executing commands in MegaORM. * It allows commands to be registered with a specific name and executed based on the provided name. */ class MegaExecutor { /** * Registers a command under a specific name. If the name is already registered or the command is invalid, an error is thrown. * * @param name The name of the command. * @param command The `MegaCommand` subclass. * @throws `MegaExecutorError` if the name is invalid, the command class is not a subclass of `MegaCommand`, or if the name already exists. */ static register(name, command) { if (!(0, test_1.isSubclass)(command, MegaCommand_1.MegaCommand)) { throw new MegaExecutorError(`Invalid command: ${String(command)}`); } if (!(0, test_1.isFullStr)(name)) { throw new MegaExecutorError(`Invalid command name: ${String(name)}`); } const cmd = this.commands.find((command) => command.name === name); if ((0, test_1.isDefined)(cmd)) { throw new MegaExecutorError(`Deplicated command names: ${name}`); } this.commands.push({ name, command }); } /** * Executes the command associated with the argument passed from the command line. * * @returns A promise that resolves when the command finishes execution, or rejects with an error. * @throws `MegaExecutorError`if the command name is undefined, the command is unknown, or the `exec` method is invalid. */ static execute() { return new Promise((resolve, reject) => { var _a; const name = process.argv[2]; if ((0, test_1.isUndefined)(name)) { return reject(new MegaExecutorError('Undefined command name')); } const command = (_a = this.commands.find((cmd) => cmd.name === name)) === null || _a === void 0 ? void 0 : _a.command; if ((0, test_1.isUndefined)(command)) { return reject(new MegaExecutorError(`Unknown command: ${name}`)); } const value = command.exec(); if (!(0, test_1.isPromise)(value)) return resolve(value); return value.then(resolve).catch(reject); }); } } exports.MegaExecutor = MegaExecutor; /** * List of registered commands with their names and associated MegaCommand classes. * * @private * @static */ MegaExecutor.commands = [ { name: 'add:command', command: AddCommandCommand_1.AddCommandCommand }, { name: 'add:cmd', command: AddCommandCommand_1.AddCommandCommand }, { name: 'add:model', command: AddModelCommand_1.AddModelCommand }, { name: 'add:seeder', command: AddSeederCommand_1.AddSeederCommand }, { name: 'add:generator', command: AddGeneratorCommand_1.AddGeneratorCommand }, { name: 'add:gen', command: AddGeneratorCommand_1.AddGeneratorCommand }, { name: 'add:for', command: AddForCommand_1.AddForCommand }, { name: 'remove:command', command: RemoveCommandCommand_1.RemoveCommandCommand }, { name: 'remove:cmd', command: RemoveCommandCommand_1.RemoveCommandCommand }, { name: 'remove:model', command: RemoveModelCommand_1.RemoveModelCommand }, { name: 'remove:seeder', command: RemoveSeederCommand_1.RemoveSeederCommand }, { name: 'remove:generator', command: RemoveGeneratorCommand_1.RemoveGeneratorCommand }, { name: 'remove:gen', command: RemoveGeneratorCommand_1.RemoveGeneratorCommand }, { name: 'remove:for', command: RemoveForCommand_1.RemoveForCommand }, { name: 'generate', command: GenerateCommand_1.GenerateCommand }, { name: 'gen', command: GenerateCommand_1.GenerateCommand }, { name: 'rollback', command: RollbackCommand_1.RollbackCommand }, { name: 'roll', command: RollbackCommand_1.RollbackCommand }, { name: 'reset', command: ResetCommand_1.ResetCommand }, { name: 'seed', command: SeedCommand_1.SeedCommand }, { name: 'clear', command: ClearCommand_1.ClearCommand }, { name: 'fetch', command: FetchCommand_1.FetchCommand }, { name: 'version', command: VersionCommand_1.VersionCommand }, { name: 'v', command: VersionCommand_1.VersionCommand }, ]; /** * Registers a command under a specific name. If the name is already registered or the command is invalid, an error is thrown. * * @param name The name of the command. * @param command The `MegaCommand` subclass. * @throws `MegaExecutorError` if the name is invalid, the command class is not a subclass of `MegaCommand`, or if the name already exists. */ exports.register = MegaExecutor.register.bind(MegaExecutor); /** * Executes the command associated with the argument passed from the command line. * * @returns A promise that resolves when the command finishes execution, or rejects with an error. * @throws `MegaExecutorError`if the command name is undefined, the command is unknown, or the `exec` method is invalid. */ exports.execute = MegaExecutor.execute.bind(MegaExecutor); //# sourceMappingURL=MegaExecutor.js.map