@re-shell/cli
Version:
Full-stack development platform uniting microservices and microfrontends. Build complete applications with .NET (ASP.NET Core Web API, Minimal API), Java (Spring Boot, Quarkus, Micronaut, Vert.x), Rust (Actix-Web, Warp, Rocket, Axum), Python (FastAPI, Dja
396 lines (350 loc) โข 10.9 kB
JavaScript
;
/**
* Shared Backend Template Generator System
* Provides unified architecture for all backend framework templates
*/
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.BackendTemplateUtils = exports.BackendTemplateGenerator = void 0;
const fs_1 = require("fs");
const path = __importStar(require("path"));
const template_engine_1 = require("../../../utils/template-engine");
class BackendTemplateGenerator {
constructor(config) {
this.config = config;
this.templateEngine = new template_engine_1.ConfigTemplateEngine();
}
/**
* Generate complete backend project structure
*/
async generate(projectPath, options) {
// Create base directory structure
await this.createDirectoryStructure(projectPath);
// Generate language-specific files
await this.generateLanguageFiles(projectPath, options);
// Generate framework-specific files
await this.generateFrameworkFiles(projectPath, options);
// Generate common files
await this.generateCommonFiles(projectPath, options);
// Generate Docker configuration
if (this.config.dockerConfig) {
await this.generateDockerFiles(projectPath, options);
}
// Generate test structure
await this.generateTestStructure(projectPath, options);
// Generate documentation
await this.generateDocumentation(projectPath, options);
}
/**
* Create standard directory structure
*/
async createDirectoryStructure(projectPath) {
const directories = [
'src',
'src/controllers',
'src/services',
'src/models',
'src/middleware',
'src/utils',
'src/config',
'tests',
'tests/unit',
'tests/integration',
'scripts',
'docs',
'.github/workflows'
];
for (const dir of directories) {
await fs_1.promises.mkdir(path.join(projectPath, dir), { recursive: true });
}
}
/**
* Generate common files across all backends
*/
async generateCommonFiles(projectPath, options) {
// .gitignore
await this.generateGitignore(projectPath);
// README.md
await this.generateReadme(projectPath, options);
// Environment files
await this.generateEnvFiles(projectPath);
// CI/CD workflows
await this.generateCICD(projectPath);
// Health check endpoint
await this.generateHealthCheck(projectPath);
// API documentation
await this.generateAPIDocs(projectPath);
}
/**
* Generate .gitignore with language-specific patterns
*/
async generateGitignore(projectPath) {
const commonPatterns = [
'# Dependencies',
'node_modules/',
'vendor/',
'.venv/',
'target/',
'build/',
'dist/',
'',
'# Environment',
'.env',
'.env.local',
'.env.*.local',
'',
'# Logs',
'*.log',
'logs/',
'',
'# IDE',
'.vscode/',
'.idea/',
'*.swp',
'*.swo',
'.DS_Store',
'',
'# Testing',
'coverage/',
'.coverage',
'*.cover',
'.pytest_cache/',
'',
'# Build artifacts',
'*.exe',
'*.dll',
'*.so',
'*.dylib',
'*.pyc',
'*.pyo',
'__pycache__/',
'',
'# Language specific',
...this.getLanguageSpecificIgnorePatterns()
];
await fs_1.promises.writeFile(path.join(projectPath, '.gitignore'), commonPatterns.join('\\n'));
}
/**
* Generate comprehensive README
*/
async generateReadme(projectPath, options) {
const content = `# ${options.name}
A ${this.config.framework} ${this.config.language} microservice built with Re-Shell CLI.
## ๐ Features
${this.config.features.map(f => `- ${f}`).join('\\n')}
## ๐ Prerequisites
- ${this.getLanguagePrerequisites()}
- Docker (optional)
- Re-Shell CLI
## ๐ ๏ธ Installation
\`\`\`bash
# Clone the repository
git clone <repository-url>
cd ${options.name}
# Install dependencies
${this.getInstallCommand()}
\`\`\`
## ๐ง Configuration
Copy the example environment file:
\`\`\`bash
cp .env.example .env
\`\`\`
Update the environment variables in \`.env\`:
\`\`\`env
${Object.entries(this.config.envVars || {}).map(([k, v]) => `${k}=${v}`).join('\\n')}
\`\`\`
## ๐ Running the Application
### Development
\`\`\`bash
${this.getDevCommand()}
\`\`\`
### Production
\`\`\`bash
${this.getProdCommand()}
\`\`\`
### Docker
\`\`\`bash
# Build the image
docker build -t ${options.name} .
# Run the container
docker run -p ${this.config.dockerConfig?.exposedPorts?.[0] || 3000}:${this.config.dockerConfig?.exposedPorts?.[0] || 3000} ${options.name}
\`\`\`
## ๐งช Testing
\`\`\`bash
# Run all tests
${this.getTestCommand()}
# Run with coverage
${this.getCoverageCommand()}
\`\`\`
## ๐ API Documentation
- Swagger UI: http://localhost:${options.port}/docs
- OpenAPI JSON: http://localhost:${options.port}/openapi.json
## ๐๏ธ Project Structure
\`\`\`
${options.name}/
โโโ src/
โ โโโ controllers/ # Request handlers
โ โโโ services/ # Business logic
โ โโโ models/ # Data models
โ โโโ middleware/ # Middleware functions
โ โโโ utils/ # Utility functions
โ โโโ config/ # Configuration files
โโโ tests/
โ โโโ unit/ # Unit tests
โ โโโ integration/ # Integration tests
โโโ scripts/ # Utility scripts
โโโ docs/ # Documentation
โโโ .github/ # GitHub workflows
\`\`\`
## ๐ค Contributing
1. Fork the repository
2. Create your feature branch (\`git checkout -b feature/amazing-feature\`)
3. Commit your changes (\`git commit -m 'Add some amazing feature'\`)
4. Push to the branch (\`git push origin feature/amazing-feature\`)
5. Open a Pull Request
## ๐ License
This project is licensed under the MIT License.
---
Built with โค๏ธ using [Re-Shell CLI](https://github.com/re-shell/cli)
`;
await fs_1.promises.writeFile(path.join(projectPath, 'README.md'), content);
}
/**
* Generate environment files
*/
async generateEnvFiles(projectPath) {
const envExample = Object.entries(this.config.envVars || {})
.map(([key, value]) => `${key}=${value}`)
.join('\\n');
await fs_1.promises.writeFile(path.join(projectPath, '.env.example'), envExample);
}
/**
* Generate CI/CD workflows
*/
async generateCICD(projectPath) {
const workflow = `name: CI/CD
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup ${this.config.language}
uses: ${this.getSetupAction()}
- name: Install dependencies
run: ${this.getInstallCommand()}
- name: Run tests
run: ${this.getTestCommand()}
- name: Run linter
run: ${this.getLintCommand()}
- name: Build
run: ${this.getBuildCommand()}
docker:
needs: test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v3
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
push: true
tags: |
ghcr.io/\${{ github.repository }}:latest
ghcr.io/\${{ github.repository }}:\${{ github.sha }}
`;
await fs_1.promises.writeFile(path.join(projectPath, '.github/workflows/ci.yml'), workflow);
}
}
exports.BackendTemplateGenerator = BackendTemplateGenerator;
/**
* Common utilities for all backend templates
*/
class BackendTemplateUtils {
/**
* Generate JWT authentication middleware
*/
static generateJWTAuth(language) {
// Language-specific JWT implementation
return '';
}
/**
* Generate rate limiting middleware
*/
static generateRateLimit(language) {
// Language-specific rate limiting
return '';
}
/**
* Generate logging configuration
*/
static generateLogging(language) {
// Language-specific logging setup
return '';
}
/**
* Generate database configuration
*/
static generateDatabaseConfig(language, orm) {
// Language and ORM specific database setup
return '';
}
/**
* Generate OpenAPI/Swagger documentation
*/
static generateOpenAPISpec(framework) {
// Framework-specific OpenAPI integration
return '';
}
/**
* Generate health check endpoint
*/
static generateHealthEndpoint(language, framework) {
// Language and framework specific health check
return '';
}
/**
* Generate Docker configuration
*/
static generateDockerfile(config) {
// Multi-stage Dockerfile generation
return '';
}
}
exports.BackendTemplateUtils = BackendTemplateUtils;