UNPKG

@megaorm/cli

Version:

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

62 lines 2.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AddCommandCommand = void 0; const CommandHandler_1 = require("../handlers/CommandHandler"); const MegaCommand_1 = require("../MegaCommand"); const MegaConfig_1 = require("../MegaConfig"); const path_1 = require("path"); /** * Represents a command to add a command file to a specific folder in the project * based on MegaORM configuration. * * @extends MegaCommand */ class AddCommandCommand extends MegaCommand_1.MegaCommand { /** * Resolves the appropriate path for adding a command file based on the configuration. * * @param config The MegaORM configuration object. * @returns The resolved path for the command file. * @throws `MegaCommandError` if `paths.commands` is absolute and TypeScript is enabled. */ static path(config) { if (config.typescript.enabled === true) { // Cannot be absolute if ((0, path_1.isAbsolute)(config.paths.commands)) { throw new MegaCommand_1.MegaCommandError(`paths.commands cannot be absolute if typescript is enabled`); } if ((0, path_1.isAbsolute)(config.typescript.src)) { return (0, path_1.join)(config.typescript.src, config.paths.commands); } return (0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.typescript.src, config.paths.commands); } if ((0, path_1.isAbsolute)(config.paths.commands)) { return config.paths.commands; } return (0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.paths.commands); } /** * Executes the command to add a command. * * @returns A promise that resolves when the command has been added successfully or rejects with an error. */ static exec() { return new Promise((resolve, reject) => { // Command name const n = this.argument('name'); MegaConfig_1.MegaConfig.load() .then((config) => { const p = this.path(config); const ts = config.typescript.enabled; MegaConfig_1.MegaConfig.mkdir(p) .then(() => new CommandHandler_1.CommandHandler().add(n, p, ts)) .then((message) => resolve(this.success(message))) .catch(reject); }) .catch(reject); }); } } exports.AddCommandCommand = AddCommandCommand; AddCommandCommand.syntax = '<! name>'; //# sourceMappingURL=AddCommandCommand.js.map