UNPKG

@megaorm/cli

Version:

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

73 lines 3.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RemoveModelCommand = void 0; const ModelHandler_1 = require("../handlers/ModelHandler"); const MegaCommand_1 = require("../MegaCommand"); const MegaConfig_1 = require("../MegaConfig"); const path_1 = require("path"); /** * Represents a command to remove a model file from specific folders in the project * based on MegaORM configuration. * * @extends MegaCommand */ class RemoveModelCommand extends MegaCommand_1.MegaCommand { /** * Resolves the appropriate paths for removing model files based on the configuration. * * @param config The MegaORM configuration object. * @returns An array of resolved paths for the model files to be removed. * @throws `MegaCommandError` if `paths.models` 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.models)) { throw new MegaCommand_1.MegaCommandError(`paths.models 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.models)); } else { paths.push((0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.typescript.src, config.paths.models)); } if ((0, path_1.isAbsolute)(config.typescript.dist)) { paths.push((0, path_1.join)(config.typescript.dist, config.paths.models)); } else { paths.push((0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.typescript.dist, config.paths.models)); } return paths; } if ((0, path_1.isAbsolute)(config.paths.models)) { paths.push(config.paths.models); return paths; } paths.push((0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.paths.models)); return paths; } /** * Removes the model file associated with the specified table name. * * @returns A promise that resolves when the model 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); const handler = new ModelHandler_1.ModelHandler(); MegaConfig_1.MegaConfig.existMany(ps) .then(() => Promise.all(ps.map((p) => handler.remove(t, p)))) .then((messages) => resolve(this.success(messages.join('\n')))) .catch(reject); }) .catch(reject); }); } } exports.RemoveModelCommand = RemoveModelCommand; RemoveModelCommand.syntax = '<! table>'; //# sourceMappingURL=RemoveModelCommand.js.map