feat: add publish button to CMS for automated git push and deployment

This commit is contained in:
Do Siki
2026-04-27 01:19:42 +02:00
parent 649724de11
commit c71c692c6a
2 changed files with 63 additions and 6 deletions
+60 -3
View File
@@ -11,6 +11,7 @@
const http = require('http');
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const PORT = 4001;
const CONTENT_DIR = path.join(__dirname, 'proto', 'src', 'content');
@@ -99,9 +100,12 @@ const HTML = (activeFile, jsonData, message) => `<!DOCTYPE html>
.btn-save { background: linear-gradient(135deg,#3b82f6,#6366f1); color: #fff; border: none; padding: 11px 26px; border-radius: 8px; font-size: 14px; font-weight: 600; cursor: pointer; transition: opacity .2s, transform .1s; }
.btn-save:hover { opacity: .9; transform: translateY(-1px); }
.btn-save:active { transform: translateY(0); }
.preview-link { color: #64748b; font-size: 13px; text-decoration: none; }
.btn-publish { background: linear-gradient(135deg,#10b981,#059669); color: #fff; border: none; padding: 11px 26px; border-radius: 8px; font-size: 14px; font-weight: 600; cursor: pointer; transition: opacity .2s, transform .1s; }
.btn-publish:hover { opacity: .9; transform: translateY(-1px); }
.btn-publish:active { transform: translateY(0); }
.preview-link { color: #64748b; font-size: 13px; text-decoration: none; margin-left: auto; }
.preview-link:hover { color: #94a3b8; }
.save-status { font-size: 12px; color: #10b981; display: none; }
.save-status { font-size: 13px; font-weight: 500; display: none; margin-left: 8px; }
/* Toast */
.toast { position: fixed; top: 20px; right: 20px; padding: 13px 18px; border-radius: 9px; font-size: 14px; font-weight: 500; z-index: 200; animation: slideIn .3s ease; }
@@ -132,7 +136,8 @@ ${message ? `<div class="toast ${message.type === 'ok' ? 'ok' : 'err'}">${messag
<div class="bottom-bar">
<button class="btn-save" onclick="save()">💾 Mentés</button>
<span class="save-status" id="saveStatus">✅ Mentve!</span>
<button class="btn-publish" onclick="publish()" id="publishBtn">🚀 Publikálás & Élesítés</button>
<span class="save-status" id="saveStatus"></span>
<a href="http://localhost:3000" target="_blank" class="preview-link">🔗 Előnézet →</a>
</div>
@@ -340,6 +345,38 @@ async function save() {
setTimeout(() => status.style.display = 'none', 3000);
}
async function publish() {
const btn = document.getElementById('publishBtn');
const status = document.getElementById('saveStatus');
// Save first
await save();
btn.textContent = '⏳ Élesítés folyamatban...';
btn.disabled = true;
try {
const res = await fetch('/publish', { method: 'POST' });
const json = await res.json();
if (json.ok) {
status.textContent = '🚀 Sikeresen elküldve a szerverre!';
status.style.color = '#10b981';
} else {
status.textContent = '❌ Hiba az élesítésnél: ' + json.error;
status.style.color = '#f87171';
}
} catch (e) {
status.textContent = '❌ Hálózati hiba';
status.style.color = '#f87171';
}
btn.textContent = '🚀 Publikálás & Élesítés';
btn.disabled = false;
status.style.display = 'inline';
setTimeout(() => status.style.display = 'none', 5000);
}
function esc(v) {
return String(v).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
}
@@ -392,6 +429,26 @@ const server = http.createServer(async (req, res) => {
return;
}
// POST /publish — Git Commit & Push
if (req.method === 'POST' && u.pathname === '/publish') {
exec('git add . && git commit -m "content: frissítve a CMS-ből" && git push', { cwd: CONTENT_DIR }, (error, stdout, stderr) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
if (error) {
// If there's nothing to commit, it's fine
if (stdout.includes('nothing to commit') || stdout.includes('working tree clean')) {
res.end(JSON.stringify({ ok: true, output: 'No changes to commit' }));
} else {
res.end(JSON.stringify({ ok: false, error: stderr || stdout || error.message }));
}
} else {
// If a deploy.sh script exists, run it optionally in background
exec('cd ../../../ && ./deploy.sh production > deploy.log 2>&1 &');
res.end(JSON.stringify({ ok: true, output: stdout }));
}
});
return;
}
// GET / — editor UI
let message = null;
let jsonData = '{}';