-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype-view.ts
More file actions
59 lines (52 loc) · 1.89 KB
/
type-view.ts
File metadata and controls
59 lines (52 loc) · 1.89 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
import { getHost, getToken } from "../auth";
import { getCustomTypes } from "../clients/custom-types";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { stringify } from "../lib/json";
import { formatTable } from "../lib/string";
import { getRepositoryName } from "../project";
const config = {
name: "prismic type view",
description: "View details of a content type.",
positionals: {
name: { description: "Name of the content type", required: true },
},
options: {
json: { type: "boolean", description: "Output as JSON" },
repo: { type: "string", short: "r", description: "Repository domain" },
},
} satisfies CommandConfig;
export default createCommand(config, async ({ positionals, values }) => {
const [name] = positionals;
const { json, repo = await getRepositoryName() } = values;
const token = await getToken();
const host = await getHost();
const customTypes = await getCustomTypes({ repo, token, host });
const type = customTypes.find((ct) => ct.label === name);
if (!type) {
throw new CommandError(`Type not found: ${name}`);
}
if (json) {
console.info(stringify(type));
return;
}
console.info(`ID: ${type.id}`);
console.info(`Name: ${type.label || "(no name)"}`);
console.info(`Format: ${type.format}`);
console.info(`Repeatable: ${type.repeatable}`);
for (const [tabName, fields] of Object.entries(type.json)) {
console.info("");
console.info(`${tabName}:`);
const entries = Object.entries(fields);
if (entries.length === 0) {
console.info(" (no fields)");
} else {
const rows = entries.map(([id, field]) => {
const config = field.config as Record<string, unknown> | undefined;
const label = (config?.label as string) || "";
const placeholder = config?.placeholder ? `"${config.placeholder}"` : "";
return [` ${id}`, field.type, label, placeholder];
});
console.info(formatTable(rows));
}
}
});