@megaorm/cli
Version:
This package allows you to communicate with MegaORM via commands directly from the command line interface (CLI).
73 lines • 3.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenerateCommand = 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 execute generator files in the project's generators folder.
*
* The command performs the following actions:
* - Resolves the path to the generators folder based on the configuration.
* - Identifies generator files that have not been executed yet.
* - Executes only the new generator files to create tables.
*
* This ensures that previously executed generator files are not re-executed, maintaining efficiency
* and avoiding duplication of database operations.
*
* @extends MegaCommand
*/
class GenerateCommand extends MegaCommand_1.MegaCommand {
/**
* Resolves the appropriate path for the generators folder based on the configuration.
*
* @param config The MegaORM configuration object.
* @returns The resolved path for the generators folder.
* @throws `MegaCommandError` if `paths.generators` is absolute and TypeScript is enabled.
*/
static path(config) {
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.dist)) {
return (0, path_1.join)(config.typescript.dist, config.paths.generators);
}
return (0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.typescript.dist, config.paths.generators);
}
if ((0, path_1.isAbsolute)(config.paths.generators)) {
return config.paths.generators;
}
return (0, path_1.resolve)(MegaConfig_1.MegaConfig.resolveSync(), config.paths.generators);
}
/**
* Executes the command to process generator files.
*
* The command identifies generator files that have not been executed yet
* and executes them to create tables as defined in the files.
*
* @returns A promise that resolves when the new generator files have been executed successfully
* or rejects with an error.
*/
static exec() {
return new Promise((resolve, reject) => {
MegaConfig_1.MegaConfig.load()
.then((config) => {
const p = this.path(config);
MegaConfig_1.MegaConfig.exist(p)
.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) => handler.generate(p))
.then((message) => resolve(this.success(message)))
.catch(reject);
})
.catch(reject);
});
}
}
exports.GenerateCommand = GenerateCommand;
//# sourceMappingURL=GenerateCommand.js.map