106 lines
3.1 KiB
JavaScript
106 lines
3.1 KiB
JavaScript
const express = require('express');
|
|
const cors = require('cors');
|
|
const { spawn } = require('child_process');
|
|
const path = require('path');
|
|
|
|
const app = express();
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
|
|
const PORT = process.env.UPDATER_PORT || 4001;
|
|
|
|
// State to keep track of the current update process
|
|
let updateState = {
|
|
status: 'idle', // idle, updating, success, error
|
|
progress: 0,
|
|
step: '',
|
|
logs: []
|
|
};
|
|
|
|
function addLog(msg) {
|
|
console.log(msg);
|
|
updateState.logs.push(msg);
|
|
if (updateState.logs.length > 100) {
|
|
updateState.logs.shift(); // Keep last 100 logs
|
|
}
|
|
}
|
|
|
|
function runCommand(command, args, stepName, progressStart, progressEnd) {
|
|
return new Promise((resolve, reject) => {
|
|
updateState.step = stepName;
|
|
updateState.progress = progressStart;
|
|
addLog(`>>> Exécution: ${command} ${args.join(' ')}`);
|
|
|
|
const proc = spawn(command, args, { cwd: path.join(__dirname), shell: true });
|
|
|
|
proc.stdout.on('data', (data) => {
|
|
addLog(data.toString().trim());
|
|
});
|
|
|
|
proc.stderr.on('data', (data) => {
|
|
addLog(`[ERREUR] ${data.toString().trim()}`);
|
|
});
|
|
|
|
proc.on('close', (code) => {
|
|
if (code !== 0) {
|
|
addLog(`>>> Échec avec le code ${code}`);
|
|
reject(new Error(`La commande ${command} a échoué.`));
|
|
} else {
|
|
updateState.progress = progressEnd;
|
|
addLog(`>>> Succès: ${stepName}`);
|
|
resolve();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
app.get('/api/update/status', (req, res) => {
|
|
res.json(updateState);
|
|
});
|
|
|
|
app.post('/api/update', async (req, res) => {
|
|
if (updateState.status === 'updating') {
|
|
return res.status(400).json({ error: 'Une mise à jour est déjà en cours.' });
|
|
}
|
|
|
|
updateState = {
|
|
status: 'updating',
|
|
progress: 10,
|
|
step: 'Initialisation',
|
|
logs: []
|
|
};
|
|
|
|
res.json({ message: 'Mise à jour démarrée' });
|
|
|
|
try {
|
|
// Step 1: Git Pull (10% to 30%)
|
|
await runCommand('git', ['pull'], 'Téléchargement des sources (git pull)', 10, 30);
|
|
|
|
// Step 2: NPM Install (30% to 60%)
|
|
await runCommand('npm', ['install'], 'Installation des dépendances (npm install)', 30, 60);
|
|
|
|
// Step 3: Build (60% to 100%)
|
|
await runCommand('npm', ['run', 'build'], 'Compilation de l\'application (npm run build)', 60, 100);
|
|
|
|
updateState.status = 'success';
|
|
updateState.step = 'Terminé';
|
|
updateState.progress = 100;
|
|
addLog('>>> Mise à jour terminée avec succès.');
|
|
|
|
} catch (error) {
|
|
updateState.status = 'error';
|
|
updateState.step = 'Erreur';
|
|
addLog(`>>> ERREUR CRITIQUE: ${error.message}`);
|
|
}
|
|
});
|
|
|
|
// Acknowledge update success and reset
|
|
app.post('/api/update/reset', (req, res) => {
|
|
updateState = { status: 'idle', progress: 0, step: '', logs: [] };
|
|
res.json({ message: 'Reset OK' });
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`[Ferrovia Updater] Écoute sur le port ${PORT}`);
|
|
});
|