@writedocs/generator 0.13.3 → 0.13.4

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@writedocs/generator",
3
- "version": "0.13.3",
3
+ "version": "0.13.4",
4
4
  "description": "Static site generator for docs — a writedocs.json + MDX folder in, a static site out.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1,124 +1,124 @@
1
- // `writedocs convert --mintlify [dir]` / `--writedocs [dir]` - turns a
2
- // Mintlify project's docs.json, or a config.json from the previous writedocs,
3
- // into writedocs.json, in the same folder, then checks the pages the same way
4
- // `writedocs validate` does. The conversions themselves are
5
- // lib/mintlify-convert.js and lib/writedocs-legacy-convert.js; this file is
6
- // the CLI around them.
7
- import fs from 'node:fs';
8
- import path from 'node:path';
9
- import { loadMintlifyConfig, convertMintlifyConfig, formatNotes } from '../lib/mintlify-convert.js';
10
- import { loadLegacyConfig, convertLegacyConfig } from '../lib/writedocs-legacy-convert.js';
11
- import { validateDocsConfig, formatValidationIssuesDetailed } from '../lib/config-schema.js';
12
- import { checkContent, formatContentIssues } from '../lib/content-check.js';
13
- import { log, step, plural, color, displayPath, CliExit, logToStderr } from './output.js';
14
-
15
- const SOURCES = {
16
- mintlify: {
17
- file: 'docs.json',
18
- load: loadMintlifyConfig,
19
- convert: async (input) => convertMintlifyConfig(input),
20
- missing(contentDir) {
21
- if (fs.existsSync(path.join(contentDir, 'mint.json'))) {
22
- log.error("Found mint.json, Mintlify's older config format.");
23
- log.detail(color.dim(`Run "npx mint upgrade" in ${contentDir} to turn it into docs.json, then run this again.`));
24
- } else {
25
- log.error(`No docs.json found in ${contentDir}`);
26
- }
27
- },
28
- domainHint: '(Mintlify sets this in its dashboard, not docs.json.)',
29
- },
30
- writedocs: {
31
- file: 'config.json',
32
- load: loadLegacyConfig,
33
- convert: (input, contentDir) => convertLegacyConfig(input, contentDir),
34
- missing(contentDir) {
35
- log.error(`No config.json found in ${contentDir}`);
36
- },
37
- domainHint: '(config.json had no field for it.)',
38
- },
39
- };
40
-
41
- export async function runConvert({ source = 'mintlify', contentDir, force = false, dryRun = false }) {
42
- // --dry-run's stdout is the converted writedocs.json and nothing else, so
43
- // `writedocs convert --dry-run > writedocs.json` works; the notes and the
44
- // page check go to stderr.
45
- if (dryRun) logToStderr();
46
- const from = SOURCES[source];
47
- const inputPath = path.join(contentDir, from.file);
48
- if (!fs.existsSync(inputPath)) {
49
- from.missing(contentDir);
50
- throw new CliExit(1);
51
- }
52
- const outPath = path.join(contentDir, 'writedocs.json');
53
- if (!dryRun && !force && fs.existsSync(outPath)) {
54
- log.error(`${displayPath(outPath)} already exists.`);
55
- log.detail(color.dim('Pass --force to overwrite it, or --dry-run to only see the result.'));
56
- throw new CliExit(1);
57
- }
58
-
59
- let input;
60
- try {
61
- input = from.load(inputPath);
62
- } catch (err) {
63
- log.error(`Couldn't read ${displayPath(inputPath)}: ${err.message}`);
64
- throw new CliExit(1);
65
- }
66
-
67
- const { config, notes } = await from.convert(input, contentDir);
68
- const text = `${JSON.stringify(config, null, 2)}\n`;
69
-
70
- // The result must pass writedocs' own schema - if it doesn't, that's a
71
- // converter bug, and writing it would only hand the author a broken file.
72
- const result = validateDocsConfig(text);
73
- if (!result.ok) {
74
- log.error('The converted writedocs.json is not valid - this is a bug in the converter, please report it:');
75
- log.line();
76
- log.line(formatValidationIssuesDetailed(result.issues));
77
- log.line(`\n${text}`);
78
- throw new CliExit(1);
79
- }
80
-
81
- if (dryRun) {
82
- process.stdout.write(text);
83
- } else {
84
- fs.writeFileSync(outPath, text);
85
- log.success(`Converted ${from.file} to ${color.bold(displayPath(outPath))}`);
86
- }
87
-
88
- if (notes.length) {
89
- log.line();
90
- log.info(`${plural(notes.length, 'thing')} couldn't be carried over as-is:`);
91
- log.line();
92
- log.line(formatNotes(notes));
93
- }
94
-
95
- // The pages, checked against the converted config - what's left to fix
96
- // before the first build.
97
- const checking = step('Checking pages');
98
- const content = await checkContent(contentDir, text);
99
- // The domain reminder comes once, at the end, with where the source tool
100
- // kept that setting.
101
- content.warnings = content.warnings.filter((w) => w.code !== 'no-domain');
102
- checking.stop();
103
- if (content.errors.length) {
104
- log.line();
105
- log.error(`${plural(content.errors.length, 'error')} in the pages - the build would fail on these:`);
106
- log.line();
107
- log.line(formatContentIssues(content.errors));
108
- }
109
- if (content.warnings.length) {
110
- log.line();
111
- log.warn(`${plural(content.warnings.length, 'warning')} in the pages:`);
112
- log.line();
113
- log.line(formatContentIssues(content.warnings));
114
- }
115
- log.line();
116
- const summary = `Checked ${plural(content.pages, 'page')}: ${plural(content.errors.length, 'error')}, ${plural(content.warnings.length, 'warning')}`;
117
- if (content.errors.length) log.error(summary);
118
- else log.success(summary);
119
- if (!config.domain) {
120
- log.info(
121
- `Next: set "domain" in writedocs.json to your site's URL - it turns on sitemap.xml and absolute links for social previews. ${from.domainHint}`
122
- );
123
- }
124
- }
1
+ // `writedocs convert --mintlify [dir]` / `--writedocs [dir]` - turns a
2
+ // Mintlify project's docs.json, or a config.json from the previous writedocs,
3
+ // into writedocs.json, in the same folder, then checks the pages the same way
4
+ // `writedocs validate` does. The conversions themselves are
5
+ // lib/mintlify-convert.js and lib/writedocs-legacy-convert.js; this file is
6
+ // the CLI around them.
7
+ import fs from 'node:fs';
8
+ import path from 'node:path';
9
+ import { loadMintlifyConfig, convertMintlifyConfig, formatNotes } from '../lib/mintlify-convert.js';
10
+ import { loadLegacyConfig, convertLegacyConfig } from '../lib/writedocs-legacy-convert.js';
11
+ import { validateDocsConfig, formatValidationIssuesDetailed } from '../lib/config-schema.js';
12
+ import { checkContent, formatContentIssues } from '../lib/content-check.js';
13
+ import { log, step, plural, color, displayPath, CliExit, logToStderr } from './output.js';
14
+
15
+ const SOURCES = {
16
+ mintlify: {
17
+ file: 'docs.json',
18
+ load: loadMintlifyConfig,
19
+ convert: async (input) => convertMintlifyConfig(input),
20
+ missing(contentDir) {
21
+ if (fs.existsSync(path.join(contentDir, 'mint.json'))) {
22
+ log.error("Found mint.json, Mintlify's older config format.");
23
+ log.detail(color.dim(`Run "npx mint upgrade" in ${contentDir} to turn it into docs.json, then run this again.`));
24
+ } else {
25
+ log.error(`No docs.json found in ${contentDir}`);
26
+ }
27
+ },
28
+ domainHint: '(Mintlify sets this in its dashboard, not docs.json.)',
29
+ },
30
+ writedocs: {
31
+ file: 'config.json',
32
+ load: loadLegacyConfig,
33
+ convert: (input, contentDir) => convertLegacyConfig(input, contentDir),
34
+ missing(contentDir) {
35
+ log.error(`No config.json found in ${contentDir}`);
36
+ },
37
+ domainHint: '(config.json had no field for it.)',
38
+ },
39
+ };
40
+
41
+ export async function runConvert({ source = 'mintlify', contentDir, force = false, dryRun = false }) {
42
+ // --dry-run's stdout is the converted writedocs.json and nothing else, so
43
+ // `writedocs convert --dry-run > writedocs.json` works; the notes and the
44
+ // page check go to stderr.
45
+ if (dryRun) logToStderr();
46
+ const from = SOURCES[source];
47
+ const inputPath = path.join(contentDir, from.file);
48
+ if (!fs.existsSync(inputPath)) {
49
+ from.missing(contentDir);
50
+ throw new CliExit(1);
51
+ }
52
+ const outPath = path.join(contentDir, 'writedocs.json');
53
+ if (!dryRun && !force && fs.existsSync(outPath)) {
54
+ log.error(`${displayPath(outPath)} already exists.`);
55
+ log.detail(color.dim('Pass --force to overwrite it, or --dry-run to only see the result.'));
56
+ throw new CliExit(1);
57
+ }
58
+
59
+ let input;
60
+ try {
61
+ input = from.load(inputPath);
62
+ } catch (err) {
63
+ log.error(`Couldn't read ${displayPath(inputPath)}: ${err.message}`);
64
+ throw new CliExit(1);
65
+ }
66
+
67
+ const { config, notes } = await from.convert(input, contentDir);
68
+ const text = `${JSON.stringify(config, null, 2)}\n`;
69
+
70
+ // The result must pass writedocs' own schema - if it doesn't, that's a
71
+ // converter bug, and writing it would only hand the author a broken file.
72
+ const result = validateDocsConfig(text);
73
+ if (!result.ok) {
74
+ log.error('The converted writedocs.json is not valid - this is a bug in the converter, please report it:');
75
+ log.line();
76
+ log.line(formatValidationIssuesDetailed(result.issues, { total: result.issuesTotal, truncated: result.issuesTruncated }));
77
+ log.line(`\n${text}`);
78
+ throw new CliExit(1);
79
+ }
80
+
81
+ if (dryRun) {
82
+ process.stdout.write(text);
83
+ } else {
84
+ fs.writeFileSync(outPath, text);
85
+ log.success(`Converted ${from.file} to ${color.bold(displayPath(outPath))}`);
86
+ }
87
+
88
+ if (notes.length) {
89
+ log.line();
90
+ log.info(`${plural(notes.length, 'thing')} couldn't be carried over as-is:`);
91
+ log.line();
92
+ log.line(formatNotes(notes));
93
+ }
94
+
95
+ // The pages, checked against the converted config - what's left to fix
96
+ // before the first build.
97
+ const checking = step('Checking pages');
98
+ const content = await checkContent(contentDir, text);
99
+ // The domain reminder comes once, at the end, with where the source tool
100
+ // kept that setting.
101
+ content.warnings = content.warnings.filter((w) => w.code !== 'no-domain');
102
+ checking.stop();
103
+ if (content.errors.length) {
104
+ log.line();
105
+ log.error(`${plural(content.errors.length, 'error')} in the pages - the build would fail on these:`);
106
+ log.line();
107
+ log.line(formatContentIssues(content.errors));
108
+ }
109
+ if (content.warnings.length) {
110
+ log.line();
111
+ log.warn(`${plural(content.warnings.length, 'warning')} in the pages:`);
112
+ log.line();
113
+ log.line(formatContentIssues(content.warnings));
114
+ }
115
+ log.line();
116
+ const summary = `Checked ${plural(content.pages, 'page')}: ${plural(content.errors.length, 'error')}, ${plural(content.warnings.length, 'warning')}`;
117
+ if (content.errors.length) log.error(summary);
118
+ else log.success(summary);
119
+ if (!config.domain) {
120
+ log.info(
121
+ `Next: set "domain" in writedocs.json to your site's URL - it turns on sitemap.xml and absolute links for social previews. ${from.domainHint}`
122
+ );
123
+ }
124
+ }
@@ -1,38 +1,42 @@
1
- // Leaving a process without losing what it printed. astro-worker.js ends with
2
- // process.exit() - Astro leaves handles open, so the process wouldn't end by
3
- // itself - and on a pipe both stdout/stderr and the IPC channel are
4
- // asynchronous (on Linux): exit() drops whatever is still queued in the
5
- // process. A fixed wait isn't enough: a page that prints 1 MiB on a busy
6
- // machine still had most of it queued after 50 ms, and the CLI got the output
7
- // cut short with the build exiting 0.
8
- //
9
- // Plain JavaScript with no Astro import, so scripts/exit-when-flushed.test.js
10
- // can run it in a child process of its own.
11
-
12
- /** How long the wait may take. If the CLI stops reading (alive, but stuck),
13
- * the worker exits anyway instead of hanging. */
14
- export const MAX_FLUSH_WAIT_MS = 10_000;
15
-
16
- /** Resolves once everything written to `stream` so far has left the process. */
17
- function flushed(stream) {
18
- return new Promise((resolve) => {
19
- if (!stream.writable || stream.destroyed) return resolve();
20
- stream.write('', () => resolve());
21
- });
22
- }
23
-
24
- /** Sends `lastMessage` to the parent (when there is one), waits for it and
25
- * for stdout and stderr to leave the process, then exits with `code` - or
26
- * after `maxWaitMs`, whichever comes first. */
27
- export async function exitWhenFlushed(code, lastMessage, { maxWaitMs = MAX_FLUSH_WAIT_MS } = {}) {
28
- // unref: the timer alone never keeps the process alive.
29
- const deadline = setTimeout(() => process.exit(code), maxWaitMs);
30
- deadline.unref();
31
- await new Promise((resolve) => {
32
- if (lastMessage !== undefined && typeof process.send === 'function' && process.connected) {
33
- process.send(lastMessage, () => resolve());
34
- } else resolve();
35
- });
36
- await Promise.all([flushed(process.stdout), flushed(process.stderr)]);
37
- process.exit(code);
38
- }
1
+ // Leaving a process without losing what it printed. astro-worker.js ends with
2
+ // process.exit() - Astro leaves handles open, so the process wouldn't end by
3
+ // itself - and on a pipe both stdout/stderr and the IPC channel are
4
+ // asynchronous (on Linux): exit() drops whatever is still queued in the
5
+ // process. A fixed wait isn't enough: a page that prints 1 MiB on a busy
6
+ // machine still had most of it queued after 50 ms, and the CLI got the output
7
+ // cut short with the build exiting 0.
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
+ //
13
+ // Plain JavaScript with no Astro import, so scripts/exit-when-flushed.test.js
14
+ // can run it in a child process of its own.
15
+
16
+ /** How long the wait may take. If the CLI stops reading (alive, but stuck),
17
+ * the worker exits anyway instead of hanging. */
18
+ export const MAX_FLUSH_WAIT_MS = 10_000;
19
+
20
+ /** Resolves once everything written to `stream` so far has left the process. */
21
+ function flushed(stream) {
22
+ return new Promise((resolve) => {
23
+ if (!stream.writable || stream.destroyed) return resolve();
24
+ stream.write('', () => resolve());
25
+ });
26
+ }
27
+
28
+ /** Sends `lastMessage` to the parent (when there is one), waits for it and
29
+ * for stdout and stderr to leave the process, then exits with `code` - or
30
+ * after `maxWaitMs`, whichever comes first. */
31
+ export async function exitWhenFlushed(code, lastMessage, { maxWaitMs = MAX_FLUSH_WAIT_MS } = {}) {
32
+ // unref: the timer alone never keeps the process alive.
33
+ const deadline = setTimeout(() => process.exit(code), maxWaitMs);
34
+ deadline.unref();
35
+ await new Promise((resolve) => {
36
+ if (lastMessage !== undefined && typeof process.send === 'function' && process.connected) {
37
+ process.send(lastMessage, () => resolve());
38
+ } else resolve();
39
+ });
40
+ await Promise.all([flushed(process.stdout), flushed(process.stderr)]);
41
+ process.exit(code);
42
+ }