-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcreate.ts
48 lines (36 loc) · 1.26 KB
/
create.ts
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
import { Args } from '@oclif/core';
import chalk from 'chalk';
import { ApifyCommand } from '../../lib/apify_command.js';
import { tryToGetDataset } from '../../lib/commands/storages.js';
import { error, success } from '../../lib/outputs.js';
import { getLoggedClientOrThrow } from '../../lib/utils.js';
export class DatasetsCreateCommand extends ApifyCommand<typeof DatasetsCreateCommand> {
static override description = 'Creates a new dataset for storing structured data on your account.';
static override args = {
datasetName: Args.string({
description: 'Optional name for the Dataset',
required: false,
}),
};
static override enableJsonFlag = true;
async run() {
const { datasetName } = this.args;
const client = await getLoggedClientOrThrow();
if (datasetName) {
const existing = await tryToGetDataset(client, datasetName);
if (existing) {
error({ message: 'A Dataset with this name already exists!' });
return;
}
}
const newDataset = await client.datasets().getOrCreate(datasetName);
if (this.flags.json) {
return newDataset;
}
success({
message: `Dataset with ID ${chalk.yellow(newDataset.id)}${datasetName ? ` (called ${chalk.yellow(datasetName)})` : ''} was created.`,
stdout: true,
});
return undefined;
}
}