-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcheckConfig.mjs
127 lines (110 loc) · 3.29 KB
/
checkConfig.mjs
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
import fetch from "node-fetch";
import dotenv from "dotenv";
import { io } from "socket.io-client";
import fs from "fs";
dotenv.config();
dotenv.config({ path: "backend/.env" });
const siteId = process.env.VITE_RICOCHET_SITEID;
const socketURL = process.env.VITE_SOCKET_URL;
const socketPath = process.env.VITE_SOCKET_PATH || "/socket.io";
const apiEndpoint = process.env.VITE_API_ENDPOINT;
const fileStore = process.env.FILE_STORE_BACKEND;
const jsonStore = process.env.JSON_STORE_BACKEND;
const check = async () => {
if (!siteId) {
console.log(
"🚨 You must define a VITE_RICOCHET_SITEID environment variable."
);
process.exit(1);
}
try {
const result = await fetch(`${apiEndpoint}/${siteId}/`);
// console.log(result);
if (result.status !== 400) {
console.log(
"🚨 The Ricochet.js server doesn't respond correctly. " +
`Have you started it? Tested url: ${apiEndpoint}`
);
return;
}
const data = await result.json();
if (!data.message.includes("X-Ricochet-Origin")) {
console.log(
"🚨 The Ricochet.js server doesn't respond correctly. " +
`Have you started it? Tested url: ${apiEndpoint}`
);
return;
}
const patch = await fetch(`${apiEndpoint}/_register/${siteId}`, {
method: "PATCH",
});
if (patch.status === 404) {
console.log(
`🚨 The '${siteId}' doesn't seem to exists on Ricochet.js. ` +
"Have you registered it?"
);
return;
}
if (patch.status === 400) {
console.log(
`✅ Ricochet.js is running and site ${siteId} seems to be registered.`
);
}
} catch (e) {
if (e.code === "ECONNREFUSED") {
console.log(
"🚨 The Ricochet.js server doesn't seem to be up and running. " +
`Have you started it? Tested url: ${apiEndpoint}`
);
return;
}
}
if (!fs.existsSync("./public/ricochet.json")) {
console.log(
"🚨 The './public/ricochet.json' file is missing. " +
"Have you generated it? \nHint: from backend/ dir execute `npm run watch`"
);
return;
}
if (jsonStore === "memory") {
console.log(
"⚠️ The Ricochet.js JSON store is in memory. " +
"Remember that you'll lost all changes and registered sites each time you stop the server."
);
}
if (fileStore === "memory") {
console.log(
"⚠️ The Ricochet.js File store is in memory. " +
"Remember that you'll lost all files each time you stop the server."
);
}
const testConn = new Promise((resolve, reject) => {
const socket = io(socketURL, {
transports: ["websocket"],
path: socketPath,
});
const out = setTimeout(() => {
reject("failed");
socket.disconnect();
}, 5000);
socket.on("connect", () => {
resolve("ok");
clearTimeout(out);
socket.disconnect();
});
});
try {
await testConn;
console.log("✅ Wire.io server is up and running.");
} catch (e) {
console.log(
"🚨 The Wire.io server hasn't responded in 5s. " +
`Have you started it? Tested url: ${socketURL}${socketPath}`
);
return;
}
console.log(
"\n👏 Congrats, everything works fine!\n\nDo you still have an issue? Go to discord channel for more help."
);
};
check();