1+ import { RESTGetApiAppAllResult , RESTPutApiAppAptResult , Routes } from "@discloudapp/api-types/v2" ;
2+ import { GluegunCommand , GluegunToolbox } from "gluegun" ;
3+ import { exit } from "node:process" ;
4+ import { apidiscloud , config , RateLimit } from "../util" ;
5+ import { Apt } from "../util/constants" ;
6+
7+ export default new class AppApt implements GluegunCommand {
8+ name = "app:apt" ;
9+ description = "Install or uninstall apt packages for you application." ;
10+
11+ async run ( toolbox : GluegunToolbox ) {
12+ const { parameters, print, prompt } = toolbox ;
13+
14+ if ( ! config . data . token )
15+ return print . error ( "Please use login command before using this command." ) ;
16+
17+ if ( RateLimit . isLimited )
18+ return print . error ( `Rate limited until: ${ RateLimit . limited } ` ) ;
19+
20+ if ( ! parameters . first ) {
21+ const spin = print . spin ( {
22+ text : print . colors . cyan ( "Fetching apps..." ) ,
23+ } ) ;
24+
25+ const apiRes = await apidiscloud . get < RESTGetApiAppAllResult > ( Routes . app ( "all" ) ) ;
26+
27+ spin . stop ( ) ;
28+
29+ if ( apiRes . data )
30+ if ( "apps" in apiRes . data ) {
31+ const { appId } = await prompt . ask ( {
32+ name : "appId" ,
33+ message : "Choose the app" ,
34+ type : "select" ,
35+ choices : [ {
36+ name : "all" ,
37+ message : "All apps" ,
38+ value : "all" ,
39+ } ] . concat ( apiRes . data . apps . map ( app => ( {
40+ name : app . id ,
41+ message : `${ app . name } - ${ app . id } - ${ app . online ?
42+ print . colors . green ( "online" ) :
43+ print . colors . red ( "offline" ) } `,
44+ value : app . id ,
45+ } ) ) ) ,
46+ } ) ;
47+
48+ parameters . first = appId ;
49+ }
50+
51+ if ( ! parameters . first )
52+ return print . error ( "Need app id." ) ;
53+ }
54+
55+ let method : "delete" | "put" | undefined ;
56+
57+ if ( Object . keys ( parameters . options ) . length )
58+ method =
59+ ( parameters . options . u ?? parameters . options . uninstall ) ? "delete" :
60+ ( parameters . options . i ?? parameters . options . install ) ? "put" :
61+ method ;
62+
63+ if ( ! method || ! Object . keys ( Apt ) . includes ( parameters . options [ method ] ) )
64+ return print . error (
65+ "You need to use one of the options below:" +
66+ "\n -i, --install [PACKAGE] Install a package." +
67+ "\n -u, --uninstall [PACKAGE] Uninstall a package." + "\n" +
68+ "\nPACKAGES:" + "\n" +
69+ Object . keys ( Apt ) . map ( pkg => ` - ${ pkg } ${ Apt [ < "tools" > pkg ] } ` ) . join ( "\n" ) ,
70+ ) ;
71+
72+ const apt = parameters . options [ method ] ;
73+
74+ let action ;
75+ switch ( method ) {
76+ case "delete" :
77+ action = `Uninstalling ${ apt } from ${ parameters . first } app...` ;
78+ break ;
79+ case "put" :
80+ action = `Installing ${ apt } for ${ parameters . first } app...` ;
81+ break ;
82+ default :
83+ action = "" ;
84+ break ;
85+ }
86+
87+ const spin = print . spin ( {
88+ text : print . colors . cyan ( action ) ,
89+ } ) ;
90+
91+ const apiRes = await apidiscloud [ method ] <
92+ RESTPutApiAppAptResult
93+ > ( Routes . appApt ( parameters . first ) ,
94+ [ "delete" , "put" ] . includes ( method ) ? {
95+ apt,
96+ } : undefined ) ;
97+
98+ new RateLimit ( apiRes . headers ) ;
99+
100+ if ( apiRes . status )
101+ if ( print . spinApiRes ( apiRes , spin ) > 399 ) return exit ( apiRes . status ) ;
102+
103+ exit ( 0 ) ;
104+ }
105+ } ;
0 commit comments