-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs-list.ts
More file actions
85 lines (73 loc) · 2.21 KB
/
docs-list.ts
File metadata and controls
85 lines (73 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { getDocsIndex, getDocsPageIndex } from "../clients/docs";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { stringify } from "../lib/json";
import { NotFoundRequestError, UnknownRequestError } from "../lib/request";
import { formatTable } from "../lib/string";
const config = {
name: "prismic docs list",
description: `
List available documentation pages.
With a path argument, list the anchors within that page.
`,
positionals: {
path: {
description: "Documentation path to list anchors for",
required: false,
},
},
options: {
json: { type: "boolean", description: "Output as JSON" },
},
} satisfies CommandConfig;
export default createCommand(config, async ({ positionals, values }) => {
const [path] = positionals;
const { json } = values;
if (path) {
let entry;
try {
entry = await getDocsPageIndex(path);
} catch (error) {
if (error instanceof NotFoundRequestError) {
throw new CommandError(`Documentation page not found: ${path}`);
}
if (error instanceof UnknownRequestError) {
const message = await error.text();
throw new CommandError(`Failed to fetch documentation index: ${message}`);
}
throw error;
}
entry.anchors.sort((a, b) => a.slug.localeCompare(b.slug));
if (json) {
console.info(stringify(entry));
return;
}
if (entry.anchors.length === 0) {
console.info("(no anchors)");
return;
}
const rows = entry.anchors.map((anchor) => [`${path}#${anchor.slug}`, anchor.excerpt]);
console.info(formatTable(rows, { headers: ["PATH", "EXCERPT"] }));
} else {
let pages;
try {
pages = await getDocsIndex();
} catch (error) {
if (error instanceof UnknownRequestError) {
const message = await error.text();
throw new CommandError(`Failed to fetch documentation index: ${message}`);
}
throw error;
}
pages.sort((a, b) => a.path.localeCompare(b.path));
if (json) {
console.info(stringify(pages));
return;
}
if (pages.length === 0) {
console.info("No documentation pages found.");
return;
}
const rows = pages.map((page) => [page.path, page.title, page.description ?? ""]);
console.info(formatTable(rows, { headers: ["PATH", "TITLE", "DESCRIPTION"] }));
}
});