209 lines
6.1 KiB
JavaScript
209 lines
6.1 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* Deploy-Script für HexaHost Backend
|
||
*
|
||
* Dieses Script:
|
||
* 1. Prüft ob wir auf develop sind
|
||
* 2. Führt den Build aus (npm run build)
|
||
* 3. Wechselt zu main
|
||
* 4. Kopiert die dist/ Dateien
|
||
* 5. Committed und pusht
|
||
* 6. Wechselt zurück zu develop
|
||
*/
|
||
|
||
const { execSync } = require('child_process');
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
|
||
// Farben für die Konsole
|
||
const colors = {
|
||
reset: '\x1b[0m',
|
||
green: '\x1b[32m',
|
||
red: '\x1b[31m',
|
||
yellow: '\x1b[33m',
|
||
blue: '\x1b[34m',
|
||
cyan: '\x1b[36m'
|
||
};
|
||
|
||
function log(message, color = 'reset') {
|
||
console.log(`${colors[color]}${message}${colors.reset}`);
|
||
}
|
||
|
||
function exec(command, options = {}) {
|
||
try {
|
||
return execSync(command, {
|
||
encoding: 'utf8',
|
||
stdio: options.silent ? 'pipe' : 'inherit',
|
||
...options
|
||
});
|
||
} catch (error) {
|
||
if (!options.ignoreError) {
|
||
throw error;
|
||
}
|
||
return null;
|
||
}
|
||
}
|
||
|
||
function getCurrentBranch() {
|
||
return exec('git branch --show-current', { silent: true }).trim();
|
||
}
|
||
|
||
function hasUncommittedChanges() {
|
||
const status = exec('git status --porcelain', { silent: true });
|
||
return status && status.trim().length > 0;
|
||
}
|
||
|
||
function copyRecursive(src, dest) {
|
||
if (!fs.existsSync(src)) {
|
||
return;
|
||
}
|
||
|
||
const stats = fs.statSync(src);
|
||
|
||
if (stats.isDirectory()) {
|
||
if (!fs.existsSync(dest)) {
|
||
fs.mkdirSync(dest, { recursive: true });
|
||
}
|
||
|
||
const files = fs.readdirSync(src);
|
||
for (const file of files) {
|
||
copyRecursive(
|
||
path.join(src, file),
|
||
path.join(dest, file)
|
||
);
|
||
}
|
||
} else {
|
||
fs.copyFileSync(src, dest);
|
||
}
|
||
}
|
||
|
||
async function deploy() {
|
||
log('\n🚀 HexaHost Deploy Script\n', 'cyan');
|
||
log('=' .repeat(50), 'cyan');
|
||
|
||
const startBranch = getCurrentBranch();
|
||
|
||
// 1. Prüfen ob wir auf develop sind
|
||
log('\n📋 Schritt 1: Branch prüfen...', 'blue');
|
||
if (startBranch !== 'develop') {
|
||
log(`❌ Du bist auf '${startBranch}', nicht auf 'develop'!`, 'red');
|
||
log(' Bitte wechsle erst zu develop: git checkout develop', 'yellow');
|
||
process.exit(1);
|
||
}
|
||
log('✅ Auf develop Branch', 'green');
|
||
|
||
// 2. Prüfen ob uncommitted changes existieren
|
||
log('\n📋 Schritt 2: Uncommitted Changes prüfen...', 'blue');
|
||
if (hasUncommittedChanges()) {
|
||
log('❌ Es gibt uncommitted changes!', 'red');
|
||
log(' Bitte erst committen oder stashen.', 'yellow');
|
||
exec('git status --short');
|
||
process.exit(1);
|
||
}
|
||
log('✅ Keine uncommitted changes', 'green');
|
||
|
||
// 3. Build ausführen
|
||
log('\n📋 Schritt 3: Build ausführen...', 'blue');
|
||
try {
|
||
exec('npm run build');
|
||
log('✅ Build erfolgreich', 'green');
|
||
} catch (error) {
|
||
log('❌ Build fehlgeschlagen!', 'red');
|
||
process.exit(1);
|
||
}
|
||
|
||
// 4. Zu main wechseln
|
||
log('\n📋 Schritt 4: Zu main wechseln...', 'blue');
|
||
exec('git checkout main');
|
||
log('✅ Auf main gewechselt', 'green');
|
||
|
||
// 5. Produktionsdateien ins Root kopieren
|
||
log('\n📋 Schritt 5: Produktionsdateien aktualisieren...', 'blue');
|
||
|
||
// Ordner die aus dist/ ins Root kopiert werden
|
||
const prodFolders = ['assets', 'config', 'includes'];
|
||
|
||
// Alte Ordner im Root löschen
|
||
for (const folder of prodFolders) {
|
||
const folderPath = path.join(process.cwd(), folder);
|
||
if (fs.existsSync(folderPath)) {
|
||
fs.rmSync(folderPath, { recursive: true, force: true });
|
||
}
|
||
}
|
||
|
||
// Dateien aus develop/dist/ ins Root holen
|
||
for (const folder of prodFolders) {
|
||
exec(`git checkout develop -- dist/${folder}/`, { silent: true, ignoreError: true });
|
||
|
||
// Von dist/ ins Root verschieben
|
||
const srcPath = path.join(process.cwd(), 'dist', folder);
|
||
const destPath = path.join(process.cwd(), folder);
|
||
if (fs.existsSync(srcPath)) {
|
||
copyRecursive(srcPath, destPath);
|
||
}
|
||
}
|
||
|
||
// dist/ Ordner aufräumen (falls vorhanden)
|
||
const distPath = path.join(process.cwd(), 'dist');
|
||
if (fs.existsSync(distPath)) {
|
||
fs.rmSync(distPath, { recursive: true, force: true });
|
||
}
|
||
|
||
log('✅ Produktionsdateien aktualisiert', 'green');
|
||
|
||
// 6. Prüfen ob es Änderungen gibt
|
||
log('\n📋 Schritt 6: Änderungen prüfen...', 'blue');
|
||
if (!hasUncommittedChanges()) {
|
||
log('ℹ️ Keine Änderungen - nichts zu deployen', 'yellow');
|
||
exec('git checkout develop');
|
||
log('\n✅ Zurück auf develop', 'green');
|
||
return;
|
||
}
|
||
|
||
// 7. Commit erstellen
|
||
log('\n📋 Schritt 7: Commit erstellen...', 'blue');
|
||
const timestamp = new Date().toISOString().split('T')[0];
|
||
const commitMessage = `deploy: Produktions-Update ${timestamp}`;
|
||
|
||
exec('git add assets/ config/ includes/');
|
||
exec(`git commit -m "${commitMessage}"`);
|
||
log(`✅ Commit erstellt: "${commitMessage}"`, 'green');
|
||
|
||
// 8. Push zu origin
|
||
log('\n📋 Schritt 8: Push zu origin...', 'blue');
|
||
try {
|
||
exec('git push origin main');
|
||
log('✅ Push erfolgreich', 'green');
|
||
} catch (error) {
|
||
log('⚠️ Push fehlgeschlagen - manuell pushen mit: git push origin main', 'yellow');
|
||
}
|
||
|
||
// 9. Zurück zu develop
|
||
log('\n📋 Schritt 9: Zurück zu develop...', 'blue');
|
||
exec('git checkout develop');
|
||
log('✅ Zurück auf develop', 'green');
|
||
|
||
// Fertig!
|
||
log('\n' + '=' .repeat(50), 'cyan');
|
||
log('🎉 Deploy erfolgreich abgeschlossen!', 'green');
|
||
log('=' .repeat(50) + '\n', 'cyan');
|
||
}
|
||
|
||
// Script starten
|
||
deploy().catch(error => {
|
||
log(`\n❌ Fehler: ${error.message}`, 'red');
|
||
|
||
// Versuche zurück zu develop zu wechseln
|
||
try {
|
||
const currentBranch = getCurrentBranch();
|
||
if (currentBranch !== 'develop') {
|
||
log('🔄 Versuche zurück zu develop zu wechseln...', 'yellow');
|
||
exec('git checkout develop', { ignoreError: true });
|
||
}
|
||
} catch (e) {
|
||
// Ignorieren
|
||
}
|
||
|
||
process.exit(1);
|
||
});
|