@writedocs/generator 0.13.3 → 0.13.5
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/README.md +28 -28
- package/astro.config.mjs +645 -645
- package/bin/writedocs.js +43 -12
- package/package.json +99 -99
- package/src/cli/base-path-layout.js +44 -44
- package/src/cli/build-auth.js +135 -135
- package/src/cli/build.js +203 -203
- package/src/cli/check-built-styles.js +41 -41
- package/src/cli/convert.js +1 -1
- package/src/cli/dev.js +268 -268
- package/src/cli/exit-when-flushed.js +4 -0
- package/src/cli/generate-api-pages.js +507 -507
- package/src/cli/og-image-worker.js +20 -20
- package/src/cli/og-images.js +70 -70
- package/src/cli/page-list-watch.js +69 -69
- package/src/cli/run-astro.js +97 -97
- package/src/cli/run-pagefind.js +154 -154
- package/src/cli/write-mcp-files.js +103 -103
- package/src/cli/write-redirects-file.js +158 -158
- package/src/components/ApiReferencePanel.astro +1792 -1792
- package/src/components/AppIcon.astro +51 -51
- package/src/components/Card.astro +193 -193
- package/src/components/CodeGroup.astro +63 -39
- package/src/components/CopyPageMenu.astro +374 -374
- package/src/components/Hint.astro +188 -188
- package/src/components/Icon.astro +72 -72
- package/src/components/Image.astro +165 -165
- package/src/components/Tile.astro +67 -67
- package/src/components/Video.astro +137 -137
- package/src/content.config.ts +203 -203
- package/src/layout/BaseLayout.astro +908 -908
- package/src/layout/components/AnalyticsScripts.astro +49 -49
- package/src/layout/components/Breadcrumbs.astro +98 -98
- package/src/layout/components/ConsentBanner.astro +119 -119
- package/src/layout/components/MobileMenu.astro +225 -225
- package/src/layout/components/NavTree.astro +408 -408
- package/src/layout/components/Sidebar.astro +232 -232
- package/src/layout/components/SiteFooter.astro +87 -87
- package/src/layout/components/TopBar.astro +386 -386
- package/src/layout/styles/base.css +332 -326
- package/src/layout/styles/mobile-menu.css +285 -285
- package/src/lib/agent-markdown.js +164 -164
- package/src/lib/asset-dimensions.js +89 -89
- package/src/lib/base-path.js +90 -90
- package/src/lib/cli-report.js +22 -22
- package/src/lib/code-line-numbers.js +13 -0
- package/src/lib/config-schema.js +414 -75
- package/src/lib/config-schema.ts +2509 -1905
- package/src/lib/config.ts +1316 -1314
- package/src/lib/consent.js +108 -108
- package/src/lib/content-store-reload-integration.js +101 -101
- package/src/lib/icon-data-dev.js +53 -53
- package/src/lib/image-size.js +152 -152
- package/src/lib/json-schema-descriptions.js +228 -228
- package/src/lib/link-check.js +544 -544
- package/src/lib/llms-index.ts +90 -90
- package/src/lib/mcp-dev-integration.js +65 -65
- package/src/lib/mcp-index.ts +102 -102
- package/src/lib/mintlify-convert.js +605 -605
- package/src/lib/moved-api-pages.js +49 -49
- package/src/lib/og-cards.js +33 -33
- package/src/lib/og-image.js +235 -235
- package/src/lib/openapi-dev-status.js +80 -80
- package/src/lib/openapi-reload-integration.js +193 -193
- package/src/lib/pages.js +312 -312
- package/src/lib/project-files-integration.js +116 -116
- package/src/lib/project-files.js +137 -137
- package/src/lib/rehype-base-path.js +62 -62
- package/src/lib/rehype-image-loading.js +71 -71
- package/src/lib/robots-txt.js +21 -21
- package/src/lib/search-scope.js +44 -44
- package/src/lib/selector-placement.js +50 -50
- package/src/lib/seo-tags.js +155 -155
- package/src/lib/shiki-code-block.js +7 -1
- package/src/lib/site-base.ts +27 -27
- package/src/lib/styles-asset-integration.js +291 -291
- package/src/lib/wdignore-cases.json +195 -195
- package/src/lib/wdignore.js +177 -177
- package/src/pages/404.astro +63 -63
- package/src/pages/[...slug].astro +1762 -1608
- package/src/pages/[...slug].md.ts +93 -93
- package/src/pages/llms-full.txt.ts +88 -88
- package/src/pages/og/[...slug].png.ts +63 -63
- package/src/scripts/mobile-menu.ts +80 -80
- package/src/scripts/search.ts +245 -245
- package/src/scripts/sidebar-scroll.ts +61 -61
- package/src/styles/global.css +40 -40
package/src/cli/dev.js
CHANGED
|
@@ -1,268 +1,268 @@
|
|
|
1
|
-
import { canonicalPath } from '../lib/canonical-path.js';
|
|
2
|
-
import { runAstro } from './run-astro.js';
|
|
3
|
-
import { preflightCheck, sameDriveCheck, writableInstallCheck } from './preflight.js';
|
|
4
|
-
import { generateApiPages } from './generate-api-pages.js';
|
|
5
|
-
import { log, step, duration, formatProblems, color, CliExit } from './output.js';
|
|
6
|
-
import { describeError, requestLog, authorWarning, verboseLine, stripAnsi } from './astro-output.js';
|
|
7
|
-
import { reportApiPages } from './api-pages-output.js';
|
|
8
|
-
import { runningPreview, writeLock, removeLock } from './dev-lock.js';
|
|
9
|
-
import { showUpdateNotice } from './update-check.js';
|
|
10
|
-
import { shouldOpenBrowser, openBrowser } from './open-browser.js';
|
|
11
|
-
import { watchPageList } from './page-list-watch.js';
|
|
12
|
-
import { readConfigText } from '../lib/config-file.js';
|
|
13
|
-
import { normalizeBasePath } from '../lib/base-path.js';
|
|
14
|
-
import { writeDevOpenApiState } from '../lib/openapi-dev-status.js';
|
|
15
|
-
|
|
16
|
-
/** The preview's address for the server's own (`http://localhost:4321/`):
|
|
17
|
-
* under writedocs.json's `basePath` when it has one - the site is served
|
|
18
|
-
* at /docs/ then, and / is Astro's "not here" page. Read when the server
|
|
19
|
-
* is ready, so it follows the file. */
|
|
20
|
-
export function previewAddress(serverUrl, configText) {
|
|
21
|
-
let basePath = '';
|
|
22
|
-
try {
|
|
23
|
-
basePath = normalizeBasePath(JSON.parse(configText).basePath);
|
|
24
|
-
} catch {
|
|
25
|
-
// An unreadable writedocs.json stops the preview elsewhere.
|
|
26
|
-
}
|
|
27
|
-
return basePath ? new URL(`${basePath.slice(1)}/`, serverUrl).href : serverUrl;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// The same problem tends to arrive more than once in a row - Vite and
|
|
31
|
-
// Astro each log a failed page, and a page compiles for more than one
|
|
32
|
-
// environment. Within this window, a repeat is dropped.
|
|
33
|
-
const REPEAT_WINDOW_MS = 2000;
|
|
34
|
-
|
|
35
|
-
export async function runDev({ contentDir, packageRoot, port, verbose = false, open = true }) {
|
|
36
|
-
// One spelling of each folder throughout - see lib/canonical-path.js.
|
|
37
|
-
contentDir = canonicalPath(contentDir);
|
|
38
|
-
packageRoot = canonicalPath(packageRoot);
|
|
39
|
-
const started = Date.now();
|
|
40
|
-
preflightCheck(contentDir);
|
|
41
|
-
writableInstallCheck(packageRoot);
|
|
42
|
-
const other = runningPreview(packageRoot);
|
|
43
|
-
if (other) {
|
|
44
|
-
log.error(`Another writedocs preview is already running${other.url ? ` at ${other.url}` : ''}.`);
|
|
45
|
-
log.detail(
|
|
46
|
-
color.dim(
|
|
47
|
-
`It's serving ${other.contentDir}. Stop it first (Ctrl+C in its terminal) - two previews at once overwrite each other's pages.`
|
|
48
|
-
)
|
|
49
|
-
);
|
|
50
|
-
throw new CliExit(1);
|
|
51
|
-
}
|
|
52
|
-
writeLock(packageRoot, { contentDir, url: null });
|
|
53
|
-
process.on('exit', () => removeLock(packageRoot));
|
|
54
|
-
|
|
55
|
-
const api = await generateApiPages({ contentDir });
|
|
56
|
-
sameDriveCheck(contentDir, packageRoot, { generatedPages: api.groups.length > 0 });
|
|
57
|
-
reportApiPages(api);
|
|
58
|
-
api.warnings.forEach((message) => log.warn(message));
|
|
59
|
-
// The spec files to watch from here on - lib/openapi-reload-integration.js.
|
|
60
|
-
writeDevOpenApiState(contentDir, { sources: api.sources, problem: null });
|
|
61
|
-
|
|
62
|
-
let starting = step('Starting local preview');
|
|
63
|
-
let ready = false;
|
|
64
|
-
let browserOpened = false;
|
|
65
|
-
// Restarting after pages were added or removed (see the watcher below):
|
|
66
|
-
// when that restart began, and the port to come back on.
|
|
67
|
-
let restartStarted = null;
|
|
68
|
-
let currentPort = port;
|
|
69
|
-
|
|
70
|
-
const lastShown = new Map();
|
|
71
|
-
const once = (key, print) => {
|
|
72
|
-
const now = Date.now();
|
|
73
|
-
if (now - (lastShown.get(key) ?? 0) < REPEAT_WINDOW_MS) return;
|
|
74
|
-
lastShown.set(key, now);
|
|
75
|
-
print();
|
|
76
|
-
};
|
|
77
|
-
const oneLine = (where, message) => (where ? `${color.bold(where)} ${message}` : message);
|
|
78
|
-
|
|
79
|
-
// A failed page is logged twice - Vite's error (message only) and then
|
|
80
|
-
// Astro's (message plus the file). Errors wait a moment so the two can
|
|
81
|
-
// be merged into one line that names the file.
|
|
82
|
-
// When the same page fails again, only the first report of it named the
|
|
83
|
-
// file: later ones point at the MDX compiler instead. The file each
|
|
84
|
-
// error was last seen in is remembered here, by the error itself.
|
|
85
|
-
const pendingErrors = new Map();
|
|
86
|
-
const knownWhere = new Map();
|
|
87
|
-
const showError = (problem) => {
|
|
88
|
-
if (problem.where) knownWhere.set(problem.key, problem.where);
|
|
89
|
-
else if (knownWhere.has(problem.key)) problem = { ...problem, where: knownWhere.get(problem.key) };
|
|
90
|
-
const existing = pendingErrors.get(problem.message);
|
|
91
|
-
if (existing) {
|
|
92
|
-
if (!existing.problem.where && problem.where) existing.problem = problem;
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
const entry = { problem };
|
|
96
|
-
entry.timer = setTimeout(() => {
|
|
97
|
-
pendingErrors.delete(problem.message);
|
|
98
|
-
const { where, message, hint } = entry.problem;
|
|
99
|
-
// Keyed by the line as shown: an OpenAPI spec that doesn't parse is
|
|
100
|
-
// reported once with its place apart (lib/openapi-reload-integration.js)
|
|
101
|
-
// and once in the message of the page that throws it.
|
|
102
|
-
once(`error\n${where ? `${where} ` : ''}${message}`, () => {
|
|
103
|
-
log.error(oneLine(where, message));
|
|
104
|
-
if (hint) log.detail(color.dim(`Hint: ${hint}`));
|
|
105
|
-
});
|
|
106
|
-
}, 150);
|
|
107
|
-
pendingErrors.set(problem.message, entry);
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
let fatal = null;
|
|
111
|
-
const rawTail = [];
|
|
112
|
-
const onEvent = (event) => {
|
|
113
|
-
if (verbose) {
|
|
114
|
-
const line = verboseLine(event);
|
|
115
|
-
if (line !== null) log.line(color.dim(line));
|
|
116
|
-
}
|
|
117
|
-
switch (event.type) {
|
|
118
|
-
case 'ready': {
|
|
119
|
-
ready = true;
|
|
120
|
-
const [local] = event.urls?.local ?? [];
|
|
121
|
-
const wanted = Number(port ?? 4321);
|
|
122
|
-
const actual = local ? Number(new URL(local).port) : wanted;
|
|
123
|
-
const address = previewAddress(local ?? `http://localhost:${wanted}/`, readConfigText(contentDir));
|
|
124
|
-
writeLock(packageRoot, { contentDir, url: address });
|
|
125
|
-
currentPort = actual;
|
|
126
|
-
// Pages changed while it was starting: restart now that it's up.
|
|
127
|
-
if (restartPending) {
|
|
128
|
-
const change = restartPending;
|
|
129
|
-
restartPending = false;
|
|
130
|
-
setTimeout(() => restartForPages(change), 0);
|
|
131
|
-
}
|
|
132
|
-
// Back after a restart: one line - the open tab reloads by itself.
|
|
133
|
-
if (restartStarted !== null) {
|
|
134
|
-
starting.succeed(`Preview updated ${color.dim(`in ${duration(Date.now() - restartStarted)}`)}`);
|
|
135
|
-
restartStarted = null;
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
starting.succeed(`Preview ready ${color.dim(`in ${duration(Date.now() - started)}`)}`);
|
|
139
|
-
log.line();
|
|
140
|
-
log.line(` ${color.bold('Local:')} ${color.cyan(address)}`);
|
|
141
|
-
if (actual !== wanted) log.line(color.dim(` Port ${wanted} was in use, so the preview is on ${actual}.`));
|
|
142
|
-
log.line();
|
|
143
|
-
log.line(color.dim(' Edit any page and the preview updates. Press Ctrl+C to stop.'));
|
|
144
|
-
log.line();
|
|
145
|
-
// `dev` runs until Ctrl+C - the notice goes under the ready screen,
|
|
146
|
-
// not after the command like everywhere else.
|
|
147
|
-
showUpdateNotice();
|
|
148
|
-
// The preview in the browser, at the address it actually got - once,
|
|
149
|
-
// not again if Astro restarts the server.
|
|
150
|
-
if (!browserOpened && shouldOpenBrowser({ open })) {
|
|
151
|
-
browserOpened = true;
|
|
152
|
-
openBrowser(address);
|
|
153
|
-
}
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
case 'fatal':
|
|
157
|
-
fatal = event.error;
|
|
158
|
-
return;
|
|
159
|
-
case 'report':
|
|
160
|
-
// 'info' notes are about the build (sitemap and the like), not
|
|
161
|
-
// the preview. An 'error' is an OpenAPI spec that stopped parsing.
|
|
162
|
-
if (event.level === 'warn') once(`warn\n${event.where}\n${event.message}`, () => log.warn(oneLine(event.where, event.message)));
|
|
163
|
-
if (event.level === 'error') showError({ where: event.where, message: event.message, key: `${event.message}|${event.where}` });
|
|
164
|
-
return;
|
|
165
|
-
case 'raw':
|
|
166
|
-
rawTail.push(event.line);
|
|
167
|
-
if (rawTail.length > 40) rawTail.shift();
|
|
168
|
-
return;
|
|
169
|
-
case 'astro-log': {
|
|
170
|
-
// A page file just deleted: Astro's list of page modules
|
|
171
|
-
// (.astro/content-modules.mjs) still points at it for a moment -
|
|
172
|
-
// "Failed to load url ... Does the file exist?" - until the restart
|
|
173
|
-
// the page-list watcher starts brings in a fresh list. Noise, not a
|
|
174
|
-
// problem in the site. Every other error still shows, and a
|
|
175
|
-
// preview that can't start still reports through 'fatal'.
|
|
176
|
-
if (event.level === 'error' && /content-modules\.mjs/.test(event.message) && /Does the file exist/.test(event.message)) return;
|
|
177
|
-
if (restartStarted !== null && event.level === 'error') return;
|
|
178
|
-
if (event.level === 'error') {
|
|
179
|
-
showError(describeError(stripAnsi(event.message), contentDir, { root: packageRoot }));
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
const request = requestLog(event);
|
|
183
|
-
// A missing page is worth knowing about (a broken link, a typo in
|
|
184
|
-
// the navigation); a missing favicon or source map isn't.
|
|
185
|
-
if (request?.status === 404 && !/\.[a-z0-9]+$/i.test(request.url.split('?')[0])) {
|
|
186
|
-
once(`404\n${request.url}`, () => log.warn(`${color.bold(request.url)} No page at this address (404).`));
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
const warning = authorWarning(event, contentDir, { root: packageRoot });
|
|
190
|
-
if (warning) once(`warn\n${warning}`, () => log.warn(warning));
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
// Pages added or removed while the preview runs: its page list is read
|
|
197
|
-
// when it starts, so it restarts - on the same port; the open tab
|
|
198
|
-
// reconnects and reloads by itself. Edits to pages and to writedocs.json
|
|
199
|
-
// don't need this (lib/config-reload-integration.js).
|
|
200
|
-
let child = null;
|
|
201
|
-
let restarting = false;
|
|
202
|
-
let restartPending = false;
|
|
203
|
-
const restartForPages = ({ added, removed }) => {
|
|
204
|
-
const what = [
|
|
205
|
-
...added.slice(0, 3).map((file) => `${file} added`),
|
|
206
|
-
...removed.slice(0, 3).map((file) => `${file} removed`),
|
|
207
|
-
];
|
|
208
|
-
const more = added.length + removed.length - what.length;
|
|
209
|
-
log.info(`${what.join(', ')}${more > 0 ? ` and ${more} more` : ''} - restarting the preview so the page list is current.`);
|
|
210
|
-
restarting = true;
|
|
211
|
-
ready = false;
|
|
212
|
-
restartStarted = Date.now();
|
|
213
|
-
starting = step('Restarting the preview');
|
|
214
|
-
child?.kill();
|
|
215
|
-
};
|
|
216
|
-
const pageWatch = watchPageList(contentDir, (change) => {
|
|
217
|
-
if (stopping || restarting) return;
|
|
218
|
-
// Still starting: restart once it's up, not halfway.
|
|
219
|
-
if (!ready) {
|
|
220
|
-
restartPending = change;
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
restartForPages(change);
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
// Ctrl+C: stop the server quietly. (The child gets the same signal from
|
|
227
|
-
// the terminal; killing it here covers the cases where it doesn't.) A
|
|
228
|
-
// second Ctrl+C doesn't wait for it.
|
|
229
|
-
let stopping = false;
|
|
230
|
-
const stop = () => {
|
|
231
|
-
if (stopping) process.exit(130);
|
|
232
|
-
stopping = true;
|
|
233
|
-
pageWatch.close();
|
|
234
|
-
child?.kill();
|
|
235
|
-
};
|
|
236
|
-
process.on('SIGINT', stop);
|
|
237
|
-
process.on('SIGTERM', stop);
|
|
238
|
-
|
|
239
|
-
let code;
|
|
240
|
-
for (;;) {
|
|
241
|
-
fatal = null;
|
|
242
|
-
rawTail.length = 0;
|
|
243
|
-
const run = runAstro('dev', { packageRoot, contentDir, port: currentPort, onEvent });
|
|
244
|
-
child = run.child;
|
|
245
|
-
code = await run.exited;
|
|
246
|
-
if (stopping || !restarting) break;
|
|
247
|
-
restarting = false;
|
|
248
|
-
}
|
|
249
|
-
pageWatch.close();
|
|
250
|
-
if (stopping) {
|
|
251
|
-
if (ready) log.line(color.dim('Preview stopped.'));
|
|
252
|
-
return;
|
|
253
|
-
}
|
|
254
|
-
// The server never stops on its own - getting here is a failure.
|
|
255
|
-
if (!ready) starting.fail('Could not start the local preview');
|
|
256
|
-
else log.error('The local preview stopped unexpectedly.');
|
|
257
|
-
if (fatal) {
|
|
258
|
-
const problem = describeError(fatal, contentDir, { root: packageRoot });
|
|
259
|
-
log.line();
|
|
260
|
-
log.line(formatProblems([{ where: problem.where, message: problem.message }]));
|
|
261
|
-
if (problem.hint) log.line(`\n${color.dim(` Hint: ${problem.hint}`)}`);
|
|
262
|
-
if (verbose && fatal.stack) log.line(`\n${color.dim(fatal.stack)}`);
|
|
263
|
-
} else if (rawTail.length) {
|
|
264
|
-
log.line();
|
|
265
|
-
log.line(color.dim(rawTail.join('\n')));
|
|
266
|
-
}
|
|
267
|
-
throw new CliExit(code || 1);
|
|
268
|
-
}
|
|
1
|
+
import { canonicalPath } from '../lib/canonical-path.js';
|
|
2
|
+
import { runAstro } from './run-astro.js';
|
|
3
|
+
import { preflightCheck, sameDriveCheck, writableInstallCheck } from './preflight.js';
|
|
4
|
+
import { generateApiPages } from './generate-api-pages.js';
|
|
5
|
+
import { log, step, duration, formatProblems, color, CliExit } from './output.js';
|
|
6
|
+
import { describeError, requestLog, authorWarning, verboseLine, stripAnsi } from './astro-output.js';
|
|
7
|
+
import { reportApiPages } from './api-pages-output.js';
|
|
8
|
+
import { runningPreview, writeLock, removeLock } from './dev-lock.js';
|
|
9
|
+
import { showUpdateNotice } from './update-check.js';
|
|
10
|
+
import { shouldOpenBrowser, openBrowser } from './open-browser.js';
|
|
11
|
+
import { watchPageList } from './page-list-watch.js';
|
|
12
|
+
import { readConfigText } from '../lib/config-file.js';
|
|
13
|
+
import { normalizeBasePath } from '../lib/base-path.js';
|
|
14
|
+
import { writeDevOpenApiState } from '../lib/openapi-dev-status.js';
|
|
15
|
+
|
|
16
|
+
/** The preview's address for the server's own (`http://localhost:4321/`):
|
|
17
|
+
* under writedocs.json's `basePath` when it has one - the site is served
|
|
18
|
+
* at /docs/ then, and / is Astro's "not here" page. Read when the server
|
|
19
|
+
* is ready, so it follows the file. */
|
|
20
|
+
export function previewAddress(serverUrl, configText) {
|
|
21
|
+
let basePath = '';
|
|
22
|
+
try {
|
|
23
|
+
basePath = normalizeBasePath(JSON.parse(configText).basePath);
|
|
24
|
+
} catch {
|
|
25
|
+
// An unreadable writedocs.json stops the preview elsewhere.
|
|
26
|
+
}
|
|
27
|
+
return basePath ? new URL(`${basePath.slice(1)}/`, serverUrl).href : serverUrl;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// The same problem tends to arrive more than once in a row - Vite and
|
|
31
|
+
// Astro each log a failed page, and a page compiles for more than one
|
|
32
|
+
// environment. Within this window, a repeat is dropped.
|
|
33
|
+
const REPEAT_WINDOW_MS = 2000;
|
|
34
|
+
|
|
35
|
+
export async function runDev({ contentDir, packageRoot, port, verbose = false, open = true }) {
|
|
36
|
+
// One spelling of each folder throughout - see lib/canonical-path.js.
|
|
37
|
+
contentDir = canonicalPath(contentDir);
|
|
38
|
+
packageRoot = canonicalPath(packageRoot);
|
|
39
|
+
const started = Date.now();
|
|
40
|
+
preflightCheck(contentDir);
|
|
41
|
+
writableInstallCheck(packageRoot);
|
|
42
|
+
const other = runningPreview(packageRoot);
|
|
43
|
+
if (other) {
|
|
44
|
+
log.error(`Another writedocs preview is already running${other.url ? ` at ${other.url}` : ''}.`);
|
|
45
|
+
log.detail(
|
|
46
|
+
color.dim(
|
|
47
|
+
`It's serving ${other.contentDir}. Stop it first (Ctrl+C in its terminal) - two previews at once overwrite each other's pages.`
|
|
48
|
+
)
|
|
49
|
+
);
|
|
50
|
+
throw new CliExit(1);
|
|
51
|
+
}
|
|
52
|
+
writeLock(packageRoot, { contentDir, url: null });
|
|
53
|
+
process.on('exit', () => removeLock(packageRoot));
|
|
54
|
+
|
|
55
|
+
const api = await generateApiPages({ contentDir });
|
|
56
|
+
sameDriveCheck(contentDir, packageRoot, { generatedPages: api.groups.length > 0 });
|
|
57
|
+
reportApiPages(api);
|
|
58
|
+
api.warnings.forEach((message) => log.warn(message));
|
|
59
|
+
// The spec files to watch from here on - lib/openapi-reload-integration.js.
|
|
60
|
+
writeDevOpenApiState(contentDir, { sources: api.sources, problem: null });
|
|
61
|
+
|
|
62
|
+
let starting = step('Starting local preview');
|
|
63
|
+
let ready = false;
|
|
64
|
+
let browserOpened = false;
|
|
65
|
+
// Restarting after pages were added or removed (see the watcher below):
|
|
66
|
+
// when that restart began, and the port to come back on.
|
|
67
|
+
let restartStarted = null;
|
|
68
|
+
let currentPort = port;
|
|
69
|
+
|
|
70
|
+
const lastShown = new Map();
|
|
71
|
+
const once = (key, print) => {
|
|
72
|
+
const now = Date.now();
|
|
73
|
+
if (now - (lastShown.get(key) ?? 0) < REPEAT_WINDOW_MS) return;
|
|
74
|
+
lastShown.set(key, now);
|
|
75
|
+
print();
|
|
76
|
+
};
|
|
77
|
+
const oneLine = (where, message) => (where ? `${color.bold(where)} ${message}` : message);
|
|
78
|
+
|
|
79
|
+
// A failed page is logged twice - Vite's error (message only) and then
|
|
80
|
+
// Astro's (message plus the file). Errors wait a moment so the two can
|
|
81
|
+
// be merged into one line that names the file.
|
|
82
|
+
// When the same page fails again, only the first report of it named the
|
|
83
|
+
// file: later ones point at the MDX compiler instead. The file each
|
|
84
|
+
// error was last seen in is remembered here, by the error itself.
|
|
85
|
+
const pendingErrors = new Map();
|
|
86
|
+
const knownWhere = new Map();
|
|
87
|
+
const showError = (problem) => {
|
|
88
|
+
if (problem.where) knownWhere.set(problem.key, problem.where);
|
|
89
|
+
else if (knownWhere.has(problem.key)) problem = { ...problem, where: knownWhere.get(problem.key) };
|
|
90
|
+
const existing = pendingErrors.get(problem.message);
|
|
91
|
+
if (existing) {
|
|
92
|
+
if (!existing.problem.where && problem.where) existing.problem = problem;
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const entry = { problem };
|
|
96
|
+
entry.timer = setTimeout(() => {
|
|
97
|
+
pendingErrors.delete(problem.message);
|
|
98
|
+
const { where, message, hint } = entry.problem;
|
|
99
|
+
// Keyed by the line as shown: an OpenAPI spec that doesn't parse is
|
|
100
|
+
// reported once with its place apart (lib/openapi-reload-integration.js)
|
|
101
|
+
// and once in the message of the page that throws it.
|
|
102
|
+
once(`error\n${where ? `${where} ` : ''}${message}`, () => {
|
|
103
|
+
log.error(oneLine(where, message));
|
|
104
|
+
if (hint) log.detail(color.dim(`Hint: ${hint}`));
|
|
105
|
+
});
|
|
106
|
+
}, 150);
|
|
107
|
+
pendingErrors.set(problem.message, entry);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
let fatal = null;
|
|
111
|
+
const rawTail = [];
|
|
112
|
+
const onEvent = (event) => {
|
|
113
|
+
if (verbose) {
|
|
114
|
+
const line = verboseLine(event);
|
|
115
|
+
if (line !== null) log.line(color.dim(line));
|
|
116
|
+
}
|
|
117
|
+
switch (event.type) {
|
|
118
|
+
case 'ready': {
|
|
119
|
+
ready = true;
|
|
120
|
+
const [local] = event.urls?.local ?? [];
|
|
121
|
+
const wanted = Number(port ?? 4321);
|
|
122
|
+
const actual = local ? Number(new URL(local).port) : wanted;
|
|
123
|
+
const address = previewAddress(local ?? `http://localhost:${wanted}/`, readConfigText(contentDir));
|
|
124
|
+
writeLock(packageRoot, { contentDir, url: address });
|
|
125
|
+
currentPort = actual;
|
|
126
|
+
// Pages changed while it was starting: restart now that it's up.
|
|
127
|
+
if (restartPending) {
|
|
128
|
+
const change = restartPending;
|
|
129
|
+
restartPending = false;
|
|
130
|
+
setTimeout(() => restartForPages(change), 0);
|
|
131
|
+
}
|
|
132
|
+
// Back after a restart: one line - the open tab reloads by itself.
|
|
133
|
+
if (restartStarted !== null) {
|
|
134
|
+
starting.succeed(`Preview updated ${color.dim(`in ${duration(Date.now() - restartStarted)}`)}`);
|
|
135
|
+
restartStarted = null;
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
starting.succeed(`Preview ready ${color.dim(`in ${duration(Date.now() - started)}`)}`);
|
|
139
|
+
log.line();
|
|
140
|
+
log.line(` ${color.bold('Local:')} ${color.cyan(address)}`);
|
|
141
|
+
if (actual !== wanted) log.line(color.dim(` Port ${wanted} was in use, so the preview is on ${actual}.`));
|
|
142
|
+
log.line();
|
|
143
|
+
log.line(color.dim(' Edit any page and the preview updates. Press Ctrl+C to stop.'));
|
|
144
|
+
log.line();
|
|
145
|
+
// `dev` runs until Ctrl+C - the notice goes under the ready screen,
|
|
146
|
+
// not after the command like everywhere else.
|
|
147
|
+
showUpdateNotice();
|
|
148
|
+
// The preview in the browser, at the address it actually got - once,
|
|
149
|
+
// not again if Astro restarts the server.
|
|
150
|
+
if (!browserOpened && shouldOpenBrowser({ open })) {
|
|
151
|
+
browserOpened = true;
|
|
152
|
+
openBrowser(address);
|
|
153
|
+
}
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
case 'fatal':
|
|
157
|
+
fatal = event.error;
|
|
158
|
+
return;
|
|
159
|
+
case 'report':
|
|
160
|
+
// 'info' notes are about the build (sitemap and the like), not
|
|
161
|
+
// the preview. An 'error' is an OpenAPI spec that stopped parsing.
|
|
162
|
+
if (event.level === 'warn') once(`warn\n${event.where}\n${event.message}`, () => log.warn(oneLine(event.where, event.message)));
|
|
163
|
+
if (event.level === 'error') showError({ where: event.where, message: event.message, key: `${event.message}|${event.where}` });
|
|
164
|
+
return;
|
|
165
|
+
case 'raw':
|
|
166
|
+
rawTail.push(event.line);
|
|
167
|
+
if (rawTail.length > 40) rawTail.shift();
|
|
168
|
+
return;
|
|
169
|
+
case 'astro-log': {
|
|
170
|
+
// A page file just deleted: Astro's list of page modules
|
|
171
|
+
// (.astro/content-modules.mjs) still points at it for a moment -
|
|
172
|
+
// "Failed to load url ... Does the file exist?" - until the restart
|
|
173
|
+
// the page-list watcher starts brings in a fresh list. Noise, not a
|
|
174
|
+
// problem in the site. Every other error still shows, and a
|
|
175
|
+
// preview that can't start still reports through 'fatal'.
|
|
176
|
+
if (event.level === 'error' && /content-modules\.mjs/.test(event.message) && /Does the file exist/.test(event.message)) return;
|
|
177
|
+
if (restartStarted !== null && event.level === 'error') return;
|
|
178
|
+
if (event.level === 'error') {
|
|
179
|
+
showError(describeError(stripAnsi(event.message), contentDir, { root: packageRoot }));
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const request = requestLog(event);
|
|
183
|
+
// A missing page is worth knowing about (a broken link, a typo in
|
|
184
|
+
// the navigation); a missing favicon or source map isn't.
|
|
185
|
+
if (request?.status === 404 && !/\.[a-z0-9]+$/i.test(request.url.split('?')[0])) {
|
|
186
|
+
once(`404\n${request.url}`, () => log.warn(`${color.bold(request.url)} No page at this address (404).`));
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
const warning = authorWarning(event, contentDir, { root: packageRoot });
|
|
190
|
+
if (warning) once(`warn\n${warning}`, () => log.warn(warning));
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
// Pages added or removed while the preview runs: its page list is read
|
|
197
|
+
// when it starts, so it restarts - on the same port; the open tab
|
|
198
|
+
// reconnects and reloads by itself. Edits to pages and to writedocs.json
|
|
199
|
+
// don't need this (lib/config-reload-integration.js).
|
|
200
|
+
let child = null;
|
|
201
|
+
let restarting = false;
|
|
202
|
+
let restartPending = false;
|
|
203
|
+
const restartForPages = ({ added, removed }) => {
|
|
204
|
+
const what = [
|
|
205
|
+
...added.slice(0, 3).map((file) => `${file} added`),
|
|
206
|
+
...removed.slice(0, 3).map((file) => `${file} removed`),
|
|
207
|
+
];
|
|
208
|
+
const more = added.length + removed.length - what.length;
|
|
209
|
+
log.info(`${what.join(', ')}${more > 0 ? ` and ${more} more` : ''} - restarting the preview so the page list is current.`);
|
|
210
|
+
restarting = true;
|
|
211
|
+
ready = false;
|
|
212
|
+
restartStarted = Date.now();
|
|
213
|
+
starting = step('Restarting the preview');
|
|
214
|
+
child?.kill();
|
|
215
|
+
};
|
|
216
|
+
const pageWatch = watchPageList(contentDir, (change) => {
|
|
217
|
+
if (stopping || restarting) return;
|
|
218
|
+
// Still starting: restart once it's up, not halfway.
|
|
219
|
+
if (!ready) {
|
|
220
|
+
restartPending = change;
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
restartForPages(change);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
// Ctrl+C: stop the server quietly. (The child gets the same signal from
|
|
227
|
+
// the terminal; killing it here covers the cases where it doesn't.) A
|
|
228
|
+
// second Ctrl+C doesn't wait for it.
|
|
229
|
+
let stopping = false;
|
|
230
|
+
const stop = () => {
|
|
231
|
+
if (stopping) process.exit(130);
|
|
232
|
+
stopping = true;
|
|
233
|
+
pageWatch.close();
|
|
234
|
+
child?.kill();
|
|
235
|
+
};
|
|
236
|
+
process.on('SIGINT', stop);
|
|
237
|
+
process.on('SIGTERM', stop);
|
|
238
|
+
|
|
239
|
+
let code;
|
|
240
|
+
for (;;) {
|
|
241
|
+
fatal = null;
|
|
242
|
+
rawTail.length = 0;
|
|
243
|
+
const run = runAstro('dev', { packageRoot, contentDir, port: currentPort, onEvent });
|
|
244
|
+
child = run.child;
|
|
245
|
+
code = await run.exited;
|
|
246
|
+
if (stopping || !restarting) break;
|
|
247
|
+
restarting = false;
|
|
248
|
+
}
|
|
249
|
+
pageWatch.close();
|
|
250
|
+
if (stopping) {
|
|
251
|
+
if (ready) log.line(color.dim('Preview stopped.'));
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
// The server never stops on its own - getting here is a failure.
|
|
255
|
+
if (!ready) starting.fail('Could not start the local preview');
|
|
256
|
+
else log.error('The local preview stopped unexpectedly.');
|
|
257
|
+
if (fatal) {
|
|
258
|
+
const problem = describeError(fatal, contentDir, { root: packageRoot });
|
|
259
|
+
log.line();
|
|
260
|
+
log.line(formatProblems([{ where: problem.where, message: problem.message }]));
|
|
261
|
+
if (problem.hint) log.line(`\n${color.dim(` Hint: ${problem.hint}`)}`);
|
|
262
|
+
if (verbose && fatal.stack) log.line(`\n${color.dim(fatal.stack)}`);
|
|
263
|
+
} else if (rawTail.length) {
|
|
264
|
+
log.line();
|
|
265
|
+
log.line(color.dim(rawTail.join('\n')));
|
|
266
|
+
}
|
|
267
|
+
throw new CliExit(code || 1);
|
|
268
|
+
}
|
|
@@ -6,6 +6,10 @@
|
|
|
6
6
|
// machine still had most of it queued after 50 ms, and the CLI got the output
|
|
7
7
|
// cut short with the build exiting 0.
|
|
8
8
|
//
|
|
9
|
+
// bin/writedocs.js leaves a failed command the same way (no IPC message
|
|
10
|
+
// there): its process.exit() lost the end of `writedocs validate`'s output in
|
|
11
|
+
// a CI log.
|
|
12
|
+
//
|
|
9
13
|
// Plain JavaScript with no Astro import, so scripts/exit-when-flushed.test.js
|
|
10
14
|
// can run it in a child process of its own.
|
|
11
15
|
|