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

Commit 1ce8001

Browse files
committed
Wait for command to execute, add intro to help message
1 parent 2de267e commit 1ce8001

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

src/cli/browser.ts

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,61 @@ type CLIEvents = {
1414
class BrowserCLI extends (EventEmitter as unknown as new () => TypedEmitter<CLIEvents>) {
1515
private instance = getCLI(yargsInstance).scriptName("seam")
1616

17+
/**
18+
* Use the Seam CLI in the browser!
19+
*/
1720
constructor(private apiKey?: string) {
1821
super()
1922
this.setUpShims()
2023
}
2124

22-
async parse(...args: Parameters<typeof yargsInstance.parse>) {
25+
/**
26+
* Parse a command line string. Output will be emitted as data events.
27+
* @param input a given command string
28+
* @example
29+
* ```
30+
* const cli = new BrowserCLI()
31+
* await cli.parse("seam --help")
32+
* ```
33+
*/
34+
async parse(input: string) {
2335
this.setUpShims()
2436

25-
const [input] = args
2637
const inputWithKey = input.includes("--api-key")
2738
? input
2839
: `${input} --api-key ${this.apiKey}`
2940

30-
await this.instance.parse(inputWithKey, ...(args.slice(1) as any))
41+
await new Promise<void>((resolve, reject) => {
42+
// .parseAsync isn't available in v16, so we listen for the ending newline instead
43+
const onData = (data: string) => {
44+
if (data === "\n") {
45+
this.removeListener("data", onData)
46+
resolve()
47+
}
48+
}
49+
this.on("data", onData)
50+
51+
this.instance.parse(
52+
inputWithKey,
53+
(error: Error, _argv: any, output?: string) => {
54+
if (error) {
55+
this.removeListener("data", onData)
56+
return reject(error)
57+
}
58+
59+
if (output) {
60+
this.emit("data", output)
61+
this.emit("data", "\n")
62+
}
63+
}
64+
)
65+
})
3166
}
3267

68+
/**
69+
* Set the wrap value of the Yargs instance
70+
* @param columns number of columns to wrap to
71+
*/
3372
setWidth(columns: number) {
3473
this.instance = this.instance.wrap(columns)
3574
}

src/cli/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ export const getCLI = (yargs: Argv) => {
1010
builder = builder.command(command.default as any)
1111
}
1212

13-
builder = builder.demandCommand().strict()
13+
builder = builder
14+
.demandCommand()
15+
.strict()
16+
.usage(
17+
"Control locks, lights and other internet of things devices with Seam's simple API."
18+
)
1419

1520
return builder
1621
}

0 commit comments

Comments
 (0)