-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice-remove.ts
More file actions
49 lines (42 loc) · 1.4 KB
/
slice-remove.ts
File metadata and controls
49 lines (42 loc) · 1.4 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
import { getAdapter } from "../adapters";
import { getHost, getToken } from "../auth";
import { getSlices, removeSlice } from "../clients/custom-types";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { UnknownRequestError } from "../lib/request";
import { getRepositoryName } from "../project";
const config = {
name: "prismic slice remove",
description: "Remove a slice.",
positionals: {
id: { description: "ID of the slice", required: true },
},
options: {
repo: { type: "string", short: "r", description: "Repository domain" },
},
} satisfies CommandConfig;
export default createCommand(config, async ({ positionals, values }) => {
const [id] = positionals;
const { repo = await getRepositoryName() } = values;
const adapter = await getAdapter();
const token = await getToken();
const host = await getHost();
const slices = await getSlices({ repo, token, host });
const slice = slices.find((s) => s.id === id);
if (!slice) {
throw new CommandError(`Slice not found: ${id}`);
}
try {
await removeSlice(slice.id, { repo, host, token });
} catch (error) {
if (error instanceof UnknownRequestError) {
const message = await error.text();
throw new CommandError(`Failed to remove slice: ${message}`);
}
throw error;
}
try {
await adapter.deleteSlice(slice.id);
} catch {}
await adapter.generateTypes();
console.info(`Slice removed: ${id}`);
});