Skip to content

Commit 5c7e5b1

Browse files
committed
better graphs
1 parent a0b45c0 commit 5c7e5b1

14 files changed

+62
-32
lines changed

docs/API.en.epub

-30.1 KB
Binary file not shown.

docs/API.en.html

Lines changed: 5 additions & 5 deletions
Large diffs are not rendered by default.

docs/API.en.pdf

-5.85 KB
Binary file not shown.

docs/API.ru.epub

30.2 KB
Binary file not shown.

docs/API.ru.html

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

docs/API.ru.pdf

6.94 KB
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"@jest/globals": "^29.6.4",
1010
"@twirl/book-builder": "0.0.28",
1111
"@types/jest": "^29.5.4",
12-
"express": "^4.18.2",
12+
"express": "^4.19.2",
1313
"jest": "^29.6.4",
1414
"jest-environment-jsdom": "^29.6.4",
1515
"jest-mock-extended": "^3.0.5",

scripts/build-graphs.mjs

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ import { existsSync } from 'node:fs';
33
import { resolve, basename } from 'path';
44
import puppeteer from 'puppeteer';
55
import { templates } from '../src/templates.mjs';
6+
import express from 'express';
67

78
const args = process.argv.slice(2);
9+
const port = process.env.PORT ?? 3030;
810
const dir = process.cwd();
911
const langs = (args[0] || 'en,ru').split(',');
1012
const target = args[1];
1113

12-
async function buildGraphs(langs, target, srcDir, dstDir, tmpDir) {
13-
if (!existsSync(tmpDir)) {
14-
await mkdir(tmpDir);
15-
}
16-
14+
async function buildGraphs(langs, target, srcDir, dstDir, urlBuilder) {
1715
for (const lang of langs) {
1816
const graphDir = resolve(srcDir, lang, 'graphs');
1917
const targets = target
@@ -25,7 +23,7 @@ async function buildGraphs(langs, target, srcDir, dstDir, tmpDir) {
2523
);
2624

2725
for (const t of targets) {
28-
await buildGraph(lang, t, dstDir, tmpDir);
26+
await buildGraph(lang, t, dstDir, urlBuilder(t));
2927
}
3028
}
3129
}
@@ -41,15 +39,9 @@ async function getGraphList(srcDir) {
4139
return result;
4240
}
4341

44-
async function buildGraph(lang, target, dstDir, tmpDir) {
42+
async function buildGraph(lang, target, dstDir, url) {
4543
const targetName = basename(target);
46-
console.log(
47-
`Processing ${target}, basename: ${targetName} dst: ${dstDir}, tmp: ${tmpDir}`
48-
);
49-
const tmpFileName = resolve(tmpDir, `${targetName}.${lang}.html`);
50-
const graph = await readFile(target, 'utf-8');
51-
await writeFile(tmpFileName, templates.graphHtmlTemplate(graph));
52-
console.log(`Tmp file ${tmpFileName} written`);
44+
console.log(`Processing ${target}, basename: ${targetName} dst: ${dstDir}`);
5345
const browser = await puppeteer.launch({
5446
headless: 'new',
5547
product: 'chrome',
@@ -64,28 +56,65 @@ async function buildGraph(lang, target, dstDir, tmpDir) {
6456
`${targetName.replace('.mermaid', '')}.${lang}.png`
6557
);
6658
const page = await browser.newPage();
67-
await page.goto(tmpFileName, {
59+
await page.goto(url, {
6860
waitUntil: 'networkidle0'
6961
});
62+
console.log(`URL ${url} loaded`);
7063
const $canvas = await page.$('svg');
7164
const bounds = await $canvas.boundingBox();
72-
const body = await page.$('body');
73-
await body.screenshot({
65+
// const body = await page.$('body');
66+
await $canvas.screenshot({
7467
path: outFile,
7568
type: 'png',
7669
captureBeyondViewport: true,
7770
clip: bounds
7871
});
7972
await browser.close();
73+
console.log(`File ${outFile} saved`);
74+
}
75+
76+
async function main() {
77+
const app = express();
78+
app.use('/src', express.static('src'))
79+
.use('/docs', express.static('docs'))
80+
.get('/graph', async (req, res, next) => {
81+
try {
82+
const file = req.query.file;
83+
console.log(`Reading file "${file}"`);
84+
const html = templates.graphHtmlTemplate(
85+
(await readFile(file)).toString('utf-8')
86+
);
87+
res.status(200);
88+
res.end(html);
89+
} catch (e) {
90+
next(e);
91+
}
92+
})
93+
.use((req, res, error, next) => {
94+
res.status(500);
95+
res.end(error.toString());
96+
throw error;
97+
});
98+
99+
app.listen(port, () => {
100+
console.log(`Graph server started at localhost:${port}`);
101+
});
102+
103+
await buildGraphs(
104+
langs,
105+
target,
106+
resolve(dir, 'src'),
107+
resolve(dir, 'src', 'img', 'graphs'),
108+
(file) =>
109+
`http://localhost:${port}/graph?file=${encodeURIComponent(file)}`
110+
);
111+
112+
console.log('All graphs built successfully');
113+
114+
await new Promise((r) => setTimeout(() => r(), 60 * 60 * 1000));
80115
}
81116

82-
buildGraphs(
83-
langs,
84-
target,
85-
resolve(dir, 'src'),
86-
resolve(dir, 'src', 'img', 'graphs'),
87-
resolve(dir, '.tmp')
88-
)
117+
main()
89118
.catch((e) => {
90119
console.error(e);
91120
})
-11.2 KB
Loading
15.8 KB
Loading

0 commit comments

Comments
 (0)