UNPKG

nitp-build-tools

Version:

Official build system for NIT Patna's backend services - A powerful Maven-based build automation tool with cross-platform support

187 lines (156 loc) 8.33 kB
/** * NITP Build Tools - Post Install Script * * This script runs after npm installation to set up the build environment * and verify system requirements. * * Created by: Ashish Kumar (https://github.com/ashishkr375) */ const fs = require('fs'); const path = require('path'); const { exec } = require('child_process'); const chalk = require('chalk'); const os = require('os'); // ASCII Art for installation const INSTALL_BANNER = ` ${chalk.cyan('╔══════════════════════════════════════════════════════════════╗')} ${chalk.cyan('║')} ${chalk.bold.green('NITP Build Tools - Installation Complete')} ${chalk.cyan('║')} ${chalk.cyan('║')} ${chalk.cyan('║')} ${chalk.cyan('║')} ${chalk.yellow('🎉 Welcome to NIT Patna Build System! 🎉')} ${chalk.cyan('║')} ${chalk.cyan('║')} ${chalk.cyan('║')} ${chalk.cyan('║')} ${chalk.blue('Created by:')} ${chalk.white('Ashish Kumar')} ${chalk.cyan('║')} ${chalk.cyan('║')} ${chalk.blue('GitHub:')} ${chalk.underline.white('https://github.com/ashishkr375')} ${chalk.cyan('║')} ${chalk.cyan('║')} ${chalk.blue('LinkedIn:')} ${chalk.underline.white('https://www.linkedin.com/in/ashish-kumar-nitp/')} ${chalk.cyan('║')} ${chalk.cyan('╚══════════════════════════════════════════════════════════════╝')} `; console.log(INSTALL_BANNER); // System information const systemInfo = { platform: os.platform(), arch: os.arch(), nodeVersion: process.version, npmVersion: null, javaVersion: null, mavenVersion: null }; // Utility functions const utils = { // Execute command and return result async execCommand(command) { return new Promise((resolve) => { exec(command, { timeout: 5000 }, (error, stdout, stderr) => { resolve({ success: !error, output: stdout || stderr, error: error }); }); }); }, // Check and get version async checkVersion(command, regex) { const result = await this.execCommand(command); if (result.success && result.output) { const match = result.output.match(regex); return match ? match[1] : 'Unknown'; } return null; }, // Print status with icon printStatus(name, status, version = null, warning = null) { const icon = status ? '✅' : '❌'; const color = status ? chalk.green : chalk.red; const versionText = version ? ` (${version})` : ''; console.log(`${icon} ${color(name)}${versionText}`); if (warning) { console.log(` ${chalk.yellow('⚠️ ' + warning)}`); } } }; async function runPostInstall() { console.log(chalk.cyan('🔧 Setting up NITP Build Tools...\n')); // Make nitp-build executable (Unix systems) const nitpBuildPath = path.join(__dirname, '../bin/nitp-build'); try { if (os.platform() !== 'win32') { fs.chmodSync(nitpBuildPath, '755'); console.log(chalk.green('✅ Made nitp-build executable')); } } catch (error) { console.warn(chalk.yellow('⚠️ Could not make nitp-build executable:'), error.message); } console.log(chalk.cyan('\n📋 Checking system requirements...\n')); // Check npm version systemInfo.npmVersion = await utils.checkVersion('npm -v', /^(.+)$/); // Check Java systemInfo.javaVersion = await utils.checkVersion('java -version', /version "(.+?)"/); // Check Maven systemInfo.mavenVersion = await utils.checkVersion('mvn -v', /Apache Maven ([\d.]+)/); // Display results console.log(chalk.blue('System Information:')); console.log(chalk.blue('─'.repeat(40))); utils.printStatus('Node.js', true, systemInfo.nodeVersion); utils.printStatus('npm', systemInfo.npmVersion !== null, systemInfo.npmVersion); const javaOk = systemInfo.javaVersion !== null; const javaWarning = !javaOk ? 'Please install Java 17+ from https://adoptium.net/' : null; utils.printStatus('Java', javaOk, systemInfo.javaVersion, javaWarning); const mavenOk = systemInfo.mavenVersion !== null; const mavenWarning = !mavenOk ? 'Please install Maven 3.8+ from https://maven.apache.org/' : null; utils.printStatus('Maven', mavenOk, systemInfo.mavenVersion, mavenWarning); // Platform-specific setup console.log(chalk.cyan('\n🌍 Platform-specific setup...\n')); if (systemInfo.platform === 'win32') { console.log(chalk.blue('Windows detected:')); console.log(' • Use Command Prompt, PowerShell, or Git Bash'); console.log(' • Ensure Java and Maven are in your PATH'); console.log(' • You may need to restart your terminal'); } else if (systemInfo.platform === 'darwin') { console.log(chalk.blue('macOS detected:')); console.log(' • You may need to install Java via Homebrew: brew install openjdk@17'); console.log(' • Maven can be installed via: brew install maven'); } else { console.log(chalk.blue('Linux detected:')); console.log(' • Use your package manager to install Java and Maven'); console.log(' • Ubuntu/Debian: sudo apt install openjdk-17-jdk maven'); console.log(' • RHEL/CentOS: sudo yum install java-17-openjdk maven'); } // Quick start guide console.log(chalk.cyan('\n🚀 Quick Start Guide:\n')); const commands = [ { cmd: 'nitp-build --help', desc: 'Show all available commands' }, { cmd: 'nitp-build doctor', desc: 'Check system health' }, { cmd: 'nitp-build info', desc: 'Show system information' }, { cmd: 'nitp-build build', desc: 'Full build with tests' }, { cmd: 'nitp-build test', desc: 'Run tests with coverage' }, { cmd: 'nitp-build start:admin', desc: 'Start Admin API server' } ]; commands.forEach(({ cmd, desc }) => { console.log(` ${chalk.green(cmd.padEnd(25))} ${chalk.gray(desc)}`); }); // Configuration info console.log(chalk.cyan('\n⚙️ Configuration:\n')); console.log(` ${chalk.blue('Config file:')} lib/nitp-build-v2.json`); console.log(` ${chalk.blue('Available environments:')} dev, test, prod`); console.log(` ${chalk.blue('Available modules:')} core, admin, tnp, all`); // Final message const allGood = systemInfo.javaVersion && systemInfo.mavenVersion; console.log(chalk.cyan('\n' + '═'.repeat(60))); if (allGood) { console.log(chalk.green('🎉 Installation completed successfully!')); console.log(chalk.green(' Your system is ready to use NITP Build Tools.')); console.log(chalk.cyan('\n💡 Tip: Start with "nitp-build doctor" to verify everything is working.')); } else { console.log(chalk.yellow('⚠️ Installation completed with warnings.')); console.log(chalk.yellow(' Please install missing dependencies before using the build system.')); console.log(chalk.cyan('\n💡 Run "nitp-build doctor" after installing dependencies.')); } console.log(chalk.cyan('\n📚 Documentation: https://github.com/ashishkr375/adminportal_updated_new')); console.log(chalk.cyan('🐛 Report Issues: https://github.com/ashishkr375/adminportal_updated_new/issues')); console.log(chalk.cyan('👨‍💻 Developer: Ashish Kumar (https://github.com/ashishkr375)')); console.log(chalk.cyan('\n' + '═'.repeat(60) + '\n')); } // Run the post-install setup runPostInstall().catch((error) => { console.error(chalk.red('Error during post-install setup:'), error); process.exit(1); });