UNPKG

event-sourcing-generator

Version:

Utils for generating CQRS + Event Sourcing classes for NestJS

74 lines (73 loc) 3.97 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const jsonfile = require("jsonfile"); const glob = require("glob-promise"); const path = require("path"); const bash_comand_1 = require("./bash-comand"); const bin_utils_1 = require("./bin-utils"); const command_options_parser_1 = require("./command-options-parser"); const config_store_1 = require("./config.store"); exports.generateCommands = (program) => __awaiter(void 0, void 0, void 0, function* () { const result = yield bash_comand_1.bashCommand(`${bin_utils_1.getBinaryPath('schematics')} ${config_store_1.config.get('collection.name').value}:.`, ['--list-schematics'], true); const schematics = result.toString().split('\n'); const filteredSchematics = schematics.filter(val => val !== ''); filteredSchematics.map((schematic) => __awaiter(void 0, void 0, void 0, function* () { const schematicDirPath = path.join(bin_utils_1.getLibPath(config_store_1.config.get('collection.name').value), config_store_1.config.get('collection.outDir').value); const files = glob.sync(`**/${schematic}/schema.json`, { cwd: schematicDirPath, absolute: true, }); if (files.length >= 0 && files[0]) { try { const schema = jsonfile.readFileSync(files[0]); const properties = schema['properties']; let optionString = ''; const cmdArgsKeys = []; Object.keys(properties).forEach(key => { if (properties[key].$default && properties[key].$default.$source) { cmdArgsKeys.push(key); optionString += `[${key}] `; } }); const cmd = program .command(`${schematic} ${optionString}`) .passCommandToAction(true) .storeOptionsAsProperties(true); cmd .description(schema['description']); const options = config_store_1.getConfigForSchematic(schematic); const args = command_options_parser_1.parseCommandOptions(options); Object.keys(properties).forEach(key => { cmd.option('--' + key, properties[key].description); }); cmd.option('--dry-run', 'Run the schematic without merging it with the current file tree.', false); cmd.action(() => __awaiter(void 0, void 0, void 0, function* () { if (!cmd.dryRun) { args.push('--debug=false'); } const allArgs = []; for (let i = 0; i < cmd.args.length; i++) { if (i < cmdArgsKeys.length) { allArgs.push(cmd.args[i]); } } allArgs.push(...args); yield bash_comand_1.bashCommand(`${bin_utils_1.getBinaryPath('schematics')} ${config_store_1.config.get('collection.name').value}:${schematic}`, allArgs, false); })); } catch (err) { console.log(err); throw err; } } })); });