Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1613,12 +1613,15 @@ These commands help you manage scheduled and configured Actor runs. Use them to

```sh
DESCRIPTION
Run saved Apify tasks (named Actor configurations). Only 'task run' is
available; create and manage tasks in Apify Console.
Run and publish saved Apify tasks (named Actor configurations). Create and
manage tasks in Apify Console.

SUBCOMMANDS
task run Executes predefined Actor task remotely using local
key-value store for input.
task run Executes predefined Actor task remotely using
local key-value store for input.
task publish Publishes the task on its public landing page.
task unpublish Unpublishes the task from its public landing
Comment on lines +1622 to +1623

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: I understand those two commands but how can one create/update tasks via CLI so they actually have publicConfig set? publish commands already assumes it is set and valid, right?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not possible today, but it's also not possible for the Actors, but I agree it's kind of strange.

page.
```

##### `apify task run`
Expand All @@ -1645,6 +1648,39 @@ FLAGS
-t, --timeout=<value> Timeout for the Task run in seconds.
Zero value means there is no timeout.
```

##### `apify task publish`

```sh
DESCRIPTION
Publishes the task on its public landing page.
The task must belong to a public Actor and have its public display
configuration set up (in Apify Console, on the task Publication tab). Requires
write access to the task and to its Actor.

USAGE
$ apify task publish <taskId>

ARGUMENTS
taskId Name of the Task to publish, or its full name (e.g. "my-task" or
"username/my-task").
```

##### `apify task unpublish`

```sh
DESCRIPTION
Unpublishes the task from its public landing page.
The public display configuration is preserved, so the task can be published
again later. Requires write access to the task and to its Actor.

USAGE
$ apify task unpublish <taskId>

ARGUMENTS
taskId Name of the Task to unpublish, or its full name (e.g. "my-task"
or "username/my-task").
```
<!-- task-commands-end -->
<!-- prettier-ignore-end -->

Expand Down
2 changes: 2 additions & 0 deletions scripts/generate-cli-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ const categories: Record<string, CommandsInCategory[]> = {
//
{ command: Commands.task },
{ command: Commands.taskRun },
{ command: Commands.taskPublish },
{ command: Commands.taskUnpublish },
],
'mcp': [
//
Expand Down
6 changes: 4 additions & 2 deletions src/commands/task/_index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
import { TaskPublishCommand } from './publish.js';
import { TaskRunCommand } from './run.js';
import { TaskUnpublishCommand } from './unpublish.js';

export class TasksIndexCommand extends ApifyCommand<typeof TasksIndexCommand> {
static override name = 'task' as const;

static override description = `Run saved Apify tasks (named Actor configurations). Only 'task run' is available; create and manage tasks in Apify Console.`;
static override description = `Run and publish saved Apify tasks (named Actor configurations). Create and manage tasks in Apify Console.`;

static override group = 'Apify Console';

static override docsUrl = 'https://docs.apify.com/cli/docs/reference#apify-task';

static override subcommands = [TaskRunCommand];
static override subcommands = [TaskRunCommand, TaskPublishCommand, TaskUnpublishCommand];

async run() {
this.printHelp();
Expand Down
71 changes: 71 additions & 0 deletions src/commands/task/publish.ts

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important: You should probably import process from 'node:process';

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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 { CommandExitCodes } from '../../lib/consts.js';
import { error, success } from '../../lib/outputs.js';
import { getLocalUserInfo, getLoggedClientOrThrow } from '../../lib/utils.js';

export class TaskPublishCommand extends ApifyCommand<typeof TaskPublishCommand> {
static override name = 'publish' as const;

static override description =
'Publishes the task on its public landing page.\n' +
'The task must belong to a public Actor and have its public display configuration set up ' +
Comment thread
katzino marked this conversation as resolved.
'(in Apify Console, on the task Publication tab). ' +
'Requires write access to the task and to its Actor.';

static override examples = [
{
description: 'Publish a task by name.',
command: 'apify task publish my-task',
},
{
description: 'Publish a task by its full name.',
command: 'apify task publish username/my-task',
},
];

static override docsUrl = 'https://docs.apify.com/cli/docs/reference#apify-task-publish';

static override args = {
taskId: Args.string({
required: true,
description: 'Name of the Task to publish, or its full name (e.g. "my-task" or "username/my-task").',
}),
};

async run() {
const apifyClient = await getLoggedClientOrThrow();
const userInfo = await getLocalUserInfo();
const usernameOrId = userInfo.username || (userInfo.id as string);

const { taskId } = this.args;
const idOrName = taskId.includes('/') ? taskId : `${usernameOrId}/${taskId.toLowerCase()}`;
const taskClient = apifyClient.task(idOrName);

const task = await taskClient.get();
if (!task) {
error({ message: `Cannot find Task with name '${taskId}' in your account.` });
process.exitCode = CommandExitCodes.NotFound;
return;
}

try {
await taskClient.publish();

success({
message: `Task ${chalk.yellow(task.name)} has been published.`,
stdout: true,
});
} catch (err) {
const casted = err as ApifyApiError;

error({
Comment thread
katzino marked this conversation as resolved.
message: `Failed to publish Task ${chalk.yellow(task.name)}\n ${casted.message || casted}`,
});
process.exitCode = CommandExitCodes.RunFailed;
}
}
}
70 changes: 70 additions & 0 deletions src/commands/task/unpublish.ts

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important: You should probably import process from 'node:process';

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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 { CommandExitCodes } from '../../lib/consts.js';
import { error, success } from '../../lib/outputs.js';
import { getLocalUserInfo, getLoggedClientOrThrow } from '../../lib/utils.js';

export class TaskUnpublishCommand extends ApifyCommand<typeof TaskUnpublishCommand> {
static override name = 'unpublish' as const;

static override description =
'Unpublishes the task from its public landing page.\n' +
'The public display configuration is preserved, so the task can be published again later. ' +
'Requires write access to the task and to its Actor.';

static override examples = [
{
description: 'Unpublish a task by name.',
command: 'apify task unpublish my-task',
},
{
description: 'Unpublish a task by its full name.',
command: 'apify task unpublish username/my-task',
},
];

static override docsUrl = 'https://docs.apify.com/cli/docs/reference#apify-task-unpublish';

static override args = {
taskId: Args.string({
required: true,
description: 'Name of the Task to unpublish, or its full name (e.g. "my-task" or "username/my-task").',
}),
};

async run() {
const apifyClient = await getLoggedClientOrThrow();
const userInfo = await getLocalUserInfo();
const usernameOrId = userInfo.username || (userInfo.id as string);

const { taskId } = this.args;
const idOrName = taskId.includes('/') ? taskId : `${usernameOrId}/${taskId.toLowerCase()}`;
const taskClient = apifyClient.task(idOrName);

const task = await taskClient.get();
if (!task) {
error({ message: `Cannot find Task with name '${taskId}' in your account.` });
process.exitCode = CommandExitCodes.NotFound;
return;
}

try {
await taskClient.unpublish();

success({
message: `Task ${chalk.yellow(task.name)} has been unpublished.`,
stdout: true,
});
} catch (err) {
const casted = err as ApifyApiError;

error({
message: `Failed to unpublish Task ${chalk.yellow(task.name)}\n ${casted.message || casted}`,
});
process.exitCode = CommandExitCodes.RunFailed;
}
}
}
Loading