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