@ezetgalaxy/titan 26.8.0 → 26.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -11
- package/package.json +2 -2
- package/templates/js/server/src/main.rs +1 -6
- package/templates/js/titan/bundle.js +1 -1
- package/templates/js/titan/dev.js +282 -194
- package/templates/rust/server/Cargo.toml +12 -0
- package/templates/rust/server/src/main.rs +0 -6
- package/templates/rust/titan/bundle.js +157 -157
- package/templates/rust/titan/dev.js +282 -194
- package/templates/rust/titan/titan.js +122 -122
- package/templates/js/server/src/actions_rust/mod.rs +0 -15
|
@@ -1,194 +1,282 @@
|
|
|
1
|
-
import chokidar from "chokidar";
|
|
2
|
-
import { spawn, execSync } from "child_process";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import { fileURLToPath } from "url";
|
|
5
|
-
import fs from "fs";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
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
|
-
console.
|
|
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
|
-
|
|
1
|
+
import chokidar from "chokidar";
|
|
2
|
+
import { spawn, execSync } from "child_process";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { fileURLToPath } from "url";
|
|
5
|
+
import fs from "fs";
|
|
6
|
+
|
|
7
|
+
// Required for __dirname in ES modules
|
|
8
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
9
|
+
const __dirname = path.dirname(__filename);
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
// Colors
|
|
13
|
+
import { createRequire } from "module";
|
|
14
|
+
|
|
15
|
+
// Colors
|
|
16
|
+
const cyan = (t) => `\x1b[36m${t}\x1b[0m`;
|
|
17
|
+
const green = (t) => `\x1b[32m${t}\x1b[0m`;
|
|
18
|
+
const yellow = (t) => `\x1b[33m${t}\x1b[0m`;
|
|
19
|
+
const red = (t) => `\x1b[31m${t}\x1b[0m`;
|
|
20
|
+
const gray = (t) => `\x1b[90m${t}\x1b[0m`;
|
|
21
|
+
const bold = (t) => `\x1b[1m${t}\x1b[0m`;
|
|
22
|
+
|
|
23
|
+
function getTitanVersion() {
|
|
24
|
+
try {
|
|
25
|
+
const require = createRequire(import.meta.url);
|
|
26
|
+
const pkgPath = require.resolve("@ezetgalaxy/titan/package.json");
|
|
27
|
+
return JSON.parse(fs.readFileSync(pkgPath, "utf-8")).version;
|
|
28
|
+
} catch (e) {
|
|
29
|
+
try {
|
|
30
|
+
// Check levels up to find the framework root
|
|
31
|
+
let cur = __dirname;
|
|
32
|
+
for (let i = 0; i < 5; i++) {
|
|
33
|
+
const pkgPath = path.join(cur, "package.json");
|
|
34
|
+
if (fs.existsSync(pkgPath)) {
|
|
35
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
|
|
36
|
+
if (pkg.name === "@ezetgalaxy/titan") return pkg.version;
|
|
37
|
+
}
|
|
38
|
+
cur = path.join(cur, "..");
|
|
39
|
+
}
|
|
40
|
+
} catch (e2) { }
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
// Fallback to calling tit --version
|
|
44
|
+
const output = execSync("tit --version", { encoding: "utf-8" }).trim();
|
|
45
|
+
const match = output.match(/v(\d+\.\d+\.\d+)/);
|
|
46
|
+
if (match) return match[1];
|
|
47
|
+
} catch (e3) { }
|
|
48
|
+
}
|
|
49
|
+
return "0.1.0";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let serverProcess = null;
|
|
53
|
+
let isKilling = false;
|
|
54
|
+
let isFirstBoot = true;
|
|
55
|
+
|
|
56
|
+
// ... (killServer same as before)
|
|
57
|
+
async function killServer() {
|
|
58
|
+
if (!serverProcess) return;
|
|
59
|
+
|
|
60
|
+
isKilling = true;
|
|
61
|
+
const pid = serverProcess.pid;
|
|
62
|
+
const killPromise = new Promise((resolve) => {
|
|
63
|
+
if (serverProcess.exitCode !== null) return resolve();
|
|
64
|
+
serverProcess.once("close", resolve);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
if (process.platform === "win32") {
|
|
68
|
+
try {
|
|
69
|
+
execSync(`taskkill /pid ${pid} /f /t`, { stdio: 'ignore' });
|
|
70
|
+
} catch (e) {
|
|
71
|
+
// Ignore errors if process is already dead
|
|
72
|
+
}
|
|
73
|
+
} else {
|
|
74
|
+
serverProcess.kill();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
await killPromise;
|
|
79
|
+
} catch (e) { }
|
|
80
|
+
serverProcess = null;
|
|
81
|
+
isKilling = false;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const delay = (ms) => new Promise(res => setTimeout(res, ms));
|
|
85
|
+
|
|
86
|
+
let spinnerTimer = null;
|
|
87
|
+
const frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
88
|
+
let frameIdx = 0;
|
|
89
|
+
|
|
90
|
+
function startSpinner(text) {
|
|
91
|
+
if (spinnerTimer) clearInterval(spinnerTimer);
|
|
92
|
+
process.stdout.write("\x1B[?25l"); // Hide cursor
|
|
93
|
+
spinnerTimer = setInterval(() => {
|
|
94
|
+
process.stdout.write(`\r ${cyan(frames[frameIdx])} ${gray(text)}`);
|
|
95
|
+
frameIdx = (frameIdx + 1) % frames.length;
|
|
96
|
+
}, 80);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function stopSpinner(success = true, text = "") {
|
|
100
|
+
if (spinnerTimer) {
|
|
101
|
+
clearInterval(spinnerTimer);
|
|
102
|
+
spinnerTimer = null;
|
|
103
|
+
}
|
|
104
|
+
process.stdout.write("\r\x1B[K"); // Clear line
|
|
105
|
+
process.stdout.write("\x1B[?25h"); // Show cursor
|
|
106
|
+
if (text) {
|
|
107
|
+
if (success) {
|
|
108
|
+
console.log(` ${green("✔")} ${green(text)}`);
|
|
109
|
+
} else {
|
|
110
|
+
console.log(` ${red("✖")} ${red(text)}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function startRustServer(retryCount = 0) {
|
|
116
|
+
const waitTime = retryCount > 0 ? 500 : 200;
|
|
117
|
+
|
|
118
|
+
await killServer();
|
|
119
|
+
await delay(waitTime);
|
|
120
|
+
|
|
121
|
+
const serverPath = path.join(process.cwd(), "server");
|
|
122
|
+
const startTime = Date.now();
|
|
123
|
+
|
|
124
|
+
startSpinner("Stabilizing your app on its orbit...");
|
|
125
|
+
|
|
126
|
+
let isReady = false;
|
|
127
|
+
let stdoutBuffer = "";
|
|
128
|
+
let buildLogs = "";
|
|
129
|
+
|
|
130
|
+
// If it takes more than 15s, update the message
|
|
131
|
+
const slowTimer = setTimeout(() => {
|
|
132
|
+
if (!isReady && !isKilling) {
|
|
133
|
+
startSpinner("Still stabilizing... (the first orbit takes longer)");
|
|
134
|
+
}
|
|
135
|
+
}, 15000);
|
|
136
|
+
|
|
137
|
+
serverProcess = spawn("cargo", ["run", "--quiet"], {
|
|
138
|
+
cwd: serverPath,
|
|
139
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
140
|
+
env: { ...process.env, CARGO_INCREMENTAL: "1" }
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
serverProcess.on("error", (err) => {
|
|
144
|
+
stopSpinner(false, "Failed to start orbit");
|
|
145
|
+
console.error(red(`[Titan] Error: ${err.message}`));
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
serverProcess.stderr.on("data", (data) => {
|
|
149
|
+
const str = data.toString();
|
|
150
|
+
if (isReady) {
|
|
151
|
+
process.stderr.write(data);
|
|
152
|
+
} else {
|
|
153
|
+
buildLogs += str;
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
serverProcess.stdout.on("data", (data) => {
|
|
158
|
+
const out = data.toString();
|
|
159
|
+
|
|
160
|
+
if (!isReady) {
|
|
161
|
+
stdoutBuffer += out;
|
|
162
|
+
if (stdoutBuffer.includes("Titan server running") || stdoutBuffer.includes("████████╗")) {
|
|
163
|
+
isReady = true;
|
|
164
|
+
clearTimeout(slowTimer);
|
|
165
|
+
stopSpinner(true, "Your app is now orbiting Titan Planet");
|
|
166
|
+
|
|
167
|
+
if (isFirstBoot) {
|
|
168
|
+
process.stdout.write(stdoutBuffer);
|
|
169
|
+
isFirstBoot = false;
|
|
170
|
+
} else {
|
|
171
|
+
// On subsequent reloads, only print non-banner lines from the buffer
|
|
172
|
+
const lines = stdoutBuffer.split("\n");
|
|
173
|
+
for (const line of lines) {
|
|
174
|
+
const isBanner = line.includes("Titan server running") ||
|
|
175
|
+
line.includes("████████╗") ||
|
|
176
|
+
line.includes("╚══") ||
|
|
177
|
+
line.includes(" ██║") ||
|
|
178
|
+
line.includes(" ╚═╝");
|
|
179
|
+
if (!isBanner && line.trim()) {
|
|
180
|
+
process.stdout.write(line + "\n");
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
stdoutBuffer = "";
|
|
185
|
+
}
|
|
186
|
+
} else {
|
|
187
|
+
process.stdout.write(data);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
serverProcess.on("close", async (code) => {
|
|
192
|
+
clearTimeout(slowTimer);
|
|
193
|
+
if (isKilling) return;
|
|
194
|
+
const runTime = Date.now() - startTime;
|
|
195
|
+
|
|
196
|
+
if (code !== 0 && code !== null) {
|
|
197
|
+
stopSpinner(false, "Orbit stabilization failed");
|
|
198
|
+
if (!isReady) {
|
|
199
|
+
console.log(gray("\n--- Build Logs ---"));
|
|
200
|
+
console.log(buildLogs);
|
|
201
|
+
console.log(gray("------------------\n"));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (runTime < 15000 && retryCount < 5) {
|
|
205
|
+
await delay(2000);
|
|
206
|
+
await startRustServer(retryCount + 1);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async function rebuild() {
|
|
213
|
+
try {
|
|
214
|
+
execSync("node app/app.js", { stdio: "ignore" });
|
|
215
|
+
// bundle is called inside app.js (t.start)
|
|
216
|
+
} catch (e) {
|
|
217
|
+
stopSpinner(false, "Failed to prepare runtime");
|
|
218
|
+
console.log(red(`[Titan] Error: ${e.message}`));
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async function startDev() {
|
|
223
|
+
const root = process.cwd();
|
|
224
|
+
const actionsDir = path.join(root, "app", "actions");
|
|
225
|
+
let hasRust = false;
|
|
226
|
+
if (fs.existsSync(actionsDir)) {
|
|
227
|
+
hasRust = fs.readdirSync(actionsDir).some(f => f.endsWith(".rs"));
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const mode = hasRust ? "Rust + JS Actions" : "JS Actions";
|
|
231
|
+
const version = getTitanVersion();
|
|
232
|
+
|
|
233
|
+
console.clear();
|
|
234
|
+
console.log("");
|
|
235
|
+
console.log(` ${bold(cyan("Titan Planet"))} ${gray("v" + version)} ${yellow("[ Dev Mode ]")}`);
|
|
236
|
+
console.log("");
|
|
237
|
+
console.log(` ${gray("Type: ")} ${mode}`);
|
|
238
|
+
console.log(` ${gray("Hot Reload: ")} ${green("Enabled")}`);
|
|
239
|
+
|
|
240
|
+
if (fs.existsSync(path.join(root, ".env"))) {
|
|
241
|
+
console.log(` ${gray("Env: ")} ${yellow("Loaded")}`);
|
|
242
|
+
}
|
|
243
|
+
console.log("");
|
|
244
|
+
|
|
245
|
+
try {
|
|
246
|
+
await rebuild();
|
|
247
|
+
await startRustServer();
|
|
248
|
+
} catch (e) {
|
|
249
|
+
// console.log(red("[Titan] Initial build failed. Waiting for changes..."));
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const watcher = chokidar.watch(["app", ".env"], {
|
|
253
|
+
ignoreInitial: true,
|
|
254
|
+
awaitWriteFinish: { stabilityThreshold: 500, pollInterval: 100 }
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
let timer = null;
|
|
258
|
+
watcher.on("all", async (event, file) => {
|
|
259
|
+
if (timer) clearTimeout(timer);
|
|
260
|
+
timer = setTimeout(async () => {
|
|
261
|
+
try {
|
|
262
|
+
await killServer();
|
|
263
|
+
await rebuild();
|
|
264
|
+
await startRustServer();
|
|
265
|
+
} catch (e) {
|
|
266
|
+
// console.log(red("[Titan] Build failed -- waiting for changes..."));
|
|
267
|
+
}
|
|
268
|
+
}, 300);
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async function handleExit() {
|
|
273
|
+
stopSpinner();
|
|
274
|
+
console.log(gray("\n[Titan] Stopping server..."));
|
|
275
|
+
await killServer();
|
|
276
|
+
process.exit(0);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
process.on("SIGINT", handleExit);
|
|
280
|
+
process.on("SIGTERM", handleExit);
|
|
281
|
+
|
|
282
|
+
startDev();
|