@@ -12,115 +12,132 @@ import {
1212import { handleResponse } from "../../utils/api" ;
1313import { BASE_URL , getApiKey } from "../utils/constants" ;
1414
15- export type PreviewHostsCommandArgs = {
16- list ?: boolean ;
17- add ?: string ;
18- remove ?: string ;
19- clear ?: boolean ;
20- } ;
21-
22- export const previewHostsCommand : yargs . CommandModule <
23- Record < string , never > ,
24- PreviewHostsCommandArgs
25- > = {
15+ export const previewHostsCommand : yargs . CommandModule = {
2616 command : "preview-hosts" ,
2717 describe :
28- "Manage preview hosts for your workspace. This allows you to access use the preview API from trusted hosts." ,
29- builder : ( yargs : yargs . Argv ) =>
30- yargs
31- . option ( "list" , {
18+ "Manage preview hosts that should be able to access the Preview API" ,
19+ builder : ( yargs ) => {
20+ return yargs
21+ . command ( {
22+ command : "list" ,
3223 describe : "List current preview hosts" ,
33- type : "boolean" ,
34- } )
35- . option ( "add" , {
36- describe : "Add a preview host, ex. " ,
37- type : "string" ,
24+ handler : async ( ) => {
25+ const API_KEY = getApiKey ( ) ;
26+ const apiClient : Client = createClient (
27+ createConfig ( {
28+ baseUrl : BASE_URL ,
29+ headers : {
30+ Authorization : `Bearer ${ API_KEY } ` ,
31+ } ,
32+ } )
33+ ) ;
34+ const resp = await previewHostList ( { client : apiClient } ) ;
35+ const data = handleResponse ( resp , "Failed to list preview hosts" ) ;
36+ const hosts = data . preview_hosts . map ( ( { host } ) => host ) ;
37+ if ( hosts . length ) {
38+ console . log ( hosts . join ( "\n" ) ) ;
39+ } else {
40+ console . log ( "No preview hosts found" ) ;
41+ }
42+ } ,
3843 } )
39- . option ( "clear" , {
40- describe : "Clear all preview hosts" ,
41- type : "boolean" ,
44+ . command ( {
45+ command : "add <host>" ,
46+ describe : "Add a preview host" ,
47+ builder : ( yargs ) =>
48+ yargs . positional ( "host" , {
49+ describe : "Host to add" ,
50+ type : "string" ,
51+ demandOption : true ,
52+ } ) ,
53+ handler : async ( argv ) => {
54+ const API_KEY = getApiKey ( ) ;
55+ const apiClient : Client = createClient (
56+ createConfig ( {
57+ baseUrl : BASE_URL ,
58+ headers : {
59+ Authorization : `Bearer ${ API_KEY } ` ,
60+ } ,
61+ } )
62+ ) ;
63+ const resp = await previewHostList ( { client : apiClient } ) ;
64+ const data = handleResponse ( resp , "Failed to list preview hosts" ) ;
65+ let hosts = data . preview_hosts . map ( ( { host } ) => host ) ;
66+ const hostToAdd = ( argv . host as string ) . trim ( ) ;
67+ if ( hosts . includes ( hostToAdd ) ) {
68+ console . log ( `Host already exists: ${ hostToAdd } ` ) ;
69+ return ;
70+ }
71+ hosts . push ( hostToAdd ) ;
72+ await previewHostUpdate ( {
73+ client : apiClient ,
74+ body : { hosts } ,
75+ } ) ;
76+ console . log ( `Added preview host: ${ hostToAdd } ` ) ;
77+ } ,
4278 } )
43- . option ( "remove" , {
79+ . command ( {
80+ command : "remove <host>" ,
4481 describe : "Remove a preview host" ,
45- type : "string" ,
46- } ) ,
47- handler : async ( argv ) => {
48- // Only allow one operation at a time
49- const ops = [ argv . list , argv . add , argv . remove , argv . clear ] . filter ( Boolean ) ;
50- if ( ops . length !== 1 ) {
51- throw new Error (
52- "Please specify exactly one operation: --list, --add, --remove, or --clear"
53- ) ;
54- }
55-
56- const API_KEY = getApiKey ( ) ;
57- const apiClient : Client = createClient (
58- createConfig ( {
59- baseUrl : BASE_URL ,
60- headers : {
61- Authorization : `Bearer ${ API_KEY } ` ,
82+ builder : ( yargs ) =>
83+ yargs . positional ( "host" , {
84+ describe : "Host to remove" ,
85+ type : "string" ,
86+ demandOption : true ,
87+ } ) ,
88+ handler : async ( argv ) => {
89+ const API_KEY = getApiKey ( ) ;
90+ const apiClient : Client = createClient (
91+ createConfig ( {
92+ baseUrl : BASE_URL ,
93+ headers : {
94+ Authorization : `Bearer ${ API_KEY } ` ,
95+ } ,
96+ } )
97+ ) ;
98+ const resp = await previewHostList ( { client : apiClient } ) ;
99+ const data = handleResponse ( resp , "Failed to list preview hosts" ) ;
100+ let hosts = data . preview_hosts . map ( ( { host } ) => host ) ;
101+ const hostToRemove = ( argv . host as string ) . trim ( ) ;
102+ if ( ! hosts . includes ( hostToRemove ) ) {
103+ console . log ( `Host not found: ${ hostToRemove } ` ) ;
104+ return ;
105+ }
106+ hosts = hosts . filter ( ( h ) => h !== hostToRemove ) ;
107+ await previewHostUpdate ( {
108+ client : apiClient ,
109+ body : { hosts } ,
110+ } ) ;
111+ console . log ( `Removed preview host: ${ hostToRemove } ` ) ;
62112 } ,
63113 } )
64- ) ;
65-
66- if ( argv . list ) {
67- const resp = await previewHostList ( { client : apiClient } ) ;
68- const data = handleResponse ( resp , "Failed to list preview hosts" ) ;
69- const hosts = data . preview_hosts . map ( ( { host } ) => host ) ;
70- if ( hosts . length ) {
71- console . log ( hosts . join ( "\n" ) ) ;
72- } else {
73- console . log ( "No preview hosts found" ) ;
74- }
75- return ;
76- }
77-
78- // For add, remove, clear: always work with the full list
79- const resp = await previewHostList ( { client : apiClient } ) ;
80- const data = handleResponse ( resp , "Failed to list preview hosts" ) ;
81- let hosts = data . preview_hosts . map ( ( { host } ) => host ) ;
82-
83- if ( argv . add ) {
84- const hostToAdd = argv . add . trim ( ) ;
85- if ( hosts . includes ( hostToAdd ) ) {
86- console . log ( `Host already exists: ${ hostToAdd } ` ) ;
87- return ;
88- }
89- hosts . push ( hostToAdd ) ;
90- await previewHostUpdate ( {
91- client : apiClient ,
92- body : { hosts } ,
93- } ) ;
94- console . log ( `Added preview host: ${ hostToAdd } ` ) ;
95- return ;
96- }
97-
98- if ( argv . remove ) {
99- const hostToRemove = argv . remove . trim ( ) ;
100- if ( ! hosts . includes ( hostToRemove ) ) {
101- console . log ( `Host not found: ${ hostToRemove } ` ) ;
102- return ;
103- }
104- hosts = hosts . filter ( ( h ) => h !== hostToRemove ) ;
105- await previewHostUpdate ( {
106- client : apiClient ,
107- body : { hosts } ,
108- } ) ;
109- console . log ( `Removed preview host: ${ hostToRemove } ` ) ;
110- return ;
111- }
112-
113- if ( argv . clear ) {
114- if ( hosts . length === 0 ) {
115- console . log ( "Preview host list is already empty." ) ;
116- return ;
117- }
118- await previewHostUpdate ( {
119- client : apiClient ,
120- body : { hosts : [ ] } ,
114+ . command ( {
115+ command : "clear" ,
116+ describe : "Clear all preview hosts" ,
117+ handler : async ( ) => {
118+ const API_KEY = getApiKey ( ) ;
119+ const apiClient : Client = createClient (
120+ createConfig ( {
121+ baseUrl : BASE_URL ,
122+ headers : {
123+ Authorization : `Bearer ${ API_KEY } ` ,
124+ } ,
125+ } )
126+ ) ;
127+ const resp = await previewHostList ( { client : apiClient } ) ;
128+ const data = handleResponse ( resp , "Failed to list preview hosts" ) ;
129+ const hosts = data . preview_hosts . map ( ( { host } ) => host ) ;
130+ if ( hosts . length === 0 ) {
131+ console . log ( "Preview host list is already empty." ) ;
132+ return ;
133+ }
134+ await previewHostUpdate ( {
135+ client : apiClient ,
136+ body : { hosts : [ ] } ,
137+ } ) ;
138+ console . log ( "Cleared all preview hosts." ) ;
139+ } ,
121140 } ) ;
122- console . log ( "Cleared all preview hosts." ) ;
123- return ;
124- }
125141 } ,
142+ handler : ( ) => { } ,
126143} ;
0 commit comments