-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathrm.ts
More file actions
85 lines (65 loc) · 2.35 KB
/
Copy pathrm.ts
File metadata and controls
85 lines (65 loc) · 2.35 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 type { ApifyApiError } from 'apify-client';
import chalk from 'chalk';
import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
import { Args } from '../../lib/command-framework/args.js';
import { YesFlag } from '../../lib/command-framework/flags.js';
import { tryToGetDataset } from '../../lib/commands/storages.js';
import { useYesNoConfirm } from '../../lib/hooks/user-confirmations/useYesNoConfirm.js';
import { error, info, success } from '../../lib/outputs.js';
import { getLoggedClientOrThrow } from '../../lib/utils.js';
export class DatasetsRmCommand extends ApifyCommand<typeof DatasetsRmCommand> {
static override name = 'rm' as const;
static override description = 'Permanently removes a dataset.';
static override interactive = true;
static override interactiveNote =
'Prompts for confirmation before deleting. Cannot be bypassed; deletion is irreversible.';
static override examples = [
{
description: 'Delete a dataset by name or ID (prompts for confirmation).',
command: 'apify datasets rm my-dataset',
},
];
static override docsUrl = 'https://docs.apify.com/cli/docs/reference#apify-datasets-rm';
static override args = {
datasetNameOrId: Args.string({
description: 'The dataset ID or name to delete.',
required: true,
}),
};
static override flags = {
...YesFlag,
};
async run() {
const { datasetNameOrId } = this.args;
const { yes } = this.flags;
const client = await getLoggedClientOrThrow();
const existingDataset = await tryToGetDataset(client, datasetNameOrId);
if (!existingDataset) {
error({
message: `Dataset with ID or name "${datasetNameOrId}" not found.`,
});
return;
}
const confirmed = await useYesNoConfirm({
message: `Are you sure you want to delete this Dataset?`,
providedConfirmFromStdin: yes || undefined,
});
if (!confirmed) {
info({ message: 'Dataset deletion has been aborted.' });
return;
}
const { id, name } = existingDataset.dataset;
try {
await existingDataset.datasetClient.delete();
success({
message: `Dataset with ID ${chalk.yellow(id)}${name ? ` (called ${chalk.yellow(name)})` : ''} has been deleted.`,
stdout: true,
});
} catch (err) {
const casted = err as ApifyApiError;
error({
message: `Failed to delete dataset with ID ${chalk.yellow(id)}\n ${casted.message || casted}`,
});
}
}
}