UNPKG

@kenniy/godeye-data-contracts

Version:

Enterprise-grade base repository architecture for GOD-EYE microservices with zero overhead and maximum code reuse

204 lines (203 loc) 9.51 kB
#!/usr/bin/env node "use strict"; /** * GOD-EYE Data Contracts CLI * * Generate optimized repositories for your GOD-EYE microservices */ 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 }); const commander_1 = require("commander"); const chalk = __importStar(require("chalk")); const repository_generator_1 = require("../generators/repository-generator"); // Package info const packageJson = require('../../package.json'); // Initialize CLI commander_1.program .name('godeye-generate') .description('Generate optimized repositories for GOD-EYE microservices') .version(packageJson.version); // Repository generation command commander_1.program .command('repository <entityName>') .alias('repo') .description('Generate an ORM-specific repository for the specified entity') .option('-f, --force', 'Overwrite existing repository file') .option('-p, --path <path>', 'Path to service directory', process.cwd()) .action(async (entityName, options) => { try { console.log(chalk.blue('🚀 GOD-EYE Repository Generator')); console.log(chalk.gray('─'.repeat(50))); console.log(chalk.cyan(`📋 Generating repository for: ${entityName}`)); console.log(); const generator = new repository_generator_1.RepositoryGenerator(); const result = await generator.generateRepository(entityName, { servicePath: options.path, force: options.force }); console.log(); console.log(chalk.green('✅ Repository generated successfully!')); console.log(chalk.gray(`📁 File: ${result.filePath}`)); console.log(); console.log(chalk.yellow('📝 Next steps:')); console.log(chalk.gray(' 1. Import in your service:')); console.log(chalk.white(` import { ${entityName}Repository } from '@/libs/dal/repositories';`)); console.log(chalk.gray(' 2. Add to module providers:')); console.log(chalk.white(` providers: [${entityName}Repository]`)); console.log(chalk.gray(' 3. Inject in service:')); console.log(chalk.white(` constructor(private ${entityName.toLowerCase()}Repo: ${entityName}Repository) {}`)); console.log(); console.log(chalk.green('🎉 Happy coding!')); } catch (error) { console.error(chalk.red('❌ Generation failed:')); console.error(chalk.red(error.message)); if (error.message.includes('package.json not found')) { console.log(); console.log(chalk.yellow('💡 Solution:')); console.log(chalk.gray(' Run this command from your GOD-EYE service root directory')); console.log(chalk.gray(' Example: cd god-eye-user-service && npx godeye-generate repository User')); } if (error.message.includes('No supported ORM detected')) { console.log(); console.log(chalk.yellow('💡 Solution:')); console.log(chalk.gray(' This service needs either TypeORM or Mongoose in package.json')); console.log(chalk.gray(' TypeORM: npm install typeorm')); console.log(chalk.gray(' Mongoose: npm install mongoose')); } if (error.message.includes('Entity file not found')) { console.log(); console.log(chalk.yellow('💡 Solution:')); console.log(chalk.gray(' Create your entity/model file first:')); console.log(chalk.gray(' TypeORM: src/libs/dal/entities/user.entity.ts')); console.log(chalk.gray(' Mongoose: src/libs/dal/models/user.model.ts')); } if (error.message.includes('already exists')) { console.log(); console.log(chalk.yellow('💡 Solution:')); console.log(chalk.gray(' Use --force flag to overwrite:')); console.log(chalk.white(` npx godeye-generate repository ${entityName} --force`)); } process.exit(1); } }); // List command - show detected ORM and entities commander_1.program .command('list') .alias('ls') .description('List detected entities and ORM configuration') .option('-p, --path <path>', 'Path to service directory', process.cwd()) .action(async (options) => { try { console.log(chalk.blue('🔍 GOD-EYE Service Analysis')); console.log(chalk.gray('─'.repeat(50))); const generator = new repository_generator_1.RepositoryGenerator(); const ormConfig = generator.detectORM(options.path); console.log(chalk.green(`✅ Detected ORM: ${ormConfig.type.toUpperCase()}`)); console.log(chalk.gray(`📁 Entity Path: ${ormConfig.entityPath}`)); console.log(chalk.gray(`📁 Repository Path: ${ormConfig.repositoryPath}`)); console.log(); // List existing entities const fs = require('fs'); const path = require('path'); const entityDir = path.join(options.path, ormConfig.entityPath); if (fs.existsSync(entityDir)) { const files = fs.readdirSync(entityDir) .filter((file) => file.endsWith('.entity.ts') || file.endsWith('.model.ts')) .map((file) => file.replace(/\.(entity|model)\.ts$/, '')); if (files.length > 0) { console.log(chalk.cyan('📋 Available Entities:')); files.forEach((file) => { const entityName = file.charAt(0).toUpperCase() + file.slice(1); console.log(chalk.white(` • ${entityName}`)); console.log(chalk.gray(` npx godeye-generate repository ${entityName}`)); }); } else { console.log(chalk.yellow('⚠️ No entities found')); console.log(chalk.gray(' Create entity files first, then generate repositories')); } } else { console.log(chalk.red(`❌ Entity directory not found: ${ormConfig.entityPath}`)); } } catch (error) { console.error(chalk.red('❌ Analysis failed:')); console.error(chalk.red(error.message)); process.exit(1); } }); // Info command - show package info and help commander_1.program .command('info') .description('Show package information and usage examples') .action(() => { console.log(chalk.blue('📦 GOD-EYE Data Contracts')); console.log(chalk.gray('─'.repeat(50))); console.log(chalk.white(`Version: ${packageJson.version}`)); console.log(chalk.white(`Description: ${packageJson.description}`)); console.log(); console.log(chalk.cyan('🎯 Purpose:')); console.log(chalk.gray(' Generate ORM-specific repositories for GOD-EYE microservices')); console.log(chalk.gray(' Zero runtime overhead - all optimization happens at build time')); console.log(); console.log(chalk.cyan('🚀 Quick Start:')); console.log(chalk.white(' cd god-eye-user-service')); console.log(chalk.white(' npx godeye-generate repository User')); console.log(); console.log(chalk.cyan('📋 Available Commands:')); console.log(chalk.white(' repository <name> Generate repository for entity')); console.log(chalk.white(' list Show detected entities')); console.log(chalk.white(' info Show this information')); console.log(); console.log(chalk.cyan('🏗️ Supported ORMs:')); console.log(chalk.white(' TypeORM PostgreSQL services (User, HRM)')); console.log(chalk.white(' Mongoose MongoDB services (File+Notification)')); console.log(); console.log(chalk.cyan('📚 Documentation:')); console.log(chalk.gray(' https://github.com/keniiy/godeye-data-contracts')); }); // Error handling commander_1.program.on('command:*', () => { console.error(chalk.red('❌ Invalid command: %s'), commander_1.program.args.join(' ')); console.log(chalk.gray('See --help for available commands')); process.exit(1); }); // Parse arguments if (process.argv.length <= 2) { commander_1.program.help(); } commander_1.program.parse(process.argv);