Skip to content

Commit 5679f70

Browse files
committed
feat: add task publish and unpublish commands
1 parent 964a9e8 commit 5679f70

5 files changed

Lines changed: 178 additions & 6 deletions

File tree

docs/reference.md

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,12 +1613,15 @@ These commands help you manage scheduled and configured Actor runs. Use them to
16131613
16141614
```sh
16151615
DESCRIPTION
1616-
Run saved Apify tasks (named Actor configurations). Only 'task run' is
1617-
available; create and manage tasks in Apify Console.
1616+
Run and publish saved Apify tasks (named Actor configurations). Create and
1617+
manage tasks in Apify Console.
16181618
16191619
SUBCOMMANDS
1620-
task run Executes predefined Actor task remotely using local
1621-
key-value store for input.
1620+
task run Executes predefined Actor task remotely using
1621+
local key-value store for input.
1622+
task publish Publishes the task on its public landing page.
1623+
task unpublish Unpublishes the task from its public landing
1624+
page.
16221625
```
16231626
16241627
##### `apify task run`
@@ -1645,6 +1648,38 @@ FLAGS
16451648
-t, --timeout=<value> Timeout for the Task run in seconds.
16461649
Zero value means there is no timeout.
16471650
```
1651+
1652+
##### `apify task publish`
1653+
1654+
```sh
1655+
DESCRIPTION
1656+
Publishes the task on its public landing page.
1657+
The task must belong to a public Actor and have its public display
1658+
configuration set up (in Apify Console, on the task Publication tab).
1659+
1660+
USAGE
1661+
$ apify task publish <taskId>
1662+
1663+
ARGUMENTS
1664+
taskId Name or ID of the Task to publish (e.g. "my-task" or
1665+
"E2jjCZBezvAZnX8Rb").
1666+
```
1667+
1668+
##### `apify task unpublish`
1669+
1670+
```sh
1671+
DESCRIPTION
1672+
Unpublishes the task from its public landing page.
1673+
The public display configuration is preserved, so the task can be published
1674+
again later.
1675+
1676+
USAGE
1677+
$ apify task unpublish <taskId>
1678+
1679+
ARGUMENTS
1680+
taskId Name or ID of the Task to unpublish (e.g. "my-task" or
1681+
"E2jjCZBezvAZnX8Rb").
1682+
```
16481683
<!-- task-commands-end -->
16491684
<!-- prettier-ignore-end -->
16501685

scripts/generate-cli-docs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ const categories: Record<string, CommandsInCategory[]> = {
114114
//
115115
{ command: Commands.task },
116116
{ command: Commands.taskRun },
117+
{ command: Commands.taskPublish },
118+
{ command: Commands.taskUnpublish },
117119
],
118120
'mcp': [
119121
//

src/commands/task/_index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
2+
import { TaskPublishCommand } from './publish.js';
23
import { TaskRunCommand } from './run.js';
4+
import { TaskUnpublishCommand } from './unpublish.js';
35

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

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

911
static override group = 'Apify Console';
1012

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

13-
static override subcommands = [TaskRunCommand];
15+
static override subcommands = [TaskRunCommand, TaskPublishCommand, TaskUnpublishCommand];
1416

1517
async run() {
1618
this.printHelp();

src/commands/task/publish.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import type { ApifyApiError } from 'apify-client';
2+
import chalk from 'chalk';
3+
4+
import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
5+
import { Args } from '../../lib/command-framework/args.js';
6+
import { error, success } from '../../lib/outputs.js';
7+
import { getLocalUserInfo, getLoggedClientOrThrow } from '../../lib/utils.js';
8+
9+
export class TaskPublishCommand extends ApifyCommand<typeof TaskPublishCommand> {
10+
static override name = 'publish' as const;
11+
12+
static override description =
13+
'Publishes the task on its public landing page.\n' +
14+
'The task must belong to a public Actor and have its public display configuration set up ' +
15+
'(in Apify Console, on the task Publication tab).';
16+
17+
static override examples = [
18+
{
19+
description: 'Publish a task by name.',
20+
command: 'apify task publish my-task',
21+
},
22+
{
23+
description: 'Publish a task by full ID.',
24+
command: 'apify task publish E2jjCZBezvAZnX8Rb',
25+
},
26+
];
27+
28+
static override docsUrl = 'https://docs.apify.com/cli/docs/reference#apify-task-publish';
29+
30+
static override args = {
31+
taskId: Args.string({
32+
required: true,
33+
description: 'Name or ID of the Task to publish (e.g. "my-task" or "E2jjCZBezvAZnX8Rb").',
34+
}),
35+
};
36+
37+
async run() {
38+
const apifyClient = await getLoggedClientOrThrow();
39+
const userInfo = await getLocalUserInfo();
40+
const usernameOrId = userInfo.username || (userInfo.id as string);
41+
42+
const { taskId } = this.args;
43+
const idOrName = taskId.includes('/') ? taskId : `${usernameOrId}/${taskId.toLowerCase()}`;
44+
const taskClient = apifyClient.task(idOrName);
45+
46+
const task = await taskClient.get();
47+
if (!task) {
48+
error({ message: `Cannot find Task with ID or name '${taskId}' in your account.` });
49+
return;
50+
}
51+
52+
try {
53+
await taskClient.publish();
54+
55+
success({
56+
message: `Task ${chalk.yellow(task.name)} has been published.`,
57+
stdout: true,
58+
});
59+
} catch (err) {
60+
const casted = err as ApifyApiError;
61+
62+
error({
63+
message: `Failed to publish Task ${chalk.yellow(task.name)}\n ${casted.message || casted}`,
64+
});
65+
}
66+
}
67+
}

src/commands/task/unpublish.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import type { ApifyApiError } from 'apify-client';
2+
import chalk from 'chalk';
3+
4+
import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
5+
import { Args } from '../../lib/command-framework/args.js';
6+
import { error, success } from '../../lib/outputs.js';
7+
import { getLocalUserInfo, getLoggedClientOrThrow } from '../../lib/utils.js';
8+
9+
export class TaskUnpublishCommand extends ApifyCommand<typeof TaskUnpublishCommand> {
10+
static override name = 'unpublish' as const;
11+
12+
static override description =
13+
'Unpublishes the task from its public landing page.\n' +
14+
'The public display configuration is preserved, so the task can be published again later.';
15+
16+
static override examples = [
17+
{
18+
description: 'Unpublish a task by name.',
19+
command: 'apify task unpublish my-task',
20+
},
21+
{
22+
description: 'Unpublish a task by full ID.',
23+
command: 'apify task unpublish E2jjCZBezvAZnX8Rb',
24+
},
25+
];
26+
27+
static override docsUrl = 'https://docs.apify.com/cli/docs/reference#apify-task-unpublish';
28+
29+
static override args = {
30+
taskId: Args.string({
31+
required: true,
32+
description: 'Name or ID of the Task to unpublish (e.g. "my-task" or "E2jjCZBezvAZnX8Rb").',
33+
}),
34+
};
35+
36+
async run() {
37+
const apifyClient = await getLoggedClientOrThrow();
38+
const userInfo = await getLocalUserInfo();
39+
const usernameOrId = userInfo.username || (userInfo.id as string);
40+
41+
const { taskId } = this.args;
42+
const idOrName = taskId.includes('/') ? taskId : `${usernameOrId}/${taskId.toLowerCase()}`;
43+
const taskClient = apifyClient.task(idOrName);
44+
45+
const task = await taskClient.get();
46+
if (!task) {
47+
error({ message: `Cannot find Task with ID or name '${taskId}' in your account.` });
48+
return;
49+
}
50+
51+
try {
52+
await taskClient.unpublish();
53+
54+
success({
55+
message: `Task ${chalk.yellow(task.name)} has been unpublished.`,
56+
stdout: true,
57+
});
58+
} catch (err) {
59+
const casted = err as ApifyApiError;
60+
61+
error({
62+
message: `Failed to unpublish Task ${chalk.yellow(task.name)}\n ${casted.message || casted}`,
63+
});
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)