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.61 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 GuardGeneratorHelper {
/**
* Generates a guard file based on the provided guard name.
* Validates and formats the guard name, creates the necessary directories,
* and generates the guard file using a predefined template.
*
* @param guardName - The name of the guard to generate.
* It can include slashes for nested paths.
* @throws Exits the process if the guard name is not provided or invalid.
*/ generateGuard(guardName) {
if (!guardName) {
this.logger.error('Guard name is required.');
process.exit(1);
}
const { parts, kebabCaseName, className } = validateAndFormatName(guardName);
const guardDir = path.join(process.cwd(), 'src', 'guards', ...parts);
const guardFile = path.join(guardDir, `${kebabCaseName}.guard.ts`);
const guardTemplate = buildTemplate(className, 'guard.template');
const specTemplate = buildTemplate(className, 'guard.spec.template', {
kebabCaseName
});
createDirectoryIfNotExists(guardDir);
generateFile(guardFile, guardTemplate);
generateFile(path.join(guardDir, `${kebabCaseName}.guard.spec.ts`), specTemplate);
}
constructor(appName){
this.appName = appName;
this.logger = new Logger(this.appName);
}
}
export { GuardGeneratorHelper };