meocord
Version:
Decorator-based Discord bot framework built on discord.js. Brings NestJS-style controllers, dependency injection, guards, and testing utilities to bot development — with a full CLI and TypeScript-first design.
38 lines (35 loc) • 1.67 kB
JavaScript
import path from 'path';
import { Logger } from '../../common/logger.js';
import '../../common/theme.js';
import { validateAndFormatName, buildTemplate, createDirectoryIfNotExists, generateFile } from '../../util/generator-cli.util.js';
class ServiceGeneratorHelper {
/**
* Generates a service file based on the provided service name.
* Validates and formats the service name, creates the necessary directories,
* and generates the service file using a predefined template.
*
* @param serviceName - The name of the service to generate.
* It can include slashes for nested paths.
* @throws Exits the process if the service name is not provided or invalid.
*/ generateService(serviceName) {
if (!serviceName) {
this.logger.error('Service name is required.');
process.exit(1);
}
const { parts, kebabCaseName, className } = validateAndFormatName(serviceName);
const serviceDir = path.join(process.cwd(), 'src', 'services', ...parts);
const serviceFile = path.join(serviceDir, `${kebabCaseName}.service.ts`);
const serviceTemplate = buildTemplate(className, 'service.template');
const specTemplate = buildTemplate(className, 'service.spec.template', {
kebabCaseName
});
createDirectoryIfNotExists(serviceDir);
generateFile(serviceFile, serviceTemplate);
generateFile(path.join(serviceDir, `${kebabCaseName}.service.spec.ts`), specTemplate);
}
constructor(appName){
this.appName = appName;
this.logger = new Logger(this.appName);
}
}
export { ServiceGeneratorHelper };