hades-cli
Version:
Hades CLI developer tool
129 lines (128 loc) • 7.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Operations = void 0;
require("reflect-metadata");
const tsyringe_1 = require("tsyringe");
const state_service_1 = require("./../services/state.service");
const types_1 = require("./../types");
const template_generator_1 = require("./../utils/template-generator");
const code_writer_1 = require("./code-writer");
const cliter_config_1 = require("./../config/cliter.config");
const child = require("child_process");
const fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
const _ = require("lodash");
class Operations {
async generateModule() {
// generate module files
await this.generateModuleFiles();
// generate intermediate tables
await this.generateIntermediateTables();
// generate framework files
await this.generateFrameworkFiles();
// create references, write imports in ts files
this.createReferences();
// flag to generate e2e tests, this test can overwrite custom tests
if (Operations.stateService.flags.tests) {
// generate testing files
await this.generateTestingFiles();
}
// generate postman files
await this.generatePostmanFiles();
// create yaml file
this.createYamlConfigFile();
this.createJsonLockFile();
}
async generateModuleFiles() {
await template_generator_1.TemplateGenerator.createDirectory(path.join('src', '@hades'), Operations.stateService.schema.boundedContextName.toLowerCase().toKebabCase());
await template_generator_1.TemplateGenerator.generateStaticContents(types_1.TemplateElement.MODULE, path.join('src', '@hades'), Operations.stateService.schema.boundedContextName.toLowerCase().toKebabCase());
await template_generator_1.TemplateGenerator.generateValueObjects(path.join('src', '@hades'), Operations.stateService.schema.boundedContextName.toLowerCase().toKebabCase());
}
async generateIntermediateTables() {
await template_generator_1.TemplateGenerator.generateIntermediateTables(path.join('src', '@hades'), Operations.stateService.schema.boundedContextName.toLowerCase().toKebabCase());
}
async generateFrameworkFiles() {
await template_generator_1.TemplateGenerator.createDirectory(path.join('src', 'apps'), Operations.stateService.schema.boundedContextName.toLowerCase().toKebabCase());
await template_generator_1.TemplateGenerator.generateStaticContents(types_1.TemplateElement.FRAMEWORK, path.join('src', 'apps'), Operations.stateService.schema.boundedContextName.toLowerCase().toKebabCase());
}
async generateTestingFiles() {
await template_generator_1.TemplateGenerator.generateStaticContents(types_1.TemplateElement.TEST, path.join('test'), '');
await this.createTestingForeignModuleImports();
}
async generatePostmanFiles() {
await template_generator_1.TemplateGenerator.createDirectory('', 'postman');
await template_generator_1.TemplateGenerator.generateStaticContents(types_1.TemplateElement.POSTMAN, '', 'postman');
}
async generateEnvFile() {
await template_generator_1.TemplateGenerator.generateStaticContents(types_1.TemplateElement.ENV, '', Operations.stateService.appName);
}
async generateGraphqlTypes() {
// graphql
return new Promise((resolve, reject) => {
child.exec('ts-node generate-typings', (err, stdout, stderr) => {
if (err) {
Operations.stateService.command.warn(`Attention! we can't generate graphql entities`);
Operations.stateService.command.error(`Error: ${err.message}`);
return;
}
Operations.stateService.command.log(`GraphQL entities generated`);
resolve(stdout ? stdout : stderr);
});
});
}
async createReferences() {
const codeWriter = new code_writer_1.CodeWriter(path.join('src'), path.join('@hades'), path.join('apps'), Operations.stateService.schema.boundedContextName.toLowerCase(), Operations.stateService.schema.moduleName.toLowerCase(), Operations.stateService.schema.moduleNames.toLowerCase());
codeWriter.generateReferences(Operations.stateService.schema.properties);
codeWriter.declareFramework();
codeWriter.declareModule();
if (Operations.stateService.schema.hasOAuth)
codeWriter.declareAuthModuleInShareModule();
}
async createTestingForeignModuleImports() {
const codeWriter = new code_writer_1.CodeWriter(path.join('src'), path.join('@hades'), path.join('apps'), Operations.stateService.schema.boundedContextName.toLowerCase(), Operations.stateService.schema.moduleName.toLowerCase(), Operations.stateService.schema.moduleNames.toLowerCase());
codeWriter.generateTestingForeignReferences(Operations.stateService.schema.properties);
}
createYamlConfigFile() {
// write yaml file
let yamlStr = yaml.dump({
version: cliter_config_1.cliterConfig.configYamlVersion,
boundedContextName: Operations.stateService.schema.boundedContextName,
moduleName: Operations.stateService.schema.moduleName,
moduleNames: Operations.stateService.schema.moduleNames,
aggregateName: Operations.stateService.schema.aggregateName,
hasOAuth: Operations.stateService.schema.hasOAuth,
hasTenant: Operations.stateService.schema.hasTenant,
aggregateProperties: Operations.stateService.schema.properties.toDto().map(item => _.omit(item, ['id'])),
excluded: Operations.stateService.schema.excluded,
}, {
lineWidth: -1,
skipInvalid: true
});
const yamlPath = path.join(process.cwd(), 'cliter', Operations.stateService.schema.boundedContextName.toKebabCase());
if (!fs.existsSync(yamlPath))
fs.mkdirSync(yamlPath, { recursive: true });
fs.writeFileSync(path.join(yamlPath, `${Operations.stateService.schema.moduleName}.yml`), yamlStr, 'utf8');
}
createJsonLockFile() {
const jsonPath = path.join(process.cwd(), 'cliter', Operations.stateService.schema.boundedContextName.toKebabCase());
if (!fs.existsSync(jsonPath))
fs.mkdirSync(jsonPath, { recursive: true });
const jsonLockFile = {
version: cliter_config_1.cliterConfig.lockJsonVersion,
files: Operations.stateService.newLockFiles
};
fs.writeFileSync(path.join(jsonPath, `${Operations.stateService.schema.moduleName}-lock.json`), JSON.stringify(jsonLockFile, null, 4), 'utf8');
}
static parseFlagOfBoundedContextAndModule(command, module) {
const boundedContextSection = module.split('/');
if (boundedContextSection.length !== 2)
command.error('Must input bounded context and module name, with format: bounded-context/module');
return {
boundedContextName: boundedContextSection[0],
moduleName: boundedContextSection[1],
};
}
}
exports.Operations = Operations;
Operations.stateService = tsyringe_1.container.resolve(state_service_1.StateService);