@writedocs/generator 0.11.1 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/cli/build.js +203 -177
- package/src/cli/og-image-worker.js +20 -0
- package/src/cli/og-images.js +70 -0
- package/src/cli/run-astro.js +97 -93
- package/src/components/ApiReferencePanel.astro +1792 -1792
- package/src/layout/BaseLayout.astro +1 -3
- package/src/layout/components/MobileMenu.astro +9 -13
- package/src/layout/components/NavTree.astro +21 -6
- package/src/layout/components/Sidebar.astro +4 -4
- package/src/layout/styles/base.css +326 -327
- package/src/layout/styles/mobile-menu.css +285 -285
- package/src/lib/og-cards.js +33 -0
- package/src/lib/og-image.js +235 -168
- package/src/pages/og/[...slug].png.ts +63 -64
- package/src/scripts/mobile-menu.ts +80 -55
- package/src/scripts/sidebar-scroll.ts +61 -65
package/package.json
CHANGED
package/src/cli/build.js
CHANGED
|
@@ -1,177 +1,203 @@
|
|
|
1
|
-
import { canonicalPath } from '../lib/canonical-path.js';
|
|
2
|
-
import fs from 'node:fs';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
import { runAstro } from './run-astro.js';
|
|
5
|
-
import { runPagefind } from './run-pagefind.js';
|
|
6
|
-
import { preflightCheck, sameDriveCheck, writableInstallCheck } from './preflight.js';
|
|
7
|
-
import { generateApiPages } from './generate-api-pages.js';
|
|
8
|
-
import { writeRedirectsFile } from './write-redirects-file.js';
|
|
9
|
-
import { rewriteRedirectPages } from './rewrite-redirect-pages.js';
|
|
10
|
-
import { readConfigText } from '../lib/config-file.js';
|
|
11
|
-
import { writeMcpFiles } from './write-mcp-files.js';
|
|
12
|
-
import { pagesWithoutStyles } from './check-built-styles.js';
|
|
13
|
-
import { writedocsBuildStagingDir } from '../lib/writedocs-temp-dir.js';
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
//
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
seen.
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
api
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
let
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
//
|
|
113
|
-
|
|
114
|
-
//
|
|
115
|
-
//
|
|
116
|
-
//
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
//
|
|
120
|
-
//
|
|
121
|
-
//
|
|
122
|
-
//
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
//
|
|
129
|
-
//
|
|
130
|
-
//
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
log.
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
1
|
+
import { canonicalPath } from '../lib/canonical-path.js';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { runAstro } from './run-astro.js';
|
|
5
|
+
import { runPagefind } from './run-pagefind.js';
|
|
6
|
+
import { preflightCheck, sameDriveCheck, writableInstallCheck } from './preflight.js';
|
|
7
|
+
import { generateApiPages } from './generate-api-pages.js';
|
|
8
|
+
import { writeRedirectsFile } from './write-redirects-file.js';
|
|
9
|
+
import { rewriteRedirectPages } from './rewrite-redirect-pages.js';
|
|
10
|
+
import { readConfigText } from '../lib/config-file.js';
|
|
11
|
+
import { writeMcpFiles } from './write-mcp-files.js';
|
|
12
|
+
import { pagesWithoutStyles } from './check-built-styles.js';
|
|
13
|
+
import { writedocsBuildStagingDir, writedocsTempDir } from '../lib/writedocs-temp-dir.js';
|
|
14
|
+
import { drawOgImages } from './og-images.js';
|
|
15
|
+
import { normalizeBasePath } from '../lib/base-path.js';
|
|
16
|
+
import { siteDirFor, moveRootFiles } from './base-path-layout.js';
|
|
17
|
+
import { log, step, plural, duration, displayPath, formatProblems, color, CliExit } from './output.js';
|
|
18
|
+
import { describeError, builtRoute, authorWarning, verboseLine } from './astro-output.js';
|
|
19
|
+
import { reportApiPages } from './api-pages-output.js';
|
|
20
|
+
|
|
21
|
+
export async function runBuild({ contentDir, packageRoot, verbose = false }) {
|
|
22
|
+
// One spelling of each folder throughout - see lib/canonical-path.js.
|
|
23
|
+
contentDir = canonicalPath(contentDir);
|
|
24
|
+
packageRoot = canonicalPath(packageRoot);
|
|
25
|
+
const started = Date.now();
|
|
26
|
+
preflightCheck(contentDir);
|
|
27
|
+
writableInstallCheck(packageRoot);
|
|
28
|
+
|
|
29
|
+
// Collected while building, printed together at the end - a warning in
|
|
30
|
+
// the middle of a spinner is easy to miss.
|
|
31
|
+
const warnings = [];
|
|
32
|
+
const notes = [];
|
|
33
|
+
const seen = new Set();
|
|
34
|
+
const addWarning = (where, message) => {
|
|
35
|
+
const key = `${where ?? ''}\n${message}`;
|
|
36
|
+
if (seen.has(key)) return;
|
|
37
|
+
seen.add(key);
|
|
38
|
+
warnings.push({ where, message });
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const api = await generateApiPages({ contentDir });
|
|
42
|
+
sameDriveCheck(contentDir, packageRoot, { generatedPages: api.groups.length > 0 });
|
|
43
|
+
reportApiPages(api);
|
|
44
|
+
api.warnings.forEach((message) => addWarning(null, message));
|
|
45
|
+
|
|
46
|
+
const building = step('Building pages');
|
|
47
|
+
const buildStarted = Date.now();
|
|
48
|
+
let pages = 0;
|
|
49
|
+
let fatal = null;
|
|
50
|
+
const rawTail = [];
|
|
51
|
+
// The link-preview cards: Astro's og route only writes their list here,
|
|
52
|
+
// and this process draws them after Astro, in parallel (og-images.js).
|
|
53
|
+
// Removed first, so a list from an earlier build is never drawn.
|
|
54
|
+
const ogCardsFile = path.join(writedocsTempDir(contentDir), 'og-cards.json');
|
|
55
|
+
fs.mkdirSync(path.dirname(ogCardsFile), { recursive: true });
|
|
56
|
+
fs.rmSync(ogCardsFile, { force: true });
|
|
57
|
+
const { exited } = runAstro('build', {
|
|
58
|
+
packageRoot,
|
|
59
|
+
contentDir,
|
|
60
|
+
env: { WRITEDOCS_OG_CARDS_FILE: ogCardsFile },
|
|
61
|
+
onEvent(event) {
|
|
62
|
+
if (verbose) {
|
|
63
|
+
const line = verboseLine(event);
|
|
64
|
+
if (line !== null) log.line(color.dim(line));
|
|
65
|
+
}
|
|
66
|
+
if (event.type === 'fatal') {
|
|
67
|
+
fatal = event.error;
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (event.type === 'report') {
|
|
71
|
+
if (event.level === 'warn') addWarning(event.where, event.message);
|
|
72
|
+
else if (event.level === 'info' && !notes.includes(event.message)) notes.push(event.message);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (event.type === 'raw') {
|
|
76
|
+
rawTail.push(event.line);
|
|
77
|
+
if (rawTail.length > 40) rawTail.shift();
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const route = builtRoute(event);
|
|
81
|
+
if (route) {
|
|
82
|
+
if (route.endsWith('.html')) pages += 1;
|
|
83
|
+
building.update(`Building pages ${color.dim(`(${pages})`)}`);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const warning = authorWarning(event, contentDir, { root: packageRoot });
|
|
87
|
+
if (warning) addWarning(null, warning);
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
const code = await exited;
|
|
91
|
+
|
|
92
|
+
if (fatal || code !== 0) {
|
|
93
|
+
building.fail('Build failed');
|
|
94
|
+
if (fatal) {
|
|
95
|
+
const problem = describeError(fatal, contentDir, { root: packageRoot });
|
|
96
|
+
log.line();
|
|
97
|
+
log.line(formatProblems([{ where: problem.where, message: problem.message }]));
|
|
98
|
+
if (problem.hint) log.line(`\n${color.dim(` Hint: ${problem.hint}`)}`);
|
|
99
|
+
if (verbose && fatal.stack) log.line(`\n${color.dim(fatal.stack)}`);
|
|
100
|
+
} else if (rawTail.length) {
|
|
101
|
+
// No structured error (the worker itself crashed) - show what it printed.
|
|
102
|
+
log.line();
|
|
103
|
+
log.line(color.dim(rawTail.join('\n')));
|
|
104
|
+
}
|
|
105
|
+
log.line();
|
|
106
|
+
log.line(color.dim('Run "writedocs validate" to list every problem in the project at once.'));
|
|
107
|
+
throw new CliExit(1);
|
|
108
|
+
}
|
|
109
|
+
building.succeed(`Built ${plural(pages, 'page')} ${color.dim(`in ${duration(Date.now() - buildStarted)}`)}`);
|
|
110
|
+
|
|
111
|
+
const distDir = path.join(contentDir, 'dist');
|
|
112
|
+
// writedocs.json has been through the build's own validation by now.
|
|
113
|
+
const siteConfig = JSON.parse(readConfigText(contentDir));
|
|
114
|
+
// With a `basePath` ("/docs"), the site goes to dist/docs/ and only the
|
|
115
|
+
// host's own files stay at the root of dist/ - see base-path-layout.js.
|
|
116
|
+
// Without one, siteDir is distDir, and every step below is what it was.
|
|
117
|
+
const basePath = normalizeBasePath(siteConfig.basePath);
|
|
118
|
+
const siteDir = siteDirFor(distDir, basePath);
|
|
119
|
+
// Astro just wrote its actual output to writedocsBuildStagingDir()
|
|
120
|
+
// (astro.config.mjs's own `outDir`), not distDir directly - see that
|
|
121
|
+
// function's own comment (writedocs-temp-dir.js) for why. fs.cpSync
|
|
122
|
+
// (unlike the fs.rename() Astro uses internally to get *into* that
|
|
123
|
+
// staging dir in the first place) copies across filesystem boundaries
|
|
124
|
+
// without issue, so this is the one step that actually needs to bridge
|
|
125
|
+
// packageRoot and contentDir potentially living on different devices.
|
|
126
|
+
// distDir is cleared first so a rebuild doesn't leave a stale file
|
|
127
|
+
// behind from a previous build that the new one no longer produces -
|
|
128
|
+
// the same "the output directory reflects exactly this build, nothing
|
|
129
|
+
// older" expectation `astro build` itself already guarantees when
|
|
130
|
+
// writing directly into outDir.
|
|
131
|
+
const stagingDir = writedocsBuildStagingDir(packageRoot, contentDir);
|
|
132
|
+
// The cards Astro's route listed, into the staging folder's og/ - the
|
|
133
|
+
// same files the route would have made, one thread per CPU (at most 4).
|
|
134
|
+
// Drawn here, before the staging folder replaces dist/: when drawing
|
|
135
|
+
// fails, the build stops with dist/ as it was - no dist/ at all, or the
|
|
136
|
+
// last good build's - the same as when Astro itself fails. Drawn after the
|
|
137
|
+
// copy, a failure left a dist/ with the new pages and none of their cards.
|
|
138
|
+
const drawing = step('Drawing link previews');
|
|
139
|
+
const drawStarted = Date.now();
|
|
140
|
+
try {
|
|
141
|
+
const drawn = await drawOgImages(ogCardsFile, stagingDir);
|
|
142
|
+
if (drawn) drawing.succeed(`Drew ${plural(drawn, 'preview image')} ${color.dim(`in ${duration(Date.now() - drawStarted)}`)}`);
|
|
143
|
+
else drawing.stop();
|
|
144
|
+
} catch (err) {
|
|
145
|
+
drawing.fail('Drawing the link previews failed');
|
|
146
|
+
log.line();
|
|
147
|
+
log.line(color.dim(err.message));
|
|
148
|
+
throw new CliExit(1);
|
|
149
|
+
}
|
|
150
|
+
fs.rmSync(distDir, { recursive: true, force: true });
|
|
151
|
+
fs.cpSync(stagingDir, siteDir, { recursive: true });
|
|
152
|
+
fs.rmSync(stagingDir, { recursive: true, force: true });
|
|
153
|
+
moveRootFiles(siteDir, distDir);
|
|
154
|
+
// Every page writedocs renders links the site's stylesheet. Builds have
|
|
155
|
+
// come out without it on every page while reporting success (the package
|
|
156
|
+
// folder spelled `c:`, fixed in lib/canonical-path.js) - so it's checked
|
|
157
|
+
// here, and a build like that stops instead of being deployed unstyled.
|
|
158
|
+
const unstyled = pagesWithoutStyles(distDir, { basePath });
|
|
159
|
+
if (unstyled.length) {
|
|
160
|
+
log.error(`The build produced ${plural(unstyled.length, 'page')} without the site's styles - for example ${unstyled[0]}.`);
|
|
161
|
+
log.detail(color.dim('This is a fault in the build, not in your pages. Build again; if it happens again, report it with the output of --verbose.'));
|
|
162
|
+
throw new CliExit(1);
|
|
163
|
+
}
|
|
164
|
+
// Astro's own writedocs.json `redirects` + the automatic "/" redirect only
|
|
165
|
+
// ever produce client-side meta-refresh pages (see write-redirects-file.js)
|
|
166
|
+
// - this turns those into real instant edge redirects on hosts that read
|
|
167
|
+
// a `_redirects` file (Cloudflare Pages, Netlify), purely additively.
|
|
168
|
+
writeRedirectsFile(siteDir, contentDir, { outDir: distDir, basePath });
|
|
169
|
+
// Every other host follows Astro's redirect pages - rewritten so the
|
|
170
|
+
// reader never sees one (rewrite-redirect-pages.js).
|
|
171
|
+
rewriteRedirectPages(siteDir, { background: siteConfig.styles?.background?.colors });
|
|
172
|
+
// The site's MCP server at /mcp: mcp-index.json is already in dist/ (an
|
|
173
|
+
// Astro route); this adds the Cloudflare _worker.js that serves it - see
|
|
174
|
+
// write-mcp-files.js for every host.
|
|
175
|
+
const { version: writedocsVersion } = JSON.parse(fs.readFileSync(path.join(packageRoot, 'package.json'), 'utf-8'));
|
|
176
|
+
const mcp = writeMcpFiles(distDir, contentDir, writedocsVersion, { siteDir, basePath });
|
|
177
|
+
if (mcp.written.length) notes.push(`MCP server at ${basePath}/mcp - dist/_worker.js runs it on Cloudflare; mcp-index.json holds the pages.`);
|
|
178
|
+
if (mcp.skipped) addWarning(null, `No _worker.js for the MCP server: ${mcp.skipped}.`);
|
|
179
|
+
|
|
180
|
+
const indexing = step('Indexing search');
|
|
181
|
+
try {
|
|
182
|
+
await runPagefind(siteDir, { packageRoot });
|
|
183
|
+
} catch (err) {
|
|
184
|
+
indexing.fail('Search indexing failed');
|
|
185
|
+
log.line();
|
|
186
|
+
log.line(color.dim(err.message));
|
|
187
|
+
throw new CliExit(1);
|
|
188
|
+
}
|
|
189
|
+
indexing.succeed('Indexed search');
|
|
190
|
+
|
|
191
|
+
if (warnings.length) {
|
|
192
|
+
log.line();
|
|
193
|
+
log.warn(plural(warnings.length, 'warning'));
|
|
194
|
+
log.line();
|
|
195
|
+
log.line(formatProblems(warnings));
|
|
196
|
+
}
|
|
197
|
+
if (notes.length) {
|
|
198
|
+
log.line();
|
|
199
|
+
notes.forEach((note) => log.info(note));
|
|
200
|
+
}
|
|
201
|
+
log.line();
|
|
202
|
+
log.success(`Done in ${duration(Date.now() - started)} ${color.dim('-')} site written to ${color.bold(displayPath(distDir))}`);
|
|
203
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// One of og-images.js's threads: asks for a card, draws it into the site's
|
|
2
|
+
// og/ folder, asks for the next - until it's sent null.
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { parentPort, workerData } from 'node:worker_threads';
|
|
6
|
+
import { ogImagePng, ogImagePath } from '../lib/og-image.js';
|
|
7
|
+
import { ogCardOptions } from '../lib/og-cards.js';
|
|
8
|
+
|
|
9
|
+
const { shared, siteDir } = workerData;
|
|
10
|
+
parentPort.on('message', async (card) => {
|
|
11
|
+
if (card === null) {
|
|
12
|
+
parentPort.close();
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const file = path.join(siteDir, ...ogImagePath(card.slug).split('/').filter(Boolean));
|
|
16
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
17
|
+
fs.writeFileSync(file, await ogImagePng(ogCardOptions(shared, card)));
|
|
18
|
+
parentPort.postMessage('drawn');
|
|
19
|
+
});
|
|
20
|
+
parentPort.postMessage('ready');
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// `writedocs build`'s link-preview cards (lib/og-cards.js), drawn after
|
|
2
|
+
// Astro, in worker threads - one per CPU this process may use
|
|
3
|
+
// (os.availableParallelism(), which follows a container's CPU limit), and
|
|
4
|
+
// at most MAX_THREADS (ogThreads()).
|
|
5
|
+
//
|
|
6
|
+
// Inside Astro they were one route among the others (src/pages/og/
|
|
7
|
+
// [...slug].png.ts), and Astro renders its routes one at a time on one core:
|
|
8
|
+
// on a site with 800 OpenAPI operations, the 800 cards were a third of the
|
|
9
|
+
// build. The route still decides nothing here - it writes the list of cards
|
|
10
|
+
// to the file it's given (WRITEDOCS_OG_CARDS_FILE, set by build.js) and
|
|
11
|
+
// renders none; each thread draws a card with the same ogImagePng() call the
|
|
12
|
+
// route makes in `writedocs dev`, so the files are the same, byte for byte.
|
|
13
|
+
//
|
|
14
|
+
// Each thread holds its own resvg, satori and fonts, ~110 MB: the threads
|
|
15
|
+
// start after Astro has exited, so that isn't on top of its peak.
|
|
16
|
+
import fs from 'node:fs';
|
|
17
|
+
import os from 'node:os';
|
|
18
|
+
import { Worker } from 'node:worker_threads';
|
|
19
|
+
|
|
20
|
+
/** No more threads than this, however many CPUs a machine shows: a CI host
|
|
21
|
+
* can show dozens without a quota, and dozens of threads would be gigabytes.
|
|
22
|
+
* The platform's container has 4 CPUs; 800 cards take ~3.5 s there. */
|
|
23
|
+
export const MAX_THREADS = 4;
|
|
24
|
+
|
|
25
|
+
/** How many threads draw the cards, for `available` CPUs. */
|
|
26
|
+
export function ogThreads(available = os.availableParallelism()) {
|
|
27
|
+
return Math.max(1, Math.min(available, MAX_THREADS));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Draws every card in `listFile` into `siteDir`/og/ - the site's root
|
|
31
|
+
* folder: build.js passes the staging folder, before it replaces dist/.
|
|
32
|
+
* Returns how many.
|
|
33
|
+
* The list is removed afterwards, drawn or not: it's only this build's, and
|
|
34
|
+
* it holds the logo, base64-encoded - no reason to leave it in the temp
|
|
35
|
+
* folder. */
|
|
36
|
+
export async function drawOgImages(listFile, siteDir, { threads = ogThreads() } = {}) {
|
|
37
|
+
try {
|
|
38
|
+
return await drawEach(listFile, siteDir, threads);
|
|
39
|
+
} finally {
|
|
40
|
+
fs.rmSync(listFile, { force: true });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function drawEach(listFile, siteDir, threads) {
|
|
45
|
+
// No file: the route never ran - a site with no pages to give a card.
|
|
46
|
+
if (!fs.existsSync(listFile)) return 0;
|
|
47
|
+
const { cards, ...shared } = JSON.parse(fs.readFileSync(listFile, 'utf8'));
|
|
48
|
+
if (!cards.length) return 0;
|
|
49
|
+
let next = 0;
|
|
50
|
+
const workers = [];
|
|
51
|
+
try {
|
|
52
|
+
await Promise.all(
|
|
53
|
+
Array.from({ length: Math.min(threads, cards.length) }, () => {
|
|
54
|
+
const worker = new Worker(new URL('./og-image-worker.js', import.meta.url), { workerData: { shared, siteDir } });
|
|
55
|
+
workers.push(worker);
|
|
56
|
+
return new Promise((resolve, reject) => {
|
|
57
|
+
// A card at a time, to whichever thread is free: a long title or a
|
|
58
|
+
// logo makes one slower than another.
|
|
59
|
+
worker.on('message', () => worker.postMessage(next < cards.length ? cards[next++] : null));
|
|
60
|
+
worker.on('error', reject);
|
|
61
|
+
worker.on('exit', (code) => (code === 0 ? resolve() : reject(new Error(`A thread drawing the link previews stopped (exit code ${code}).`))));
|
|
62
|
+
});
|
|
63
|
+
})
|
|
64
|
+
);
|
|
65
|
+
} finally {
|
|
66
|
+
// One thread failing stops the others.
|
|
67
|
+
await Promise.all(workers.map((worker) => worker.terminate()));
|
|
68
|
+
}
|
|
69
|
+
return cards.length;
|
|
70
|
+
}
|