@shadow-dev/core
Version:
A modular core framework for Discord bot development, providing commands, buttons, menus, middleware, and more.
317 lines (306 loc) • 13 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateProject = generateProject;
const child_process_1 = require("child_process");
const fs_1 = require("fs");
const path = __importStar(require("path"));
async function generateProject(projectName, packageManager, tools, examples) {
console.log(`Generating project: ${projectName}`);
// Here you would typically create the project directory and initialize it with the selected package manager
const projectPath = path.join(process.cwd(), projectName);
try {
await fs_1.promises.access(projectPath);
console.error(`Error: Directory "${projectName}" already exists.`);
return;
}
catch {
await fs_1.promises.mkdir(projectPath, { recursive: true });
console.log(`Created directory: ${projectName}`);
}
console.log(`Generating project with ${packageManager}...`);
(0, child_process_1.execSync)(`${packageManager} init -y`, { cwd: projectPath, stdio: "inherit" });
// Install required dependencies for the base project
const baseDependencies = ["@shadow-dev/core", "discord.js"];
(0, child_process_1.execSync)(`${packageManager} add ${baseDependencies.join(" ")}`, {
cwd: projectPath,
stdio: "inherit",
});
// Setup basic project structure
const indexts = `import { Bot } from '@shadow-dev/core';
import { GatewayIntentBits } from 'discord.js';
// Replace with your bot's token
const token = 'YOUR_BOT_TOKEN'; // Replace with your Bot's token
const GUILD_ID = 'YOUR_GUILD_ID'; // Replace with your server's ID
// Initialize the bot
export const bot = new Bot(
token,
[
'Guilds',
'GuildMessages',
'MessageContent'
],
false, // Debug mode disabled
GUILD_ID
);
// Log when the bot is ready
bot.getClient().once('ready', () => {
console.log(\`✅ Logged in as \${bot.getClient().user?.tag}\`);
});
`;
const srcPath = path.join(projectPath, "src");
await fs_1.promises.mkdir(srcPath, { recursive: true });
await fs_1.promises.mkdir(path.join(srcPath, "commands"), { recursive: true });
await fs_1.promises.mkdir(path.join(srcPath, "events"), { recursive: true });
await fs_1.promises.mkdir(path.join(srcPath, "middleware"), { recursive: true });
await fs_1.promises.mkdir(path.join(srcPath, "buttons"), { recursive: true });
await fs_1.promises.mkdir(path.join(srcPath, "menus"), { recursive: true });
await fs_1.promises.writeFile(path.join(srcPath, "index.ts"), `// Entry point for your project\n\n${indexts}`, "utf-8");
console.log(`Selected tools: ${tools.join(", ")}`);
// Install selected tools
if (tools.length > 0) {
for (const tool of tools) {
if (tool === "typescript") {
(0, child_process_1.execSync)(`${packageManager} add -D typescript @types/node ts-node`, {
cwd: projectPath,
stdio: "inherit",
});
await fs_1.promises.writeFile(path.join(projectPath, "tsconfig.json"), `{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}`, "utf-8");
}
}
// Special handling if both Prettier and ESLint are selected
if (tools.includes("eslint") && tools.includes("prettier")) {
if (packageManager === "npm") {
(0, child_process_1.execSync)(`npm init @eslint/config@latest`, {
cwd: projectPath,
stdio: "inherit",
});
}
else {
(0, child_process_1.execSync)(`${packageManager} create @eslint/config@latest`, {
cwd: projectPath,
stdio: "inherit",
});
}
const eslintConfigPath = path.join(projectPath, "eslint.config.mjs");
const eslintConfigJsPath = path.join(projectPath, "eslint.config.js");
const eslintConfigCjsPath = path.join(projectPath, "eslint.config.cjs");
const eslintConfigExists = (await fs_1.promises.stat(eslintConfigPath).catch(() => false)) ||
(await fs_1.promises.stat(eslintConfigJsPath).catch(() => false)) ||
(await fs_1.promises.stat(eslintConfigCjsPath).catch(() => false));
if (eslintConfigExists) {
const eslintConfig = `import { defineConfig } from 'eslint/config';
import globals from 'globals';
import js from '@eslint/js';
import tseslint from 'typescript-eslint';
import prettier from 'eslint-config-prettier';
import prettierPlugin from 'eslint-plugin-prettier';
export default defineConfig([
{ files: ['**/*.{js,mjs,cjs,ts}'] },
{ files: ['**/*.js'], languageOptions: { sourceType: 'script' } },
{
files: ['**/*.{js,mjs,cjs,ts}'],
languageOptions: { globals: { ...globals.browser, ...globals.node } },
},
{
files: ['**/*.{js,mjs,cjs,ts}'],
plugins: { js, prettier: prettierPlugin },
extends: ['js/recommended', prettier],
},
{ rules: { 'prettier/prettier': 'error' } },
tseslint.configs.recommended,
]);
`;
if (eslintConfigPath) {
await fs_1.promises.writeFile(eslintConfigPath, eslintConfig, "utf-8");
}
else if (eslintConfigJsPath) {
await fs_1.promises.writeFile(eslintConfigJsPath, eslintConfig, "utf-8");
}
else if (eslintConfigCjsPath) {
await fs_1.promises.writeFile(eslintConfigCjsPath, eslintConfig, "utf-8");
}
else {
console.error("Error: No valid ESLint configuration file path found.");
}
}
(0, child_process_1.execSync)(`${packageManager} add --save-dev prettier eslint-config-prettier eslint-plugin-prettier`, {
cwd: projectPath,
stdio: "inherit",
});
const prettierConfigPath = path.join(projectPath, ".prettierrc");
const prettierConfig = `{
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5"
}`;
await fs_1.promises.writeFile(prettierConfigPath, prettierConfig, "utf-8");
}
else {
for (const tool of tools) {
if (tool === "eslint") {
if (packageManager === "npm") {
(0, child_process_1.execSync)(`npm init @eslint/config@latest`, {
cwd: projectPath,
stdio: "inherit",
});
}
else {
(0, child_process_1.execSync)(`${packageManager} create @eslint/config@latest`, {
cwd: projectPath,
stdio: "inherit",
});
}
}
if (tool === "prettier") {
(0, child_process_1.execSync)(`${packageManager} add -D prettier`, {
cwd: projectPath,
stdio: "inherit",
});
await fs_1.promises.writeFile(path.join(projectPath, ".prettierrc"), `{
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5"
}`, "utf-8");
}
}
}
}
console.log(`Selected examples: ${examples.join(", ")}`);
// Install selected examples
if (examples.length > 0) {
for (const example of examples) {
if (example === "command") {
const commandExample = `import { Command } from '@shadow-dev/core';
export default new Command({
name: 'ping',
description: 'Pong',
run: async (interaction, client) => {
const uptime = client.uptime; // This line is just to ensure the client is referenced, you can remove it if not needed.
await interaction.reply({
content: uptime ? Math.floor(uptime / 1000).toString() : '🏓 Pong!',
});
},
});
`;
await fs_1.promises.mkdir(path.join(srcPath, "commands", "fun"), { recursive: true });
await fs_1.promises.writeFile(path.join(srcPath, "commands", "fun", "ping.ts"), commandExample, "utf-8");
}
if (example === "event") {
const eventExample = `import { Event } from '@shadow-dev/core';
import { bot } from '../index';
export default new Event(
'ready',
async (client) => {
console.log('✅ Logged in as', client.user?.tag);
},
true
);
`;
await fs_1.promises.writeFile(path.join(srcPath, "events", "ready.ts"), eventExample, "utf-8");
}
if (example === "button") {
const buttonExample = `import { Button, splitSpecialId } from '@shadow-dev/core';
export default new Button({
customId: 'test-button',
run: async (interaction) => {
const parsedId = splitSpecialId(interaction.customId);
interaction.reply({ content: parsedId.id });
// await interaction.reply("Testing Button")
},
});
`;
await fs_1.promises.mkdir(path.join(srcPath, "buttons", "test"), { recursive: true });
await fs_1.promises.writeFile(path.join(srcPath, "buttons", "test", "test.ts"), buttonExample, "utf-8");
}
if (example === "menu") {
const menuExample = `import { Menu } from '@shadow-dev/core';
export default new Menu({
customId: 'testing-menu',
run: async (interaction) => {
interaction.reply('Test menu');
},
});
`;
await fs_1.promises.mkdir(path.join(srcPath, "menus", "test"), { recursive: true });
await fs_1.promises.writeFile(path.join(srcPath, "menus", "test", "test.ts"), menuExample, "utf-8");
}
if (example === "middleware") {
const middlewareglobalExample = `import { CommandMiddleware } from '@shadow-dev/core';
export default new CommandMiddleware({
name: 'global',
beforeExecution: async () => {
console.log('Global Before');
return true;
},
afterExecution: async () => {
console.log('Global After');
},
});
`;
const middlewarespecificExample = `import { CommandMiddleware } from '@shadow-dev/core';
export default new CommandMiddleware({
name: 'ping',
beforeExecution: async () => {
console.log('Ping Before');
return true;
},
afterExecution: async () => {
console.log('Ping After');
},
});
`;
await fs_1.promises.mkdir(path.join(srcPath, "middleware", "command"), {
recursive: true,
});
await fs_1.promises.writeFile(path.join(srcPath, "middleware", "command", "global.ts"), middlewareglobalExample, "utf-8");
await fs_1.promises.writeFile(path.join(srcPath, "middleware", "command", "ping.ts"), middlewarespecificExample, "utf-8");
}
}
}
}