-
Notifications
You must be signed in to change notification settings - Fork 56
feat: add task publish and unpublish commands #1317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| 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 { 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 ' + | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Same as on |
||
| '(in Apify Console, on the task Publication tab).'; | ||
|
|
||
| static override examples = [ | ||
| { | ||
| description: 'Publish a task by name.', | ||
| command: 'apify task publish my-task', | ||
| }, | ||
| { | ||
| description: 'Publish a task by full ID.', | ||
| command: 'apify task publish E2jjCZBezvAZnX8Rb', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Important: This example fails — |
||
| }, | ||
| ]; | ||
|
|
||
| static override docsUrl = 'https://docs.apify.com/cli/docs/reference#apify-task-publish'; | ||
|
|
||
| static override args = { | ||
| taskId: Args.string({ | ||
| required: true, | ||
| description: 'Name or ID of the Task to publish (e.g. "my-task" or "E2jjCZBezvAZnX8Rb").', | ||
| }), | ||
| }; | ||
|
|
||
| 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 ID or name '${taskId}' in your account.` }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Important: |
||
| 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({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
| message: `Failed to publish Task ${chalk.yellow(task.name)}\n ${casted.message || casted}`, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| 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 { 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.'; | ||
|
|
||
| static override examples = [ | ||
| { | ||
| description: 'Unpublish a task by name.', | ||
| command: 'apify task unpublish my-task', | ||
| }, | ||
| { | ||
| description: 'Unpublish a task by full ID.', | ||
| command: 'apify task unpublish E2jjCZBezvAZnX8Rb', | ||
| }, | ||
| ]; | ||
|
|
||
| static override docsUrl = 'https://docs.apify.com/cli/docs/reference#apify-task-unpublish'; | ||
|
|
||
| static override args = { | ||
| taskId: Args.string({ | ||
| required: true, | ||
| description: 'Name or ID of the Task to unpublish (e.g. "my-task" or "E2jjCZBezvAZnX8Rb").', | ||
| }), | ||
| }; | ||
|
|
||
| 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 ID or name '${taskId}' in your account.` }); | ||
| 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}`, | ||
| }); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
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
publicConfigset?publishcommands already assumes it is set and valid, right?