UNPKG

petcarescript

Version:

PetCareScript - A modern, expressive programming language designed for humans with async, HTTP, database, and testing support

319 lines (238 loc) 8.91 kB
/** * PetCareScript Installer Builder */ const fs = require('fs'); const path = require('path'); const { execSync } = require('child_process'); const os = require('os'); class InstallerBuilder { constructor() { this.version = require('../package.json').version; this.installersDir = './installers'; this.distDir = './dist'; } build() { console.log('📦 Building installers...'); this.createInstallersDirectory(); this.buildWindowsInstaller(); this.buildLinuxInstaller(); this.generateChecksums(); console.log('✅ Installers built successfully!'); } createInstallersDirectory() { if (!fs.existsSync(this.installersDir)) { fs.mkdirSync(this.installersDir, { recursive: true }); } } buildWindowsInstaller() { console.log('🪟 Building Windows installer...'); const windowsScript = `@echo off echo Installing PetCareScript v${this.version}... REM Create installation directory if not exist "%PROGRAMFILES%\\PetCareScript" mkdir "%PROGRAMFILES%\\PetCareScript" REM Copy files xcopy /E /I /Y "%~dp0dist\\*" "%PROGRAMFILES%\\PetCareScript\\" REM Add to PATH setx PATH "%PATH%;%PROGRAMFILES%\\PetCareScript" /M REM Create desktop shortcut echo [InternetShortcut] > "%USERPROFILE%\\Desktop\\PetCareScript.url" echo URL=file:///"%PROGRAMFILES%\\PetCareScript\\index.js" >> "%USERPROFILE%\\Desktop\\PetCareScript.url" echo. echo PetCareScript v${this.version} installed successfully! echo You can now use 'pcs' command from any command prompt. echo. pause `; fs.writeFileSync(path.join(this.installersDir, 'install-windows.bat'), windowsScript); // Create Windows uninstaller const uninstallScript = `@echo off echo Uninstalling PetCareScript... REM Remove installation directory rmdir /S /Q "%PROGRAMFILES%\\PetCareScript" REM Remove from PATH (manual step required) echo Please manually remove "%PROGRAMFILES%\\PetCareScript" from your PATH environment variable. REM Remove desktop shortcut del "%USERPROFILE%\\Desktop\\PetCareScript.url" echo PetCareScript uninstalled successfully! pause `; fs.writeFileSync(path.join(this.installersDir, 'uninstall-windows.bat'), uninstallScript); // Package Windows installer this.packageWindowsInstaller(); } packageWindowsInstaller() { const packageDir = path.join(this.installersDir, 'windows-package'); if (!fs.existsSync(packageDir)) { fs.mkdirSync(packageDir, { recursive: true }); } // Copy dist files this.copyDirectory(this.distDir, path.join(packageDir, 'dist')); // Copy installer scripts fs.copyFileSync( path.join(this.installersDir, 'install-windows.bat'), path.join(packageDir, 'install.bat') ); fs.copyFileSync( path.join(this.installersDir, 'uninstall-windows.bat'), path.join(packageDir, 'uninstall.bat') ); // Create README const readme = `PetCareScript v${this.version} - Windows Installation To install: 1. Run install.bat as Administrator 2. Follow the instructions To uninstall: 1. Run uninstall.bat as Administrator For more information, visit: https://petcarescript.org `; fs.writeFileSync(path.join(packageDir, 'README.txt'), readme); console.log('📁 Windows package created in installers/windows-package/'); } buildLinuxInstaller() { console.log('🐧 Building Linux installer...'); const linuxScript = `#!/bin/bash echo "Installing PetCareScript v${this.version}..." # Check if running as root if [ "$EUID" -ne 0 ]; then echo "Please run as root (use sudo)" exit 1 fi # Create installation directory mkdir -p /usr/local/lib/petcarescript mkdir -p /usr/local/bin # Copy files cp -r ./dist/* /usr/local/lib/petcarescript/ # Create executable symlink ln -sf /usr/local/lib/petcarescript/index.js /usr/local/bin/pcs chmod +x /usr/local/lib/petcarescript/index.js # Make sure node is available if ! command -v node &> /dev/null; then echo "Node.js is required but not installed." echo "Please install Node.js (>=14.0.0) and try again." exit 1 fi echo "PetCareScript v${this.version} installed successfully!" echo "You can now use 'pcs' command from any terminal." `; fs.writeFileSync(path.join(this.installersDir, 'install-linux.sh'), linuxScript); // Make installer executable try { execSync(`chmod +x ${path.join(this.installersDir, 'install-linux.sh')}`); } catch (error) { console.warn('Could not make installer executable:', error.message); } // Create Linux uninstaller const uninstallScript = `#!/bin/bash echo "Uninstalling PetCareScript..." # Check if running as root if [ "$EUID" -ne 0 ]; then echo "Please run as root (use sudo)" exit 1 fi # Remove installation directory rm -rf /usr/local/lib/petcarescript # Remove symlink rm -f /usr/local/bin/pcs echo "PetCareScript uninstalled successfully!" `; fs.writeFileSync(path.join(this.installersDir, 'uninstall-linux.sh'), uninstallScript); try { execSync(`chmod +x ${path.join(this.installersDir, 'uninstall-linux.sh')}`); } catch (error) { console.warn('Could not make uninstaller executable:', error.message); } // Package Linux installer this.packageLinuxInstaller(); } packageLinuxInstaller() { const packageDir = path.join(this.installersDir, 'linux-package'); if (!fs.existsSync(packageDir)) { fs.mkdirSync(packageDir, { recursive: true }); } // Copy dist files this.copyDirectory(this.distDir, path.join(packageDir, 'dist')); // Copy installer scripts fs.copyFileSync( path.join(this.installersDir, 'install-linux.sh'), path.join(packageDir, 'install.sh') ); fs.copyFileSync( path.join(this.installersDir, 'uninstall-linux.sh'), path.join(packageDir, 'uninstall.sh') ); // Create README const readme = `PetCareScript v${this.version} - Linux Installation Requirements: - Node.js >= 14.0.0 To install: 1. sudo ./install.sh To uninstall: 1. sudo ./uninstall.sh For more information, visit: https://petcarescript.org `; fs.writeFileSync(path.join(packageDir, 'README.txt'), readme); console.log('📁 Linux package created in installers/linux-package/'); } generateChecksums() { console.log('🔒 Generating checksums...'); const crypto = require('crypto'); const checksums = {}; const packages = [ 'windows-package', 'linux-package' ]; for (const packageName of packages) { const packagePath = path.join(this.installersDir, packageName); if (fs.existsSync(packagePath)) { const checksum = this.calculateDirectoryChecksum(packagePath); checksums[packageName] = checksum; } } fs.writeFileSync( path.join(this.installersDir, 'checksums.json'), JSON.stringify(checksums, null, 2) ); console.log('🔒 Checksums generated'); } calculateDirectoryChecksum(dirPath) { const crypto = require('crypto'); const hash = crypto.createHash('sha256'); const files = this.getAllFiles(dirPath).sort(); for (const file of files) { const content = fs.readFileSync(file); hash.update(content); } return hash.digest('hex'); } getAllFiles(dirPath) { const files = []; const entries = fs.readdirSync(dirPath, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.join(dirPath, entry.name); if (entry.isDirectory()) { files.push(...this.getAllFiles(fullPath)); } else { files.push(fullPath); } } return files; } copyDirectory(src, dest) { if (!fs.existsSync(dest)) { fs.mkdirSync(dest, { recursive: true }); } const entries = fs.readdirSync(src, { withFileTypes: true }); for (const entry of entries) { const srcPath = path.join(src, entry.name); const destPath = path.join(dest, entry.name); if (entry.isDirectory()) { this.copyDirectory(srcPath, destPath); } else { fs.copyFileSync(srcPath, destPath); } } } } // Execute installer build const installerBuilder = new InstallerBuilder(); installerBuilder.build();