-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
115 lines (88 loc) · 2.85 KB
/
index.ts
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
import glob from "glob";
import fs from "fs";
import { COMPRESSION_LEVEL, zip } from "zip-a-folder";
import axios from "axios";
import FormData from "form-data";
import Configstore from "configstore";
import prompt from "prompts";
import chalk from "chalk";
import packageJson from "./package.json";
import * as process from "node:process";
const appConfig = require("require-module")("./kokateam-deploy-config.json");
const vault = new Configstore(packageJson.name, {});
const generateError = (message: string) => {
throw new Error(message);
};
const start = async () => {
if (!appConfig) generateError("❌ Create kokateam-deploy-config.json first!");
if (!appConfig.app_id)
generateError("❌ Enter a valid app_id in kokateam-deploy-config.json!");
let access_token: string = vault.get("access_token");
if (process.env.KOKATEAM_DEPLOY_TOKEN)
access_token = process.env.KOKATEAM_DEPLOY_TOKEN;
if (!access_token) {
const promptRequest = await prompt({
type: "text",
name: "token",
message: chalk.magenta(
"🔑 Please, enter your token (usually starts with KOKA): "
),
});
if (promptRequest.token) access_token = promptRequest.token;
vault.set("access_token", access_token);
}
if (!fs.existsSync("./" + appConfig.static_path))
generateError(
"📦 Build your project first (or change static_path in config)"
);
const excludedFiles: string[] = glob.sync(
"./" + appConfig.static_path + "/**/*.txt"
);
excludedFiles.forEach((file: string) => fs.rmSync(file));
await zip("./" + appConfig.static_path, "app.zip", {
compression: COMPRESSION_LEVEL.high,
});
const formData = new FormData();
formData.append("app", fs.createReadStream("app.zip"));
formData.append(
"app_id",
process.env.KOKATEAM_DEPLOY_APP_ID || String(appConfig.app_id)
);
try {
const uploadAction = await axios.post(
`https://deploy-backend-production.koka.team/upload`,
formData,
{
headers: {
"Content-Type": "multipart/form-data",
Authorization: "Bearer " + access_token,
},
}
);
if (uploadAction.data.errorCode) {
switch (uploadAction.data.errorCode) {
case 2:
generateError("❌ Bad request!");
break;
case 5:
generateError("❌ Bad app_id!");
break;
case 0:
vault.set("access_token", access_token);
generateError("❌ Access denied!");
break;
default:
generateError(`❌ Error caught! Code: ${uploadAction.data.message}`);
break;
}
return;
}
if (uploadAction.data.data.url) {
console.log(`\n✅ Deployed to ${uploadAction.data.data.url}`);
fs.rmSync("app.zip");
}
} catch (err: any) {
console.error(err?.response?.data || err);
}
};
export default start;