@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.
@@ -1,93 +1,97 @@
1
- import path from 'node:path';
2
- import { spawn } from 'node:child_process';
3
- import { canonicalPath } from '../lib/canonical-path.js';
4
- import { fileURLToPath } from 'node:url';
5
-
6
- const workerPath = path.join(path.dirname(fileURLToPath(import.meta.url)), 'astro-worker.js');
7
-
8
- /**
9
- * Runs Astro (`command`: 'dev' or 'build') in a child process -
10
- * astro-worker.js - and turns everything it reports into events for
11
- * `onEvent`, so the CLI decides what reaches the terminal:
12
- *
13
- * { type: 'astro-log', level, label, message } - an Astro/Vite log line
14
- * { type: 'report', level, message, where } - writedocs' own code
15
- * (lib/cli-report.js)
16
- * { type: 'raw', stream, line } - anything printed directly
17
- * { type: 'ready', urls } / { type: 'done' } / { type: 'fatal', error }
18
- *
19
- * A raw "[writedocs] ..." line is turned into a 'report' too, so a
20
- * console.warn that doesn't go through cli-report.js still shows up.
21
- *
22
- * Returns { child, exited } - `exited` resolves with the exit code.
23
- */
24
- export function runAstro(command, { packageRoot, contentDir, port, onEvent }) {
25
- // One spelling of each folder for Astro and Vite - see canonical-path.js.
26
- packageRoot = canonicalPath(packageRoot);
27
- contentDir = canonicalPath(contentDir);
28
- const child = spawn(process.execPath, [workerPath, command], {
29
- stdio: ['ignore', 'pipe', 'pipe', 'ipc'],
30
- // Explicit, not inherited: astro.config.mjs's own `outDir` lives
31
- // inside packageRoot (writedocsBuildStagingDir() - see its own
32
- // comment in writedocs-temp-dir.js for the full EXDEV story this is
33
- // one half of), and Astro's static builder only stages its
34
- // prerendered output *inside* that outDir - rather than falling
35
- // back to `<process.cwd()>/.astro/.prerender/` - when outDir starts
36
- // with process.cwd() (getOutDirWithinCwd, astro/dist/core/build/
37
- // common.js). Left unset, this child would just inherit whatever
38
- // directory the *parent* writedocs CLI process happened to be
39
- // launched from - unrelated to packageRoot for a real globally-
40
- // installed CLI invoked from wherever the user's shell happens to
41
- // be - so the fallback branch would fire regardless, staging
42
- // outside packageRoot again and reintroducing the exact module-
43
- // resolution problem this whole design is meant to avoid. Pinning
44
- // cwd to packageRoot here guarantees the "starts with cwd" check
45
- // passes deterministically, independent of the parent process's own
46
- // cwd.
47
- cwd: packageRoot,
48
- env: {
49
- ...process.env,
50
- WRITEDOCS_CONTENT_DIR: contentDir,
51
- // Astro is always run with root: packageRoot (astro-worker.js), so
52
- // every CollectionEntry's `filePath` comes back relative to *this*,
53
- // not to contentDir or the process's cwd. Exposed so lib/config.ts's
54
- // fileIdForEntry() can resolve it back into a docs/-relative file id
55
- // - see that function for why.
56
- WRITEDOCS_PACKAGE_ROOT: packageRoot,
57
- // 'dev' or 'build' - lib/build-memo.js keeps per-build results only in a build.
58
- WRITEDOCS_COMMAND: command,
59
- ...(port ? { WRITEDOCS_PORT: String(port) } : {}),
60
- },
61
- });
62
-
63
- child.on('message', (message) => {
64
- if (message && typeof message === 'object' && typeof message.type === 'string') onEvent(message);
65
- });
66
-
67
- for (const streamName of ['stdout', 'stderr']) {
68
- let pending = '';
69
- const emit = (line) => {
70
- if (line.trim() === '') return;
71
- const own = line.match(/^\[writedocs\]\s+(?:(\S+:\d+(?::\d+)?) - )?(.*)$/);
72
- if (own) onEvent({ type: 'report', level: streamName === 'stderr' ? 'warn' : 'info', message: own[2], where: own[1] ?? null });
73
- else onEvent({ type: 'raw', stream: streamName, line });
74
- };
75
- child[streamName].setEncoding('utf8');
76
- child[streamName].on('data', (chunk) => {
77
- pending += chunk;
78
- const lines = pending.split(/\r?\n/);
79
- pending = lines.pop();
80
- lines.forEach(emit);
81
- });
82
- child[streamName].on('end', () => {
83
- if (pending) emit(pending);
84
- pending = '';
85
- });
86
- }
87
-
88
- const exited = new Promise((resolve, reject) => {
89
- child.on('error', reject);
90
- child.on('close', (code, signal) => resolve(code ?? (signal ? 1 : 0)));
91
- });
92
- return { child, exited };
93
- }
1
+ import path from 'node:path';
2
+ import { spawn } from 'node:child_process';
3
+ import { canonicalPath } from '../lib/canonical-path.js';
4
+ import { fileURLToPath } from 'node:url';
5
+
6
+ const workerPath = path.join(path.dirname(fileURLToPath(import.meta.url)), 'astro-worker.js');
7
+
8
+ /**
9
+ * Runs Astro (`command`: 'dev' or 'build') in a child process -
10
+ * astro-worker.js - and turns everything it reports into events for
11
+ * `onEvent`, so the CLI decides what reaches the terminal:
12
+ *
13
+ * { type: 'astro-log', level, label, message } - an Astro/Vite log line
14
+ * { type: 'report', level, message, where } - writedocs' own code
15
+ * (lib/cli-report.js)
16
+ * { type: 'raw', stream, line } - anything printed directly
17
+ * { type: 'ready', urls } / { type: 'done' } / { type: 'fatal', error }
18
+ *
19
+ * A raw "[writedocs] ..." line is turned into a 'report' too, so a
20
+ * console.warn that doesn't go through cli-report.js still shows up.
21
+ *
22
+ * `env` adds variables for the child only (build.js's
23
+ * WRITEDOCS_OG_CARDS_FILE).
24
+ *
25
+ * Returns { child, exited } - `exited` resolves with the exit code.
26
+ */
27
+ export function runAstro(command, { packageRoot, contentDir, port, env = {}, onEvent }) {
28
+ // One spelling of each folder for Astro and Vite - see canonical-path.js.
29
+ packageRoot = canonicalPath(packageRoot);
30
+ contentDir = canonicalPath(contentDir);
31
+ const child = spawn(process.execPath, [workerPath, command], {
32
+ stdio: ['ignore', 'pipe', 'pipe', 'ipc'],
33
+ // Explicit, not inherited: astro.config.mjs's own `outDir` lives
34
+ // inside packageRoot (writedocsBuildStagingDir() - see its own
35
+ // comment in writedocs-temp-dir.js for the full EXDEV story this is
36
+ // one half of), and Astro's static builder only stages its
37
+ // prerendered output *inside* that outDir - rather than falling
38
+ // back to `<process.cwd()>/.astro/.prerender/` - when outDir starts
39
+ // with process.cwd() (getOutDirWithinCwd, astro/dist/core/build/
40
+ // common.js). Left unset, this child would just inherit whatever
41
+ // directory the *parent* writedocs CLI process happened to be
42
+ // launched from - unrelated to packageRoot for a real globally-
43
+ // installed CLI invoked from wherever the user's shell happens to
44
+ // be - so the fallback branch would fire regardless, staging
45
+ // outside packageRoot again and reintroducing the exact module-
46
+ // resolution problem this whole design is meant to avoid. Pinning
47
+ // cwd to packageRoot here guarantees the "starts with cwd" check
48
+ // passes deterministically, independent of the parent process's own
49
+ // cwd.
50
+ cwd: packageRoot,
51
+ env: {
52
+ ...process.env,
53
+ WRITEDOCS_CONTENT_DIR: contentDir,
54
+ // Astro is always run with root: packageRoot (astro-worker.js), so
55
+ // every CollectionEntry's `filePath` comes back relative to *this*,
56
+ // not to contentDir or the process's cwd. Exposed so lib/config.ts's
57
+ // fileIdForEntry() can resolve it back into a docs/-relative file id
58
+ // - see that function for why.
59
+ WRITEDOCS_PACKAGE_ROOT: packageRoot,
60
+ // 'dev' or 'build' - lib/build-memo.js keeps per-build results only in a build.
61
+ WRITEDOCS_COMMAND: command,
62
+ ...(port ? { WRITEDOCS_PORT: String(port) } : {}),
63
+ ...env,
64
+ },
65
+ });
66
+
67
+ child.on('message', (message) => {
68
+ if (message && typeof message === 'object' && typeof message.type === 'string') onEvent(message);
69
+ });
70
+
71
+ for (const streamName of ['stdout', 'stderr']) {
72
+ let pending = '';
73
+ const emit = (line) => {
74
+ if (line.trim() === '') return;
75
+ const own = line.match(/^\[writedocs\]\s+(?:(\S+:\d+(?::\d+)?) - )?(.*)$/);
76
+ if (own) onEvent({ type: 'report', level: streamName === 'stderr' ? 'warn' : 'info', message: own[2], where: own[1] ?? null });
77
+ else onEvent({ type: 'raw', stream: streamName, line });
78
+ };
79
+ child[streamName].setEncoding('utf8');
80
+ child[streamName].on('data', (chunk) => {
81
+ pending += chunk;
82
+ const lines = pending.split(/\r?\n/);
83
+ pending = lines.pop();
84
+ lines.forEach(emit);
85
+ });
86
+ child[streamName].on('end', () => {
87
+ if (pending) emit(pending);
88
+ pending = '';
89
+ });
90
+ }
91
+
92
+ const exited = new Promise((resolve, reject) => {
93
+ child.on('error', reject);
94
+ child.on('close', (code, signal) => resolve(code ?? (signal ? 1 : 0)));
95
+ });
96
+ return { child, exited };
97
+ }