|
| 1 | +import type { Command } from "commander"; |
| 2 | +import type { CliContext } from "./context.ts"; |
| 3 | +import { pruneEmpty } from "../lib/compact-json.ts"; |
| 4 | +import { resolveChannelId } from "../slack/channels.ts"; |
| 5 | +import { |
| 6 | + getWorkflowSchema, |
| 7 | + listChannelWorkflows, |
| 8 | + previewWorkflow, |
| 9 | + resolveShortcutUrl, |
| 10 | + runWorkflow, |
| 11 | +} from "../slack/workflows.ts"; |
| 12 | + |
| 13 | +type WorkspaceOption = { |
| 14 | + workspace?: string; |
| 15 | +}; |
| 16 | + |
| 17 | +export function registerWorkflowCommand(input: { program: Command; ctx: CliContext }): void { |
| 18 | + const workflowCmd = input.program |
| 19 | + .command("workflow") |
| 20 | + .description("Discover and interact with Slack workflows"); |
| 21 | + |
| 22 | + workflowCmd |
| 23 | + .command("list") |
| 24 | + .description("List workflows bookmarked or featured in a channel") |
| 25 | + .argument("<channel>", "Channel id or name (#channel, channel, C...)") |
| 26 | + .option( |
| 27 | + "--workspace <url>", |
| 28 | + "Workspace selector (full URL or unique substring; required if you have multiple workspaces)", |
| 29 | + ) |
| 30 | + .action(async (...args) => { |
| 31 | + const [channel, options] = args as [string, WorkspaceOption]; |
| 32 | + try { |
| 33 | + const workspaceUrl = input.ctx.effectiveWorkspaceUrl(options.workspace); |
| 34 | + const payload = await input.ctx.withAutoRefresh({ |
| 35 | + workspaceUrl, |
| 36 | + work: async () => { |
| 37 | + const { client } = await input.ctx.getClientForWorkspace(workspaceUrl); |
| 38 | + const channelId = await resolveChannelId(client, channel); |
| 39 | + return await listChannelWorkflows(client, channelId); |
| 40 | + }, |
| 41 | + }); |
| 42 | + console.log(JSON.stringify(pruneEmpty(payload), null, 2)); |
| 43 | + } catch (err: unknown) { |
| 44 | + console.error(input.ctx.errorMessage(err)); |
| 45 | + process.exitCode = 1; |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + workflowCmd |
| 50 | + .command("preview") |
| 51 | + .description("Get workflow metadata from a trigger ID (no side effects)") |
| 52 | + .argument("<trigger-id>", "Trigger ID (Ft...)") |
| 53 | + .option( |
| 54 | + "--workspace <url>", |
| 55 | + "Workspace selector (full URL or unique substring; required if you have multiple workspaces)", |
| 56 | + ) |
| 57 | + .action(async (...args) => { |
| 58 | + const [triggerId, options] = args as [string, WorkspaceOption]; |
| 59 | + try { |
| 60 | + const workspaceUrl = input.ctx.effectiveWorkspaceUrl(options.workspace); |
| 61 | + const payload = await input.ctx.withAutoRefresh({ |
| 62 | + workspaceUrl, |
| 63 | + work: async () => { |
| 64 | + const { client } = await input.ctx.getClientForWorkspace(workspaceUrl); |
| 65 | + return await previewWorkflow(client, triggerId); |
| 66 | + }, |
| 67 | + }); |
| 68 | + console.log(JSON.stringify(pruneEmpty(payload), null, 2)); |
| 69 | + } catch (err: unknown) { |
| 70 | + console.error(input.ctx.errorMessage(err)); |
| 71 | + process.exitCode = 1; |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + workflowCmd |
| 76 | + .command("get") |
| 77 | + .description("Get workflow definition including form fields and steps (accepts Ft... or Wf...)") |
| 78 | + .argument("<id>", "Trigger ID (Ft...) or Workflow ID (Wf...)") |
| 79 | + .option( |
| 80 | + "--workspace <url>", |
| 81 | + "Workspace selector (full URL or unique substring; required if you have multiple workspaces)", |
| 82 | + ) |
| 83 | + .action(async (...args) => { |
| 84 | + const [id, options] = args as [string, WorkspaceOption]; |
| 85 | + try { |
| 86 | + const workspaceUrl = input.ctx.effectiveWorkspaceUrl(options.workspace); |
| 87 | + const payload = await input.ctx.withAutoRefresh({ |
| 88 | + workspaceUrl, |
| 89 | + work: async () => { |
| 90 | + const { client } = await input.ctx.getClientForWorkspace(workspaceUrl); |
| 91 | + let workflowId = id; |
| 92 | + if (id.startsWith("Ft")) { |
| 93 | + const preview = await previewWorkflow(client, id); |
| 94 | + workflowId = preview.workflow.id; |
| 95 | + } |
| 96 | + return await getWorkflowSchema(client, workflowId); |
| 97 | + }, |
| 98 | + }); |
| 99 | + console.log(JSON.stringify(pruneEmpty(payload), null, 2)); |
| 100 | + } catch (err: unknown) { |
| 101 | + console.error(input.ctx.errorMessage(err)); |
| 102 | + process.exitCode = 1; |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + workflowCmd |
| 107 | + .command("run") |
| 108 | + .description("Trip a workflow trigger") |
| 109 | + .argument("<trigger-id>", "Trigger ID (Ft...)") |
| 110 | + .requiredOption("--channel <id-or-name>", "Channel where the workflow is bookmarked") |
| 111 | + .option( |
| 112 | + "--workspace <url>", |
| 113 | + "Workspace selector (full URL or unique substring; required if you have multiple workspaces)", |
| 114 | + ) |
| 115 | + .action(async (...args) => { |
| 116 | + const [triggerId, options] = args as [string, WorkspaceOption & { channel: string }]; |
| 117 | + try { |
| 118 | + const workspaceUrl = input.ctx.effectiveWorkspaceUrl(options.workspace); |
| 119 | + const payload = await input.ctx.withAutoRefresh({ |
| 120 | + workspaceUrl, |
| 121 | + work: async () => { |
| 122 | + const { client } = await input.ctx.getClientForWorkspace(workspaceUrl); |
| 123 | + const channelId = await resolveChannelId(client, options.channel); |
| 124 | + const shortcutUrl = await resolveShortcutUrl(client, { channelId, triggerId }); |
| 125 | + return await runWorkflow(client, { shortcutUrl, channelId, triggerId }); |
| 126 | + }, |
| 127 | + }); |
| 128 | + console.log(JSON.stringify(pruneEmpty(payload), null, 2)); |
| 129 | + } catch (err: unknown) { |
| 130 | + console.error(input.ctx.errorMessage(err)); |
| 131 | + process.exitCode = 1; |
| 132 | + } |
| 133 | + }); |
| 134 | +} |
0 commit comments