@koalarx/nest-cli
Version:
CLI para criação de projetos utilizando Koala Nest
99 lines (98 loc) • 4.57 kB
JavaScript
import chalk from 'chalk';
import { execSync } from 'node:child_process';
import { cpSync, readFileSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import inquirer from 'inquirer';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const TEMPLATE_BASE = join(__dirname, '../../templates/startup-project');
const TEMPLATES_DIR = join(__dirname, '../../templates');
export async function newProject(projectName) {
const targetDir = join(process.cwd(), projectName);
console.log(chalk.blue('🚀 Criando projeto Koala Nest...'));
console.log(chalk.gray(`📁 Destino: ${targetDir}\n`));
console.log(chalk.yellow('📋 Copiando estrutura base...'));
cpSync(TEMPLATE_BASE, targetDir, {
recursive: true,
filter: (src) => {
const relativePath = src.replace(TEMPLATE_BASE, '');
const isBlacklisted = relativePath.includes('/node_modules/') ||
relativePath === '/.git' ||
relativePath.startsWith('/.git/');
return !isBlacklisted;
}
});
console.log(chalk.yellow('📦 Adicionando configurações extras...'));
const dockerfilePath = join(TEMPLATES_DIR, 'startup-project', 'Dockerfile');
try {
cpSync(dockerfilePath, join(targetDir, 'Dockerfile'));
}
catch {
console.log(chalk.red('⚠️ Dockerfile não encontrado nos templates, pulando...'));
}
console.log(chalk.yellow('⚙️ Configurando package.json...'));
const packageJsonPath = join(targetDir, 'package.json');
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
packageJson.name = projectName;
writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
console.log(chalk.yellow('📄 Atualizando README...'));
const readmePath = join(targetDir, 'README.md');
let readme = readFileSync(readmePath, 'utf-8');
readme = readme.replace(/\[projectName\]/g, projectName);
writeFileSync(readmePath, readme);
console.log(chalk.yellow('🔐 Criando arquivo .env...'));
const envTemplate = readFileSync(join(TEMPLATES_DIR, 'env', 'config.txt'), 'utf-8');
const envContent = envTemplate.replace(/\[projectName\]/g, projectName.replace(/-/g, '_'));
writeFileSync(join(targetDir, '.env'), envContent);
console.log(chalk.yellow('🚫 Criando .gitignore...'));
const gitIgnoreContent = readFileSync(join(TEMPLATES_DIR, 'gitignore', 'config.txt'), 'utf-8');
writeFileSync(join(targetDir, '.gitignore'), gitIgnoreContent);
console.log(chalk.yellow('\n📥 Instalando dependências...'));
try {
execSync(`cd ${projectName} && bun install`, {
stdio: 'inherit',
});
console.log(chalk.yellow('🔨 Gerando Prisma Client...'));
execSync(`cd ${projectName} && bun run build:prisma`, {
stdio: 'inherit',
});
}
catch {
console.log(chalk.red('⚠️ Erro ao instalar dependências. Execute manualmente:'));
console.log(chalk.gray(` cd ${projectName}`));
console.log(chalk.gray(` bun install`));
console.log(chalk.gray(` bun run build:prisma`));
}
const installMcp = await inquirer
.prompt([
{
type: 'confirm',
name: 'install',
message: 'Deseja instalar o Koala Nest MCP Server localmente?',
default: true,
},
])
.then((answers) => answers.install);
if (installMcp) {
const { installMcpServer } = await import('../mcp.js');
try {
await installMcpServer();
}
catch {
console.log(chalk.red('⚠️ Erro ao instalar MCP Server'));
console.log(chalk.gray(' Você pode instalar manualmente depois com:'));
console.log(chalk.gray(' koala-nest mcp install'));
}
}
console.log(chalk.green('\n✅ Projeto criado com sucesso!'));
console.log(chalk.cyan('\n📚 Próximos passos:'));
console.log(chalk.gray(` cd ${projectName}`));
console.log(chalk.gray(` bun run prisma:deploy # Executar migrations no banco`));
console.log(chalk.gray(` bun run start:dev # Iniciar aplicação`));
if (!installMcp) {
console.log(chalk.gray(`\n💡 Para instalar o MCP Server depois:`));
console.log(chalk.gray(` koala-nest mcp install`));
}
console.log(chalk.gray(`\n📖 Documentação: https://github.com/igordrangel/koala-nest\n`));
}