import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const app = express();
const port = 8084;
const repoRoot = path.join(__dirname, '../..');
const hookTranspilerRoot = path.join(repoRoot, '../hook-transpiler');
const wasmDir = path.join(repoRoot, 'wasm');
const hookWasmDir = path.join(hookTranspilerRoot, 'wasm');
app.use('/wasm', (req, res, next) => {
const filename = req.path.split('/').pop();
if (!filename.endsWith('.wasm')) return next();
const stylerWasmPath = path.join(wasmDir, filename);
if (fs.existsSync(stylerWasmPath)) {
res.setHeader('Content-Type', 'application/wasm');
return res.sendFile(stylerWasmPath);
}
const hookWasmPath = path.join(hookWasmDir, filename);
if (fs.existsSync(hookWasmPath)) {
res.setHeader('Content-Type', 'application/wasm');
return res.sendFile(hookWasmPath);
}
res.status(404).send('WASM not found');
});
app.use('/wasm', express.static(wasmDir));
app.use('/wasm', express.static(hookWasmDir));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/themed-styler/dist', express.static(path.join(repoRoot, 'dist')));
app.use('/hook-transpiler/dist', express.static(path.join(hookTranspilerRoot, 'dist')));
app.use('/react', express.static(path.join(repoRoot, 'node_modules/react/umd')));
app.use('/react-dom', express.static(path.join(repoRoot, 'node_modules/react-dom/umd')));
app.get('/hooks/test-hook.jsx', (req, res) => {
res.setHeader('Content-Type', 'text/javascript');
res.send(`
import React from 'react';
export default function TestHook() {
return (
<div className="p-4 bg-blue-500 text-white rounded shadow-lg">
<h1 className="text-2xl font-bold">Hello from Test Hook!</h1>
<p className="mt-2">This hook was transpiled and rendered by HookRenderer.</p>
<div className="mt-4 p-2 bg-white text-blue-800 rounded">
Tailwind class 'bg-blue-500' should be active if themed-styler is working.
</div>
</div>
);
}
`);
});
app.get('*', (req, res, next) => {
if (req.path.includes('.')) {
return res.status(404).send('404: Not Found');
}
next();
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(port, () => {
console.log(`Test server running at http://localhost:${port}`);
});