1- import { SquareCloudAPI } from "@/index" ;
1+ import { assertPathLike , assertString } from "@/assertions/literal" ;
2+ import { ApplicationStatus , SquareCloudAPI } from "@/index" ;
23import {
34 APIUserApplication ,
45 APIWebsiteApplication ,
56 ApplicationLanguage ,
67} from "@squarecloud/api-types/v2" ;
8+ import FormData from "form-data" ;
9+ import { readFile } from "fs/promises" ;
710import { Application } from "./application" ;
811import { WebsiteApplication } from "./website" ;
912
@@ -16,8 +19,8 @@ import { WebsiteApplication } from "./website";
1619 */
1720export class BaseApplication {
1821 id : string ;
19- tag : string ;
20- description ?: string | undefined ;
22+ name : string ;
23+ description ?: string ;
2124 url : string ;
2225 ram : number ;
2326 /**
@@ -34,7 +37,7 @@ export class BaseApplication {
3437 */
3538 language : ApplicationLanguage ;
3639 cluster : string ;
37- isWebsite : boolean ;
40+ private _isWebsite : boolean ;
3841
3942 constructor (
4043 public readonly client : SquareCloudAPI ,
@@ -43,16 +46,16 @@ export class BaseApplication {
4346 const { id, tag, desc, ram, lang, cluster, isWebsite } = data ;
4447
4548 this . id = id ;
46- this . tag = tag ;
49+ this . name = tag ;
4750 this . description = desc ;
4851 this . ram = ram ;
4952 this . language = lang ;
5053 this . cluster = cluster ;
51- this . isWebsite = isWebsite ;
54+ this . _isWebsite = isWebsite ;
5255 this . url = `https://squarecloud.app/dashboard/app/${ id } ` ;
5356 }
5457
55- async fetch ( ) {
58+ async fetch ( ) : Promise < Application > {
5659 const data = await this . client . api . application ( "" , this . id ) ;
5760
5861 if ( data . response . isWebsite ) {
@@ -63,4 +66,132 @@ export class BaseApplication {
6366 }
6467 return new Application ( this . client , data . response ) ;
6568 }
69+
70+ /** @returns The application current status information */
71+ async getStatus ( ) : Promise < ApplicationStatus > {
72+ const data = await this . client . api . application ( "status" , this . id ) ;
73+ const status = new ApplicationStatus ( this . client , data . response , this . id ) ;
74+
75+ return status ;
76+ }
77+
78+ /** @returns The application logs */
79+ async getLogs ( ) : Promise < string > {
80+ const data = await this . client . api . application ( "logs" , this . id ) ;
81+ const { logs } = data . response ;
82+
83+ return logs ;
84+ }
85+
86+ /**
87+ * Starts up the application
88+ * @returns `true` for success or `false` for fail
89+ */
90+ async start ( ) : Promise < boolean > {
91+ const data = await this . client . api . application (
92+ "start" ,
93+ this . id ,
94+ undefined ,
95+ "POST" ,
96+ ) ;
97+
98+ return data ?. status === "success" ;
99+ }
100+
101+ /**
102+ * Stops the application
103+ * @returns `true` for success or `false` for fail
104+ */
105+ async stop ( ) : Promise < boolean > {
106+ const data = await this . client . api . application (
107+ "stop" ,
108+ this . id ,
109+ undefined ,
110+ "POST" ,
111+ ) ;
112+
113+ return data ?. status === "success" ;
114+ }
115+
116+ /**
117+ * Restarts the application
118+ * @returns `true` for success or `false` for fail
119+ */
120+ async restart ( ) : Promise < boolean > {
121+ const data = await this . client . api . application (
122+ "restart" ,
123+ this . id ,
124+ undefined ,
125+ "POST" ,
126+ ) ;
127+
128+ return data ?. status === "success" ;
129+ }
130+
131+ /**
132+ * Deletes your whole application
133+ *
134+ * - This action is irreversible.
135+ * @returns `true` for success or `false` for fail
136+ */
137+ async delete ( ) : Promise < boolean > {
138+ const data = await this . client . api . application (
139+ "delete" ,
140+ this . id ,
141+ undefined ,
142+ "DELETE" ,
143+ ) ;
144+
145+ return data ?. status === "success" ;
146+ }
147+
148+ /**
149+ * Commit files to your application folder
150+ *
151+ * - This action is irreversible.
152+ *
153+ * - Tip: use this to get an absolute path.
154+ * ```ts
155+ * require('path').join(__dirname, 'fileName')
156+ * ```
157+ * - Tip2: use a zip file to commit more than one archive
158+ *
159+ * @param file - Buffer or absolute path to the file
160+ * @param fileName - The file name (e.g.: "index.js")
161+ * @param restart - Whether the application should be restarted after the commit
162+ * @returns `true` for success or `false` for fail
163+ */
164+ async commit (
165+ file : string | Buffer ,
166+ fileName ?: string ,
167+ restart ?: boolean ,
168+ ) : Promise < boolean > {
169+ assertPathLike ( file , "COMMIT_DATA" ) ;
170+
171+ if ( fileName ) {
172+ assertString ( fileName , "FILE_NAME" ) ;
173+ }
174+
175+ if ( typeof file === "string" ) {
176+ file = await readFile ( file ) ;
177+ }
178+
179+ const formData = new FormData ( ) ;
180+ formData . append ( "file" , file , { filename : fileName || "app.zip" } ) ;
181+
182+ const data = await this . client . api . application (
183+ "commit" ,
184+ this . id ,
185+ {
186+ restart : `${ Boolean ( restart ) } ` ,
187+ } ,
188+ {
189+ method : "POST" ,
190+ body : formData . getBuffer ( ) ,
191+ headers : formData . getHeaders ( ) ,
192+ } ,
193+ ) ;
194+
195+ return data ?. status === "success" ;
196+ }
66197}
0 commit comments