UNPKG

@megaorm/cli

Version:

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

76 lines 3.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RemoveSeederCommand = void 0; const SeederHandler_1 = require("../handlers/SeederHandler"); 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 seeder file from specific folders in the project * based on MegaORM configuration. * * @extends MegaCommand */ class RemoveSeederCommand extends MegaCommand_1.MegaCommand { /** * Resolves the appropriate paths for removing seeder files based on the configuration. * * @param config The MegaORM configuration object. * @returns An array of resolved paths for the seeder files to be removed. * @throws `MegaCommandError` if `paths.seeders` 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.seeders)) { throw new MegaCommand_1.MegaCommandError(`paths.seeders 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.seeders)); } else { paths.push((0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.typescript.src, config.paths.seeders)); } if ((0, path_1.isAbsolute)(config.typescript.dist)) { paths.push((0, path_1.join)(config.typescript.dist, config.paths.seeders)); } else { paths.push((0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.typescript.dist, config.paths.seeders)); } return paths; } if ((0, path_1.isAbsolute)(config.paths.seeders)) { paths.push(config.paths.seeders); return paths; } paths.push((0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.paths.seeders)); return paths; } /** * Removes the seeder file associated with the specified table name. * * @returns A promise that resolves when the seeder 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 SeederHandler_1.SeederHandler(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.RemoveSeederCommand = RemoveSeederCommand; RemoveSeederCommand.syntax = '<! table>'; //# sourceMappingURL=RemoveSeederCommand.js.map