import { confirm, log } from "@clack/prompts" import { defineCommand } from "../lib/command" import { globalOptions, globalOptionsSchema } from "../lib/global-options" import { z } from "zod" import { apiClient } from "../lib/api-client" import { accent, dimmed } from "../lib/io" import { clearSpinner, startSpinner } from "../lib/spinner" import { AUTOMATION_OPT, automationOptsSchema, output, PROJECT_OPT, requireSession, safelyPrompt, sessionIntro, useAutomation, PROJECT_OPTION_DESCRIPTIONS, AUTOMATION_OPTION_DESCRIPTIONS, } from "../utils" const DISABLE_AUTOMATION_OPTS_SCHEMA = automationOptsSchema.extend({ yes: z.boolean().default(false), }) export const automationEnableCommand = defineCommand({ name: "enable", description: "Enable an automation", options: { ...globalOptions, ...AUTOMATION_OPT, ...PROJECT_OPT, }, optionDescriptions: { ...AUTOMATION_OPTION_DESCRIPTIONS, ...PROJECT_OPTION_DESCRIPTIONS, }, schema: globalOptionsSchema.extend(automationOptsSchema.shape), run: (opts) => setAutomationEnabled(opts, true), }) export const automationDisableCommand = defineCommand({ name: "disable", description: "Disable an automation", options: { ...globalOptions, ...AUTOMATION_OPT, ...PROJECT_OPT, yes: { type: "boolean", short: "y", default: false, }, }, optionDescriptions: { ...AUTOMATION_OPTION_DESCRIPTIONS, ...PROJECT_OPTION_DESCRIPTIONS, yes: "Skip confirmation prompt", }, schema: globalOptionsSchema.extend(DISABLE_AUTOMATION_OPTS_SCHEMA.shape), run: (opts) => setAutomationEnabled(opts, false), }) /** * Enables or disables one active automation. * * @param opts - Automation selector and optional confirmation bypass. * @param enabled - Desired trigger acceptance state. */ async function setAutomationEnabled( opts: z.infer & { yes?: boolean }, enabled: boolean, ) { const session = await requireSession( `${enabled ? "enable" : "disable"} an automation`, ) output.normal(() => sessionIntro(session)) const automation = await useAutomation(opts) if ( !enabled && !opts.yes && !(await safelyPrompt( () => confirm({ message: `Disable ${accent(automation.description)}? New trigger events will not start it.`, initialValue: false, }), "--yes", )) ) { output.json({ cancelled: true }) return } startSpinner(`${enabled ? "Enabling" : "Disabling"} automation`) // Finish the mutation before clearing the spinner and rendering output. const result = await apiClient.automation.update({ automation: automation.id, enabled, }) clearSpinner() output .normal(() => log.success( `${enabled ? "Enabled" : "Disabled"} ${accent(automation.description)} ${dimmed(automation.id)}`, ), ) .json({ automationId: automation.id, enabled, txid: result.txid }) }