legendaryjs
Version:
LegendaryJS โ The ultimate backend framework for speed, power, and simplicity.
124 lines (108 loc) โข 4.49 kB
JavaScript
const chalk = require('chalk');
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const args = process.argv.slice(2);
const initProject = require('./utils/init');
function run(command, desc) {
try {
console.log(chalk.cyan(`๐ ${desc}...`));
execSync(command, { stdio: 'inherit' });
} catch (err) {
console.error(chalk.red(`โ Failed: ${desc}`));
console.error(err.message || err);
}
}
function loadConfig() {
const configPath = path.join(process.cwd(), 'legendary.config.js');
if (!fs.existsSync(configPath)) {
console.error(chalk.red('โ legendary.config.js not found in root directory.'));
process.exit(1);
}
return require(configPath);
}
function generateTestScaffold() {
const routesPath = path.join(process.cwd(), 'routes/api.v1.json');
const outputDir = path.join(process.cwd(), '__tests__');
if (!fs.existsSync(routesPath)) return console.error('โ No routes found');
if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir);
const { routes } = JSON.parse(fs.readFileSync(routesPath));
const file = path.join(outputDir, 'generated.test.js');
const lines = [
"const request = require('supertest');",
"const app = require('../app');",
"describe('๐งช Auto Generated API Tests', () => {"
];
for (const route of routes || []) {
const { method = 'get', path } = route;
lines.push(` it('${method.toUpperCase()} ${path} should return 200', async () => {`);
lines.push(` const res = await request(app).${method.toLowerCase()}('${path}');`);
lines.push(` expect(res.statusCode).toBe(200);`);
lines.push(' });');
}
lines.push('});');
fs.writeFileSync(file, lines.join('\n'));
console.log(chalk.green(`โ
Test scaffold generated at __tests__/generated.test.js`));
}
// Main command handler
(async () => {
const command = args[0];
switch (command) {
case 'init': {
await initProject();
break;
}
case 'start': {
const config = loadConfig();
const runCommand = config.dev?.hotReload ? 'npx nodemon app.js' : 'node app.js';
run(runCommand, 'Starting LegendaryJS project');
break;
}
case 'test': {
const sub = args[1];
if (sub === 'generate') {
generateTestScaffold();
} else {
const hasJest = fs.existsSync('./jest.config.js');
const hasMocha = fs.existsSync('./test');
if (hasJest) run('npx jest', 'Running Jest tests');
else if (hasMocha) run('npx mocha', 'Running Mocha tests');
else console.log(chalk.yellow('โ ๏ธ No testing framework found. Create `jest.config.js` or `test/`.'));
}
break;
}
case 'info': {
const config = loadConfig();
console.log(chalk.bold('\n๐ LegendaryJS Project Info'));
console.log(chalk.gray('โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ'));
console.log('๐ฆ Port:', config.port);
console.log('๐ Auth:', JSON.stringify(config.auth));
console.log('๐งฌ Protocol:', config.protocol);
console.log('๐งฑ DBs:', Object.entries(config.db).filter(e => e[1] === true).map(e => e[0]).join(', '));
console.log('๐ Features:', Object.keys(config).filter(k => config[k] === true).join(', '));
break;
}
default: {
console.log(chalk.magenta('\nLegendaryJS CLI'));
console.log('Usage:');
console.log(chalk.yellow(' legendaryjs init') + ' โ Initialize a new project');
console.log(chalk.yellow(' legendaryjs start') + ' โ Start the project server');
console.log(chalk.yellow(' legendaryjs test') + ' โ Run tests (Jest/Mocha)');
console.log(chalk.yellow(' legendaryjs test generate') + ' โ Auto-generate test cases');
console.log(chalk.yellow(' legendaryjs info') + ' โ View config and feature summary');
break;
}
}
})();
// index.js โ LegendaryJS Framework Entry Point
const { createApp } = require('./core/createApp');
const { createModel } = require('./core/helpers/createModel');
const { createRoute } = require('./core/helpers/createRoute');
const { validate } = require('./core/helpers/validate');
module.exports = {
createApp,
createModel,
createRoute,
validate
};