math-tasks/bs-config.cjs

128 lines
4.0 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const { execFile } = require('child_process');
module.exports = {
server: {
baseDir: "."
},
files: [
"tasks/index.html",
"tasks/*/index.html",
"tasks/*/docs/*.template.html",
"tasks/*/docs/*.output.html",
"tasks/*/editor.html",
"assets/**/*",
"public/**/*"
],
port: 3300,
open: false,
notify: false,
ui: false,
middleware: [
{
route: "/",
handle: function (req, res, next) {
if (req.url === '/' || req.url === '') {
res.writeHead(302, { 'Location': '/tasks/index.html' });
res.end();
return;
}
next();
}
},
{
route: "/api/save-edits",
handle: function (req, res, next) {
if (req.method !== 'POST') return next();
let body = '';
req.on('data', chunk => body += chunk);
req.on('end', () => {
try {
const payload = JSON.parse(body);
const { taskType, docId, data } = payload;
if (!taskType || !docId || !data) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Missing taskType, docId, or data' }));
return;
}
const docsDir = path.join(__dirname, 'tasks', taskType, 'docs');
const tempDir = path.join(__dirname, 'tasks', taskType, 'temp');
fs.mkdirSync(tempDir, { recursive: true });
const dataPath = path.join(docsDir, docId + '.data.json');
const diffPath = path.join(tempDir, docId + '.diff.json');
// Read old data for diff
let oldData = null;
try {
oldData = JSON.parse(fs.readFileSync(dataPath, 'utf-8'));
} catch (e) {
// No previous data — first save
}
// Write new data
fs.writeFileSync(dataPath, JSON.stringify(data, null, 2));
// Compute and write diff
const diff = computeDiff(oldData, data, docId);
fs.writeFileSync(diffPath, JSON.stringify(diff, null, 2));
// Run generate.mjs → output.html + screenshots (async, don't block response)
const generateScript = path.join(__dirname, 'tasks', taskType, 'scripts', 'generate.mjs');
if (fs.existsSync(generateScript)) {
execFile('node', [generateScript, docId], { timeout: 60000 }, (err, stdout, stderr) => {
if (err) console.error(`[generate] ${taskType}/${docId} failed:`, stderr || err.message);
else console.log(`[generate] ${stdout.trim()}`);
});
}
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ ok: true, path: dataPath }));
} catch (e) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: e.message }));
}
});
}
}
]
};
function computeDiff(oldData, newData, docId) {
const changes = [];
const timestamp = new Date().toISOString();
if (!oldData) {
return { timestamp, docId, firstSave: true, changes: [] };
}
const oldPages = oldData.pages || [];
const newPages = newData.pages || [];
for (let i = 0; i < Math.max(oldPages.length, newPages.length); i++) {
const oldPage = oldPages[i] || {};
const newPage = newPages[i] || {};
const pageNum = (newPage.page || oldPage.page || i + 1);
// Compare all element arrays in the page
for (const key of new Set([...Object.keys(oldPage), ...Object.keys(newPage)])) {
if (key === 'page') continue;
const oldVal = JSON.stringify(oldPage[key]);
const newVal = JSON.stringify(newPage[key]);
if (oldVal !== newVal) {
changes.push({
page: pageNum,
field: key,
from: oldPage[key],
to: newPage[key]
});
}
}
}
return { timestamp, docId, changes };
}