-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice-view.ts
More file actions
55 lines (48 loc) · 1.71 KB
/
slice-view.ts
File metadata and controls
55 lines (48 loc) · 1.71 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
import { getHost, getToken } from "../auth";
import { getSlices } from "../clients/custom-types";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { stringify } from "../lib/json";
import { getRepositoryName } from "../project";
const config = {
name: "prismic slice view",
description: "View details of a slice.",
positionals: {
id: { description: "ID of the slice", 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 [id] = positionals;
const { json, repo = await getRepositoryName() } = values;
const token = await getToken();
const host = await getHost();
const slices = await getSlices({ repo, token, host });
const slice = slices.find((slice) => slice.id === id);
if (!slice) {
throw new CommandError(`Slice not found: ${id}`);
}
if (json) {
console.info(stringify(slice));
return;
}
console.info(`ID: ${slice.id}`);
console.info(`Name: ${slice.name}`);
for (const variation of slice.variations ?? []) {
console.info("");
console.info(`${variation.id}:`);
const entries = Object.entries(variation.primary ?? {});
if (entries.length === 0) {
console.info(" (no fields)");
} else {
for (const [id, field] of entries) {
const config = field.config as Record<string, unknown> | undefined;
const label = (config?.label as string) || "";
const placeholder = config?.placeholder ? `"${config.placeholder}"` : "";
console.info(` ${[id, field.type, label, placeholder].filter(Boolean).join(" ")}`);
}
}
}
});