@@ -14,22 +14,61 @@ type CLIEvents = {
1414class 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 }
0 commit comments