UNPKG

@megaorm/cli

Version:

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

73 lines 3.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RemoveCommandCommand = 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 remove a command file from specific folders in the project * based on MegaORM configuration. * * @extends MegaCommand */ class RemoveCommandCommand extends MegaCommand_1.MegaCommand { /** * Resolves the appropriate paths for removing command files based on the configuration. * * @param config The MegaORM configuration object. * @returns An array of resolved paths for the command files to be removed. * @throws `MegaCommandError` if `paths.commands` is absolute and TypeScript is enabled. */ static paths(config) { const paths = []; 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)) { paths.push((0, path_1.join)(config.typescript.src, config.paths.commands)); } else { paths.push((0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.typescript.src, config.paths.commands)); } if ((0, path_1.isAbsolute)(config.typescript.dist)) { paths.push((0, path_1.join)(config.typescript.dist, config.paths.commands)); } else { paths.push((0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.typescript.dist, config.paths.commands)); } return paths; } if ((0, path_1.isAbsolute)(config.paths.commands)) { paths.push(config.paths.commands); return paths; } paths.push((0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.paths.commands)); return paths; } /** * Removes the command file with the specified name. * * @returns A promise that resolves when the command files have been successfully removed or rejects with an error. */ static exec() { return new Promise((resolve, reject) => { const n = this.argument('name'); MegaConfig_1.MegaConfig.load() .then((config) => { const ps = this.paths(config); const handler = new CommandHandler_1.CommandHandler(); MegaConfig_1.MegaConfig.existMany(ps) .then(() => Promise.all(ps.map((p) => handler.remove(n, p)))) .then((messages) => resolve(this.success(messages.join('\n')))) .catch(reject); }) .catch(reject); }); } } exports.RemoveCommandCommand = RemoveCommandCommand; RemoveCommandCommand.syntax = '<! name>'; //# sourceMappingURL=RemoveCommandCommand.js.map