Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

Commit 172102f

Browse files
committed
Add CLI framework & handlers for connected accounts, action attempts
1 parent 2d93ec4 commit 172102f

File tree

4 files changed

+327
-27
lines changed

4 files changed

+327
-27
lines changed

Diff for: package.json

+9-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
],
1010
"version": "2.0.0",
1111
"license": "MIT",
12+
"sideEffects": false,
1213
"main": "./cjs-wrapper.js",
1314
"module": "./dist/index.mjs",
1415
"types": "./dist/index.d.ts",
@@ -24,7 +25,10 @@
2425
"cjs-wrapper.js"
2526
],
2627
"dependencies": {
27-
"axios": "0.25.0"
28+
"axios": "0.25.0",
29+
"change-case": "4.1.2",
30+
"ora": "5.4.1",
31+
"yargs": "17.3.1"
2832
},
2933
"devDependencies": {
3034
"@semantic-release/commit-analyzer": "9.0.2",
@@ -33,6 +37,7 @@
3337
"@semantic-release/release-notes-generator": "10.0.3",
3438
"@swc/core": "1.2.133",
3539
"@types/node": "17.0.10",
40+
"@types/yargs": "17.0.8",
3641
"ajv": "8.9.0",
3742
"ava": "4.0.1",
3843
"esbuild": "0.14.18",
@@ -44,10 +49,10 @@
4449
"pgknexlove": "1.1.21",
4550
"prettier": "2.5.1",
4651
"testcontainers": "8.2.0",
47-
"ts-json-schema-generator": "0.97.0",
52+
"ts-json-schema-generator": "0.98.0",
4853
"tsup": "5.11.11",
4954
"type-fest": "2.11.1",
50-
"typedoc": "0.22.11",
55+
"typedoc": "0.22.12",
5156
"typedoc-plugin-markdown": "3.11.12",
5257
"typescript": "4.5.5"
5358
},
@@ -58,7 +63,7 @@
5863
"prepare": "husky install",
5964
"prepack": "npm run build",
6065
"build:package": "tsup",
61-
"build:docs": "typedoc --plugin typedoc-plugin-markdown --gitRevision main src/index.ts",
66+
"build:docs": "typedoc",
6267
"build": "npm run build:package && npm run build:docs",
6368
"format": "prettier --write .",
6469
"format:check": "prettier --check .",

Diff for: src/cli.ts

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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()

Diff for: typedoc.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"plugin": "typedoc-plugin-markdown",
3+
"gitRevision": "main",
4+
"entryPoints": "src/index.ts"
5+
}

0 commit comments

Comments
 (0)