UNPKG

@megaorm/cli

Version:

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

76 lines 3.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RemoveGeneratorCommand = void 0; const GeneratorHandler_1 = require("../handlers/GeneratorHandler"); const builder_1 = require("@megaorm/builder"); const MegaCommand_1 = require("../MegaCommand"); const MegaConfig_1 = require("../MegaConfig"); const path_1 = require("path"); /** * Represents a command to remove a generator file from specific folders in the project * based on MegaORM configuration. * * @extends MegaCommand */ class RemoveGeneratorCommand extends MegaCommand_1.MegaCommand { /** * Resolves the appropriate paths for removing generator files based on the configuration. * * @param config The MegaORM configuration object. * @returns An array of resolved paths for the generator files to be removed. * @throws `MegaCommandError` if `paths.generators` 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.generators)) { throw new MegaCommand_1.MegaCommandError(`paths.generators 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.generators)); } else { paths.push((0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.typescript.src, config.paths.generators)); } if ((0, path_1.isAbsolute)(config.typescript.dist)) { paths.push((0, path_1.join)(config.typescript.dist, config.paths.generators)); } else { paths.push((0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.typescript.dist, config.paths.generators)); } return paths; } if ((0, path_1.isAbsolute)(config.paths.generators)) { paths.push(config.paths.generators); return paths; } paths.push((0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.paths.generators)); return paths; } /** * Removes the generator file associated with the specified table name. * * @returns A promise that resolves when the generator files have been successfully removed or rejects with an error. */ static exec() { return new Promise((resolve, reject) => { const t = this.argument('table'); MegaConfig_1.MegaConfig.load() .then((config) => { const ps = this.paths(config); MegaConfig_1.MegaConfig.existMany(ps) .then(() => config.cluster.request(config.default)) .then((con) => Promise.resolve(new builder_1.MegaBuilder(con))) .then((builder) => Promise.resolve(new GeneratorHandler_1.GeneratorHandler(builder))) .then((handler) => Promise.all(ps.map((p) => handler.remove(t, p)))) .then((messages) => resolve(this.success(messages.join('\n')))) .catch(reject); }) .catch(reject); }); } } exports.RemoveGeneratorCommand = RemoveGeneratorCommand; RemoveGeneratorCommand.syntax = '<! table>'; //# sourceMappingURL=RemoveGeneratorCommand.js.map