2024-06-29 16:20:46 +05:30
|
|
|
/* eslint-disable no-console */
|
2024-06-30 01:45:14 +05:30
|
|
|
import chokidar from 'chokidar';
|
2023-08-13 18:33:41 +05:30
|
|
|
import cors from 'cors';
|
|
|
|
import { context } from 'esbuild';
|
2024-06-30 01:45:14 +05:30
|
|
|
import type { Request, Response } from 'express';
|
|
|
|
import express from 'express';
|
2023-11-19 00:44:44 +05:30
|
|
|
import { packageOptions } from '../.build/common.js';
|
2024-06-30 01:45:14 +05:30
|
|
|
import { generateLangium } from '../.build/generateLangium.js';
|
|
|
|
import { defaultOptions, getBuildConfig } from './util.js';
|
2023-08-13 18:51:02 +05:30
|
|
|
|
2023-11-24 10:38:32 +05:30
|
|
|
const configs = Object.values(packageOptions).map(({ packageName }) =>
|
|
|
|
getBuildConfig({ ...defaultOptions, minify: false, core: false, entryName: packageName })
|
2023-08-20 17:00:45 +03:00
|
|
|
);
|
2023-11-24 10:38:32 +05:30
|
|
|
const mermaidIIFEConfig = getBuildConfig({
|
|
|
|
...defaultOptions,
|
|
|
|
minify: false,
|
|
|
|
core: false,
|
|
|
|
entryName: 'mermaid',
|
|
|
|
format: 'iife',
|
|
|
|
});
|
|
|
|
configs.push(mermaidIIFEConfig);
|
|
|
|
|
2024-06-30 13:47:33 +05:30
|
|
|
const contexts = await Promise.all(
|
|
|
|
configs.map(async (config) => ({ config, context: await context(config) }))
|
|
|
|
);
|
2023-08-13 18:51:02 +05:30
|
|
|
|
2024-06-30 13:47:33 +05:30
|
|
|
let rebuildCounter = 1;
|
2023-08-13 18:51:02 +05:30
|
|
|
const rebuildAll = async () => {
|
2024-06-30 13:47:33 +05:30
|
|
|
const buildNumber = rebuildCounter++;
|
|
|
|
const timeLabel = `Rebuild ${buildNumber} Time (total)`;
|
|
|
|
console.time(timeLabel);
|
|
|
|
await Promise.all(
|
|
|
|
contexts.map(async ({ config, context }) => {
|
|
|
|
const buildVariant = `Rebuild ${buildNumber} Time (${Object.keys(config.entryPoints!)[0]} ${config.format})`;
|
|
|
|
console.time(buildVariant);
|
|
|
|
await context.rebuild();
|
|
|
|
console.timeEnd(buildVariant);
|
|
|
|
})
|
|
|
|
).catch((e) => console.error(e));
|
|
|
|
console.timeEnd(timeLabel);
|
2023-08-13 18:51:02 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
let clients: { id: number; response: Response }[] = [];
|
2024-06-30 01:45:14 +05:30
|
|
|
function eventsHandler(request: Request, response: Response) {
|
2023-08-13 18:51:02 +05:30
|
|
|
const headers = {
|
|
|
|
'Content-Type': 'text/event-stream',
|
|
|
|
Connection: 'keep-alive',
|
|
|
|
'Cache-Control': 'no-cache',
|
|
|
|
};
|
|
|
|
response.writeHead(200, headers);
|
|
|
|
const clientId = Date.now();
|
|
|
|
clients.push({
|
|
|
|
id: clientId,
|
|
|
|
response,
|
|
|
|
});
|
|
|
|
request.on('close', () => {
|
|
|
|
clients = clients.filter((client) => client.id !== clientId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-30 13:47:33 +05:30
|
|
|
let timeoutID: NodeJS.Timeout | undefined = undefined;
|
2023-08-13 18:51:02 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* Debounce file change events to avoid rebuilding multiple times.
|
|
|
|
*/
|
|
|
|
function handleFileChange() {
|
2024-06-30 13:47:33 +05:30
|
|
|
if (timeoutID !== undefined) {
|
|
|
|
clearTimeout(timeoutID);
|
2023-08-13 18:51:02 +05:30
|
|
|
}
|
2024-06-29 16:20:46 +05:30
|
|
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
2024-06-30 13:47:33 +05:30
|
|
|
timeoutID = setTimeout(async () => {
|
2023-08-13 18:51:02 +05:30
|
|
|
await rebuildAll();
|
|
|
|
sendEventsToAll();
|
2024-06-30 13:47:33 +05:30
|
|
|
timeoutID = undefined;
|
2023-08-13 18:51:02 +05:30
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendEventsToAll() {
|
|
|
|
clients.forEach(({ response }) => response.write(`data: ${Date.now()}\n\n`));
|
|
|
|
}
|
2023-08-13 18:33:41 +05:30
|
|
|
|
|
|
|
async function createServer() {
|
2023-08-22 13:38:23 +03:00
|
|
|
await generateLangium();
|
2023-08-14 00:40:48 +05:30
|
|
|
handleFileChange();
|
2023-08-13 18:33:41 +05:30
|
|
|
const app = express();
|
2023-08-13 18:51:02 +05:30
|
|
|
chokidar
|
2023-08-20 17:00:45 +03:00
|
|
|
.watch('**/src/**/*.{js,ts,langium,yaml,json}', {
|
2023-08-13 18:51:02 +05:30
|
|
|
ignoreInitial: true,
|
|
|
|
ignored: [/node_modules/, /dist/, /docs/, /coverage/],
|
|
|
|
})
|
2024-06-29 16:20:46 +05:30
|
|
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
2023-08-13 18:51:02 +05:30
|
|
|
.on('all', async (event, path) => {
|
|
|
|
// Ignore other events.
|
|
|
|
if (!['add', 'change'].includes(event)) {
|
|
|
|
return;
|
|
|
|
}
|
2024-06-30 13:47:33 +05:30
|
|
|
console.log(`${path} changed. Rebuilding...`);
|
2024-06-30 00:58:02 +05:30
|
|
|
if (path.endsWith('.langium')) {
|
2023-08-22 13:38:23 +03:00
|
|
|
await generateLangium();
|
2023-08-20 17:00:45 +03:00
|
|
|
}
|
2023-08-13 18:51:02 +05:30
|
|
|
handleFileChange();
|
|
|
|
});
|
2023-08-13 18:33:41 +05:30
|
|
|
|
|
|
|
app.use(cors());
|
2023-08-13 18:51:02 +05:30
|
|
|
app.get('/events', eventsHandler);
|
2023-11-19 00:44:44 +05:30
|
|
|
for (const { packageName } of Object.values(packageOptions)) {
|
|
|
|
app.use(express.static(`./packages/${packageName}/dist`));
|
|
|
|
}
|
2023-08-13 18:33:41 +05:30
|
|
|
app.use(express.static('demos'));
|
|
|
|
app.use(express.static('cypress/platform'));
|
|
|
|
|
|
|
|
app.listen(9000, () => {
|
|
|
|
console.log(`Listening on http://localhost:9000`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-29 16:20:46 +05:30
|
|
|
void createServer();
|