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.
69 lines (66 loc) • 2.88 kB
JavaScript
import fs from 'fs';
import path from 'path';
import { loadMeoCordConfig } from './meocord-config-loader.util.js';
import wait from './wait.util.js';
import chalk from 'chalk';
/**
* Finds the package directory for the given module name
* @param {string} moduleName - The name of the module to find
* @param {string} baseDir - The starting directory to search from (defaults to process.cwd())
* @returns {string} - The directory path where the module's package.json is located
*/ const findModulePackageDir = (moduleName, baseDir = process.cwd())=>{
try {
// Resolve the node_modules directory from the base directory
let currentDir = baseDir;
// Traverse the node_modules directories upwards until the package is found
while(currentDir !== path.parse(currentDir).root){
const modulePath = path.join(currentDir, 'node_modules', moduleName);
if (fs.existsSync(modulePath)) {
return modulePath // Return the full path to the module directory
;
}
// Move up one level in the directory structure
currentDir = path.join(currentDir, '..');
}
throw new Error(`Module ${moduleName} not found in node_modules.`);
} catch (error) {
if (error instanceof Error) {
console.error(chalk.red(`Error finding package directory for ${moduleName}:`, error.message));
} else {
console.error(chalk.red(`Error finding package directory for ${moduleName}:`, error));
}
return null;
}
};
/**
* Compiles and validates the MeoCord configuration file.
* Ensures that `meocord.config.ts` exists, compiles it, and checks
* the presence of essential configuration properties like `discordToken`.
*
* @throws Will exit the process if the `meocord.config.ts` file is missing or
* if the `discordToken` property is not found in the configuration.
*/ async function compileAndValidateConfig() {
const meocordConfigPath = path.resolve(process.cwd(), 'meocord.config.ts');
if (!fs.existsSync(meocordConfigPath)) {
console.error(chalk.red('Configuration file "meocord.config.ts" is missing!'));
await wait(100);
process.exit(1);
}
const meocordConfig = loadMeoCordConfig();
if (!meocordConfig?.discordToken) {
console.error(chalk.red('Discord token is missing!'));
await wait(100);
process.exit(1);
}
}
/**
* Sets the environment mode for the application.
* Assigns the provided mode to `process.env.NODE_ENV` if it hasn't been set already.
*
* @param {'production' | 'development'} mode - The desired environment mode.
*/ function setEnvironment(mode) {
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = mode;
}
}
export { compileAndValidateConfig, findModulePackageDir, setEnvironment };