1
0
mirror of https://github.com/twirl/The-API-Book.git synced 2025-05-19 21:33:04 +02:00
The-API-Book/scripts/build-graphs.mjs

125 lines
3.5 KiB
JavaScript
Raw Normal View History

2023-06-15 23:27:11 +03:00
import { readFile, writeFile, readdir, mkdir } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import { resolve, basename } from 'path';
2022-07-18 00:08:54 +03:00
import puppeteer from 'puppeteer';
2023-09-09 19:50:19 +03:00
import { templates } from '../src/templates.mjs';
2024-08-18 19:38:17 +03:00
import express from 'express';
2022-07-18 00:08:54 +03:00
const args = process.argv.slice(2);
2024-08-18 19:38:17 +03:00
const port = process.env.PORT ?? 3030;
2022-07-18 00:08:54 +03:00
const dir = process.cwd();
2022-08-27 23:07:11 +03:00
const langs = (args[0] || 'en,ru').split(',');
const target = args[1];
2022-07-18 00:08:54 +03:00
2024-08-18 19:38:17 +03:00
async function buildGraphs(langs, target, srcDir, dstDir, urlBuilder) {
2023-06-15 23:27:11 +03:00
for (const lang of langs) {
const graphDir = resolve(srcDir, lang, 'graphs');
const targets = target
? [resolve(graphDir, target)]
: await getGraphList(graphDir);
2022-08-13 16:00:49 +03:00
2023-06-15 23:27:11 +03:00
console.log(
`Lang=${lang}, ${targets.length} .mermaid files to process`
);
2022-07-18 00:08:54 +03:00
2023-06-15 23:27:11 +03:00
for (const t of targets) {
2024-08-18 19:38:17 +03:00
await buildGraph(lang, t, dstDir, urlBuilder(t));
2023-06-15 23:27:11 +03:00
}
}
2022-07-18 00:08:54 +03:00
}
2023-06-15 23:27:11 +03:00
async function getGraphList(srcDir) {
const files = await readdir(srcDir);
const result = [];
for (const file of files) {
if (file.endsWith('.mermaid')) {
result.push(resolve(srcDir, file));
}
}
return result;
2022-08-27 23:07:11 +03:00
}
2022-07-18 00:08:54 +03:00
2024-08-18 19:38:17 +03:00
async function buildGraph(lang, target, dstDir, url) {
2023-06-15 23:27:11 +03:00
const targetName = basename(target);
2024-08-18 19:38:17 +03:00
console.log(`Processing ${target}, basename: ${targetName} dst: ${dstDir}`);
2023-06-15 23:27:11 +03:00
const browser = await puppeteer.launch({
2023-09-09 19:50:19 +03:00
headless: 'new',
2023-06-15 23:27:11 +03:00
product: 'chrome',
defaultViewport: {
deviceScaleFactor: 2,
2023-09-09 19:50:19 +03:00
width: 1200,
height: 1200
2023-06-15 23:27:11 +03:00
}
});
const outFile = resolve(
dstDir,
`${targetName.replace('.mermaid', '')}.${lang}.png`
);
const page = await browser.newPage();
2024-08-18 19:38:17 +03:00
await page.goto(url, {
2023-06-15 23:27:11 +03:00
waitUntil: 'networkidle0'
});
2024-08-18 19:38:17 +03:00
console.log(`URL ${url} loaded`);
2023-09-09 19:50:19 +03:00
const $canvas = await page.$('svg');
const bounds = await $canvas.boundingBox();
2024-08-18 19:38:17 +03:00
// const body = await page.$('body');
await $canvas.screenshot({
2023-06-15 23:27:11 +03:00
path: outFile,
type: 'png',
2023-09-09 19:50:19 +03:00
captureBeyondViewport: true,
clip: bounds
2023-06-15 23:27:11 +03:00
});
await browser.close();
2024-08-18 19:38:17 +03:00
console.log(`File ${outFile} saved`);
}
async function main() {
const app = express();
app.use('/src', express.static('src'))
.use('/docs', express.static('docs'))
.get('/graph', async (req, res, next) => {
try {
const file = req.query.file;
console.log(`Reading file "${file}"`);
const html = templates.graphHtmlTemplate(
(await readFile(file)).toString('utf-8')
);
res.status(200);
res.end(html);
} catch (e) {
next(e);
}
})
.use((req, res, error, next) => {
res.status(500);
res.end(error.toString());
throw error;
});
app.listen(port, () => {
console.log(`Graph server started at localhost:${port}`);
});
await buildGraphs(
langs,
target,
resolve(dir, 'src'),
resolve(dir, 'src', 'img', 'graphs'),
(file) =>
`http://localhost:${port}/graph?file=${encodeURIComponent(file)}`
);
console.log('All graphs built successfully');
await new Promise((r) => setTimeout(() => r(), 60 * 60 * 1000));
2022-07-18 00:08:54 +03:00
}
2023-06-15 23:27:11 +03:00
2024-08-18 19:38:17 +03:00
main()
2023-06-15 23:27:11 +03:00
.catch((e) => {
console.error(e);
})
.finally(() => {
console.log('Graph build done');
process.exit(0);
});