|
| 1 | +#!/usr/bin/env node |
| 2 | +import yargs from "yargs" |
| 3 | +import { hideBin } from "yargs/helpers" |
| 4 | +import ora from "ora" |
| 5 | +import { paramCase } from "change-case" |
| 6 | +import Seam, { SeamAPIError } from "." |
| 7 | + |
| 8 | +const parser = yargs(hideBin(process.argv)) |
| 9 | + |
| 10 | +const executeCommand = async ( |
| 11 | + methodName: string, |
| 12 | + args: any[], |
| 13 | + executeArgs: { json?: boolean; quiet?: boolean } |
| 14 | +) => { |
| 15 | + const displaySpinner = !(executeArgs.quiet || executeArgs.json) |
| 16 | + |
| 17 | + const spinner = displaySpinner |
| 18 | + ? ora( |
| 19 | + methodName |
| 20 | + .split(".") |
| 21 | + .map((v) => paramCase(v)) |
| 22 | + .join(".") |
| 23 | + ).start() |
| 24 | + : undefined |
| 25 | + |
| 26 | + const seam = new Seam() |
| 27 | + |
| 28 | + let method: any = seam |
| 29 | + for (const path of methodName.split(".")) { |
| 30 | + method = method[path] |
| 31 | + } |
| 32 | + |
| 33 | + try { |
| 34 | + const result = await method(...args) |
| 35 | + spinner?.succeed() |
| 36 | + |
| 37 | + if (executeArgs.json) { |
| 38 | + console.log(JSON.stringify(result, null, 2)) |
| 39 | + } else { |
| 40 | + console.log(result) |
| 41 | + } |
| 42 | + } catch (error) { |
| 43 | + let message = "Unknown error" |
| 44 | + if (error instanceof SeamAPIError) { |
| 45 | + message = `${error.name} (HTTP ${error.status}): ${error.message}` |
| 46 | + } else if (error instanceof Error) { |
| 47 | + message = error.message |
| 48 | + } |
| 49 | + spinner?.fail(message) |
| 50 | + process.exit(1) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +parser |
| 55 | + .option("api-key", { |
| 56 | + describe: |
| 57 | + "Seam API key (the environment variable SEAM_API_KEY is used if not provided)", |
| 58 | + }) |
| 59 | + .option("quiet", { |
| 60 | + describe: "Hide progress indicators", |
| 61 | + type: "boolean", |
| 62 | + }) |
| 63 | + .option("json", { |
| 64 | + describe: "Output JSON", |
| 65 | + type: "boolean", |
| 66 | + }) |
| 67 | + .command( |
| 68 | + "connected-accounts", |
| 69 | + "interact with connected accounts", |
| 70 | + (yargs) => { |
| 71 | + yargs |
| 72 | + .command( |
| 73 | + "list", |
| 74 | + "list connected accounts", |
| 75 | + () => {}, |
| 76 | + async (argv) => { |
| 77 | + await executeCommand("connectedAccounts.list", [], argv) |
| 78 | + } |
| 79 | + ) |
| 80 | + .command( |
| 81 | + "get <id>", |
| 82 | + "get a connected account", |
| 83 | + (yargs) => { |
| 84 | + return yargs.positional("id", { |
| 85 | + describe: "the connected account ID", |
| 86 | + demandOption: true, |
| 87 | + }) |
| 88 | + }, |
| 89 | + async (argv) => { |
| 90 | + await executeCommand("connectedAccounts.get", [argv.id], argv) |
| 91 | + } |
| 92 | + ) |
| 93 | + } |
| 94 | + ) |
| 95 | + .command("action-attempts", "interact with action attempts", (yargs) => { |
| 96 | + yargs.command( |
| 97 | + "get <id>", |
| 98 | + "get an action attempt", |
| 99 | + (yargs) => { |
| 100 | + return yargs.positional("id", { |
| 101 | + describe: "the action attempt ID", |
| 102 | + demandOption: true, |
| 103 | + }) |
| 104 | + }, |
| 105 | + async (argv) => { |
| 106 | + await executeCommand("actionAttempts.get", [argv.id], argv) |
| 107 | + } |
| 108 | + ) |
| 109 | + }) |
| 110 | + .parse() |
0 commit comments