-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase.ts
More file actions
220 lines (190 loc) · 5.91 KB
/
base.ts
File metadata and controls
220 lines (190 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import type {
APIUserApplication,
ApplicationLanguage,
} from "@squarecloud/api-types/v2";
import { readFile } from "fs/promises";
import type { SquareCloudAPI } from "@/index";
import { assertPathLike, assertString } from "@/assertions/literal";
import { Routes } from "@/lib/routes";
import { DeploysModule, FilesModule, SnapshotsModule } from "@/modules";
import { ApplicationCacheService } from "@/services";
import { ApplicationStatus } from "@/structures";
import type { Application } from "./application";
/**
* Represents the base application from the user endpoint
*/
export class BaseApplication {
/** The application ID */
public readonly id: string;
/** The application display name */
public name: string;
/** The application description */
public description?: string;
/** The url to manage the application via web */
public url: string;
/** The application total ram */
public ram: number;
/** The application current cluster */
public cluster: string;
/** Date the application was created */
public createdAt: Date;
/**
* The application programming language
*
* - `javascript`
* - `typescript`
* - `python`
* - `java`
* - `elixir`
* - `rust`
* - `go`
* - `php`
* - `dotnet`
* - `static`
*/
public language: ApplicationLanguage;
/** Cache service for this application */
public readonly cache = new ApplicationCacheService();
/** Files module for this application */
public readonly files = new FilesModule(this);
/** Snapshots module for this application */
public readonly snapshots = new SnapshotsModule(this);
/** Deploys module for this application */
public readonly deploys = new DeploysModule(this);
/**
* Represents the base application from the user endpoint
*
* @constructor
* @param client - The client for this application
* @param data - The data from this application
*/
constructor(
public readonly client: SquareCloudAPI,
data: APIUserApplication,
) {
const { id, name, desc, ram, lang, cluster, created_at } = data;
this.id = id;
this.name = name;
this.description = desc;
this.ram = ram;
this.language = lang;
this.cluster = cluster;
this.createdAt = new Date(created_at);
this.url = `https://squarecloud.app/dashboard/app/${id}`;
}
/** @deprecated Use `Application#snapshots` instead */
get backup() {
console.warn(
"[SquareCloudAPI] The 'backup' property is deprecated and will be removed in the next major version. Use Application#snapshots instead.",
);
return this.snapshots;
}
/** @deprecated Use `Application#snapshots` instead */
get backups() {
console.warn(
"[SquareCloudAPI] The 'backups' property is deprecated and will be removed in the next major version. Use Application#snapshots instead.",
);
return this.snapshots;
}
/**
* Fetches this application for full information
*/
async fetch(): Promise<Application> {
return this.client.applications.fetch(this.id);
}
/**
* Gets the application current status information
*/
async getStatus(): Promise<ApplicationStatus> {
const data = await this.client.api.request(Routes.apps.status(this.id));
const status = new ApplicationStatus(this.client, data.response, this.id);
this.client.emit("statusUpdate", this, this.cache.status, status);
this.cache.set("status", status);
return status;
}
/**
* Gets the application current logs
*/
async getLogs(): Promise<string> {
const data = await this.client.api.request(Routes.apps.logs(this.id));
const { logs } = data.response;
this.client.emit("logsUpdate", this, this.cache.logs, logs);
this.cache.set("logs", logs);
return logs;
}
/**
* Starts up the application
* @returns `boolean` for success or fail
*/
async start(): Promise<boolean> {
const data = await this.client.api.request(Routes.apps.start(this.id), {
method: "POST",
});
return data?.status === "success";
}
/**
* Stops the application
* @returns `boolean` for success or fail
*/
async stop(): Promise<boolean> {
const data = await this.client.api.request(Routes.apps.stop(this.id), {
method: "POST",
});
return data?.status === "success";
}
/**
* Restarts the application
* @returns `boolean` for success or fail
*/
async restart(): Promise<boolean> {
const data = await this.client.api.request(Routes.apps.restart(this.id), {
method: "POST",
});
return data?.status === "success";
}
/**
* Deletes your whole application
* - This action is irreversible.
*
* @returns `boolean` for success or fail
*/
async delete(): Promise<boolean> {
const data = await this.client.api.request(Routes.apps.delete(this.id), {
method: "DELETE",
});
return data?.status === "success";
}
/**
* Commit files to your application folder
*
* - This action is irreversible.
*
* - Tip: use this to get an absolute path.
* ```ts
* require('path').join(__dirname, 'fileName')
* ```
* - Tip 2: use a zip file to commit more than one archive
*
* @param file - Buffer or absolute path to the file
* @param fileName - The file name (e.g.: "index.js")
* @param restart - Whether the application should be restarted after the commit
* @returns `true` for success or `false` for fail
*/
async commit(file: string | Buffer, fileName?: string): Promise<boolean> {
assertPathLike(file, "COMMIT_FILE");
if (fileName) {
assertString(fileName, "FILE_NAME");
}
if (typeof file === "string") {
file = await readFile(file);
}
const formData = new FormData();
const blob = new Blob([new Uint8Array(file)]);
formData.append("file", blob, fileName || "commit.zip");
const data = await this.client.api.request(Routes.apps.commit(this.id), {
method: "POST",
body: formData,
});
return data?.status === "success";
}
}