@writedocs/generator 0.12.0 → 0.13.1
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/astro.config.mjs +12 -1
- package/package.json +1 -1
- package/src/cli/dev.js +268 -261
- package/src/cli/generate-api-pages.js +121 -36
- package/src/content.config.ts +203 -189
- package/src/lib/cli-report.js +22 -20
- package/src/lib/content-store-reload-integration.js +101 -0
- package/src/lib/icon-data-dev.js +53 -0
- package/src/lib/moved-api-pages.js +49 -54
- package/src/lib/openapi-dev-status.js +80 -0
- package/src/lib/openapi-reload-integration.js +193 -0
- package/src/pages/[...slug].astro +4 -0
- package/src/styles/global.css +40 -28
package/astro.config.mjs
CHANGED
|
@@ -38,6 +38,9 @@ import { writedocsTempDir, writedocsBuildStagingDir } from './src/lib/writedocs-
|
|
|
38
38
|
import { stylesAssetFallback } from './src/lib/styles-asset-integration.js';
|
|
39
39
|
import { mcpDevServer } from './src/lib/mcp-dev-integration.js';
|
|
40
40
|
import { configReload } from './src/lib/config-reload-integration.js';
|
|
41
|
+
import { openapiReload } from './src/lib/openapi-reload-integration.js';
|
|
42
|
+
import { contentStoreReload } from './src/lib/content-store-reload-integration.js';
|
|
43
|
+
import { iconDataFromFile } from './src/lib/icon-data-dev.js';
|
|
41
44
|
import { canonicalPath } from './src/lib/canonical-path.js';
|
|
42
45
|
import { withBase, withoutBase } from './src/lib/base-path.js';
|
|
43
46
|
import { folderAddresses } from './src/lib/folder-redirects.js';
|
|
@@ -379,6 +382,11 @@ export default defineConfig({
|
|
|
379
382
|
mcpDevServer({ version: JSON.parse(fs.readFileSync(path.join(packageRoot, 'package.json'), 'utf-8')).version, basePath }),
|
|
380
383
|
// writedocs dev: an edit to writedocs.json reloads the open tab.
|
|
381
384
|
configReload({ configPath: path.join(contentDir, 'writedocs.json') }),
|
|
385
|
+
// writedocs dev: an edit to an OpenAPI spec regenerates its pages.
|
|
386
|
+
openapiReload({ contentDir, packageRoot }),
|
|
387
|
+
// writedocs dev, installed: a change to a page's entry (its title, an
|
|
388
|
+
// API page renamed) reaches the open tab.
|
|
389
|
+
contentStoreReload({ packageRoot }),
|
|
382
390
|
],
|
|
383
391
|
output: 'static',
|
|
384
392
|
// Dual Shiki themes for fenced code blocks (```) in MDX content, so
|
|
@@ -569,7 +577,10 @@ export default defineConfig({
|
|
|
569
577
|
// Tailwind has to see the added @source when it compiles global.css.
|
|
570
578
|
// mintlifyReactHooksPlugin() does for a site's .jsx/.tsx snippets what
|
|
571
579
|
// remarkMintlifyReactHooks does for its MDX pages.
|
|
572
|
-
|
|
580
|
+
// iconDataFromFile() (writedocs dev only): astro-icon's ~15 MB of icon
|
|
581
|
+
// data read from a file rather than passed through Vite - about 5 s of
|
|
582
|
+
// every dev preview's first page (see src/lib/icon-data-dev.js).
|
|
583
|
+
plugins: [contentTailwindSource(), tailwindcss(), mintlifyReactHooksPlugin(contentDir), iconDataFromFile(writedocsTempDir(contentDir))],
|
|
573
584
|
// `writedocs dev`: Vite only prepares for the browser the dependencies
|
|
574
585
|
// it finds by scanning the project's own source - and when writedocs is
|
|
575
586
|
// installed, its source sits in node_modules, which the scan skips. The
|
package/package.json
CHANGED
package/src/cli/dev.js
CHANGED
|
@@ -1,261 +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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
//
|
|
31
|
-
//
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
log.
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
api
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
let
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
//
|
|
80
|
-
//
|
|
81
|
-
//
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (
|
|
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
|
-
if (
|
|
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
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
if (
|
|
179
|
-
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
if (
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
}
|
|
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
|
+
}
|