-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
149 lines (126 loc) · 4.51 KB
/
update.js
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
require("dotenv").config();
const { exec, spawn } = require("child_process");
const path = require("path");
const fs = require("fs");
const currentDir = __dirname;
const envFilePath = path.join(currentDir, ".env");
const tempEnvFilePath = path.join(currentDir, ".env.temp");
// Configuration
const useDebugMode = process.env.USE_DEBUG_MODE === "1";
console.log(
`USE_DEBUG_MODE: ${process.env.USE_DEBUG_MODE}, useDebugMode: ${useDebugMode}`
);
const repoUrl =
"https://github.com/pastelnetwork/pastel_inference_js_client.git";
const branch = "master"; // Changed from "main" to "master"
function updateApplication() {
if (useDebugMode) {
console.log("Debug mode is enabled. Skipping auto-update.");
startApplication();
return;
}
console.log("Checking for updates...");
// Move .env file to a temporary location
if (fs.existsSync(envFilePath)) {
fs.renameSync(envFilePath, tempEnvFilePath);
}
exec(
`git fetch origin ${branch}`,
{ cwd: currentDir },
(err, stdout, stderr) => {
if (err) {
console.error("Error fetching updates:", stderr);
// Restore .env file if an error occurs
if (fs.existsSync(tempEnvFilePath)) {
fs.renameSync(tempEnvFilePath, envFilePath);
}
return;
}
exec("git rev-parse HEAD", { cwd: currentDir }, (err, currentCommit) => {
if (err) {
console.error("Error getting current commit:", stderr);
// Restore .env file if an error occurs
if (fs.existsSync(tempEnvFilePath)) {
fs.renameSync(tempEnvFilePath, envFilePath);
}
return;
}
exec(
`git rev-parse origin/${branch}`,
{ cwd: currentDir },
(err, latestCommit) => {
if (err) {
console.error("Error getting latest commit:", stderr);
// Restore .env file if an error occurs
if (fs.existsSync(tempEnvFilePath)) {
fs.renameSync(tempEnvFilePath, envFilePath);
}
return;
}
if (currentCommit.trim() !== latestCommit.trim()) {
console.log("Update available. Pulling latest changes...");
exec(
`git pull origin ${branch}`,
{ cwd: currentDir },
(err, stdout, stderr) => {
if (err) {
console.error("Error pulling updates:", stderr);
// Restore .env file if an error occurs
if (fs.existsSync(tempEnvFilePath)) {
fs.renameSync(tempEnvFilePath, envFilePath);
}
return;
}
console.log("Installing dependencies...");
exec(
"npm install",
{ cwd: currentDir },
(err, stdout, stderr) => {
if (err) {
console.error("Error installing dependencies:", stderr);
// Restore .env file if an error occurs
if (fs.existsSync(tempEnvFilePath)) {
fs.renameSync(tempEnvFilePath, envFilePath);
}
return;
}
console.log(
"Update applied successfully. Restarting application..."
);
// Restore .env file after successful update
if (fs.existsSync(tempEnvFilePath)) {
fs.renameSync(tempEnvFilePath, envFilePath);
}
process.exit(0); // Exit to let the process manager (e.g., PM2) restart the app
}
);
}
);
} else {
console.log("Application is up-to-date.");
// Restore .env file if no update is needed
if (fs.existsSync(tempEnvFilePath)) {
fs.renameSync(tempEnvFilePath, envFilePath);
}
startApplication();
}
}
);
});
}
);
}
function startApplication() {
console.log("Starting application...");
const server = spawn("node", ["server.js"], {
cwd: currentDir,
stdio: "inherit",
});
server.on("close", (code) => {
console.log(`server.js process exited with code ${code}`);
});
server.on("error", (err) => {
console.error("Error starting application:", err);
});
}
updateApplication();