@babylen/legion 0.1.1 → 0.1.2
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/dist/index.js +69 -33
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -23,11 +23,15 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
23
23
|
mod
|
|
24
24
|
));
|
|
25
25
|
|
|
26
|
+
// src/index.ts
|
|
27
|
+
var import_child_process = require("child_process");
|
|
28
|
+
|
|
29
|
+
// package.json
|
|
30
|
+
var version = "0.1.2";
|
|
31
|
+
|
|
26
32
|
// src/index.ts
|
|
27
33
|
var import_socket = require("socket.io-client");
|
|
28
34
|
var os3 = __toESM(require("os"));
|
|
29
|
-
var import_fs2 = require("fs");
|
|
30
|
-
var import_path4 = require("path");
|
|
31
35
|
|
|
32
36
|
// src/core/config.ts
|
|
33
37
|
var import_os = __toESM(require("os"));
|
|
@@ -261,25 +265,57 @@ var Log;
|
|
|
261
265
|
})(Log || (Log = {}));
|
|
262
266
|
|
|
263
267
|
// src/index.ts
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
268
|
+
if (process.argv.includes("--version") || process.argv.includes("-v")) {
|
|
269
|
+
console.log(version);
|
|
270
|
+
process.exit(0);
|
|
271
|
+
}
|
|
272
|
+
function checkForUpdates() {
|
|
273
|
+
setTimeout(async () => {
|
|
274
|
+
try {
|
|
275
|
+
const packageName = "@babylen/legion";
|
|
276
|
+
const command = `npm view ${packageName} version`;
|
|
277
|
+
const timeout = 3e3;
|
|
278
|
+
const latestVersion = (0, import_child_process.execSync)(command, {
|
|
279
|
+
encoding: "utf-8",
|
|
280
|
+
timeout,
|
|
281
|
+
stdio: ["ignore", "pipe", "ignore"]
|
|
282
|
+
}).trim();
|
|
283
|
+
if (latestVersion !== version) {
|
|
284
|
+
console.warn(`
|
|
285
|
+
\u26A0\uFE0F Update available: ${version} \u2192 ${latestVersion}`);
|
|
286
|
+
console.warn(` Run: npm install -g ${packageName}@latest
|
|
287
|
+
`);
|
|
288
|
+
}
|
|
289
|
+
} catch (error) {
|
|
290
|
+
}
|
|
291
|
+
}, 100);
|
|
271
292
|
}
|
|
272
293
|
async function main() {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
294
|
+
let log = null;
|
|
295
|
+
const safeLog = {
|
|
296
|
+
info: (message, extra) => {
|
|
297
|
+
if (log) log.info(message, extra);
|
|
298
|
+
else console.log(message, extra);
|
|
299
|
+
},
|
|
300
|
+
error: (message, extra) => {
|
|
301
|
+
if (log) log.error(message, extra);
|
|
302
|
+
else console.error(message, extra);
|
|
303
|
+
},
|
|
304
|
+
warn: (message, extra) => {
|
|
305
|
+
if (log) log.warn(message, extra);
|
|
306
|
+
else console.warn(message, extra);
|
|
307
|
+
},
|
|
308
|
+
debug: (message, extra) => {
|
|
309
|
+
if (log) log.debug(message, extra);
|
|
310
|
+
}
|
|
311
|
+
};
|
|
277
312
|
try {
|
|
278
|
-
|
|
313
|
+
log = Log.create({ service: "legion" });
|
|
314
|
+
checkForUpdates();
|
|
279
315
|
await Global.init();
|
|
280
|
-
|
|
316
|
+
log.info(`\u{1F6E1}\uFE0F Legion v${version} starting...`);
|
|
281
317
|
const config2 = await getConfig();
|
|
282
|
-
|
|
318
|
+
log.info("\u{1F517} Connecting to server", { serverUrl: config2.serverUrl });
|
|
283
319
|
const socket = (0, import_socket.io)(config2.serverUrl, {
|
|
284
320
|
auth: {
|
|
285
321
|
id: config2.id,
|
|
@@ -295,7 +331,7 @@ async function main() {
|
|
|
295
331
|
reconnectionAttempts: Infinity
|
|
296
332
|
});
|
|
297
333
|
socket.on("connect", () => {
|
|
298
|
-
|
|
334
|
+
log.info("\u2705 Connected to server", { socketId: socket.id });
|
|
299
335
|
const handshakeData = {
|
|
300
336
|
hostname: os3.hostname(),
|
|
301
337
|
platform: os3.platform(),
|
|
@@ -303,61 +339,61 @@ async function main() {
|
|
|
303
339
|
cwd: process.cwd()
|
|
304
340
|
};
|
|
305
341
|
socket.emit("legion:handshake", handshakeData);
|
|
306
|
-
|
|
342
|
+
log.debug("\u{1F4E4} Sent handshake", handshakeData);
|
|
307
343
|
});
|
|
308
344
|
socket.on("connect_error", (err) => {
|
|
309
|
-
|
|
345
|
+
log.error("\u274C Connection error", {
|
|
310
346
|
message: err.message,
|
|
311
347
|
type: err.type
|
|
312
348
|
});
|
|
313
349
|
if (err.message.includes("auth") || err.message.includes("token")) {
|
|
314
|
-
|
|
315
|
-
|
|
350
|
+
log.error("\u{1F510} Authentication failed. Please check your token.");
|
|
351
|
+
log.error("\u{1F4A1} Re-authenticate by updating ~/.tanuki/config.json or LEGION_TOKEN env var");
|
|
316
352
|
process.exit(1);
|
|
317
353
|
}
|
|
318
354
|
});
|
|
319
355
|
socket.on("disconnect", (reason) => {
|
|
320
|
-
|
|
356
|
+
log.warn("\u26A0\uFE0F Disconnected from server", { reason });
|
|
321
357
|
if (reason === "io server disconnect") {
|
|
322
|
-
|
|
358
|
+
log.error("\u{1F6AB} Server disconnected this client. Please check your token.");
|
|
323
359
|
process.exit(1);
|
|
324
360
|
}
|
|
325
361
|
});
|
|
326
362
|
socket.on("server:ping", (data2) => {
|
|
327
|
-
|
|
363
|
+
log.debug("\u{1F4E9} Ping from server", data2);
|
|
328
364
|
socket.emit("legion:pong", { ts: Date.now() });
|
|
329
365
|
});
|
|
330
366
|
socket.on("reconnect", (attemptNumber) => {
|
|
331
|
-
|
|
367
|
+
log.info("\u{1F504} Reconnected to server", { attempt: attemptNumber });
|
|
332
368
|
});
|
|
333
369
|
socket.on("reconnect_attempt", (attemptNumber) => {
|
|
334
|
-
|
|
370
|
+
log.debug("\u{1F504} Reconnection attempt", { attempt: attemptNumber });
|
|
335
371
|
});
|
|
336
372
|
socket.on("reconnect_error", (error) => {
|
|
337
|
-
|
|
373
|
+
log.error("\u{1F504} Reconnection error", { message: error.message });
|
|
338
374
|
});
|
|
339
375
|
socket.on("reconnect_failed", () => {
|
|
340
|
-
|
|
376
|
+
log.error("\u{1F504} Reconnection failed after all attempts");
|
|
341
377
|
process.exit(1);
|
|
342
378
|
});
|
|
343
379
|
process.stdin.resume();
|
|
344
380
|
const shutdown = () => {
|
|
345
|
-
|
|
381
|
+
log.info("\u{1F6D1} Shutting down...");
|
|
346
382
|
socket.disconnect();
|
|
347
383
|
process.exit(0);
|
|
348
384
|
};
|
|
349
385
|
process.on("SIGINT", shutdown);
|
|
350
386
|
process.on("SIGTERM", shutdown);
|
|
351
387
|
process.on("unhandledRejection", (reason) => {
|
|
352
|
-
|
|
388
|
+
log.error("Unhandled rejection", { reason });
|
|
353
389
|
});
|
|
354
390
|
process.on("uncaughtException", (error) => {
|
|
355
|
-
|
|
391
|
+
log.error("Uncaught exception", { error: error.message, stack: error.stack });
|
|
356
392
|
shutdown();
|
|
357
393
|
});
|
|
358
394
|
} catch (error) {
|
|
359
395
|
if (error instanceof Error) {
|
|
360
|
-
|
|
396
|
+
safeLog.error("Failed to start Legion", {
|
|
361
397
|
message: error.message,
|
|
362
398
|
stack: error.stack
|
|
363
399
|
});
|
|
@@ -366,7 +402,7 @@ async function main() {
|
|
|
366
402
|
console.error(" - Set LEGION_TOKEN environment variable, or");
|
|
367
403
|
console.error(" - Create ~/.tanuki/config.json with your token");
|
|
368
404
|
} else {
|
|
369
|
-
|
|
405
|
+
safeLog.error("Failed to start Legion", { error });
|
|
370
406
|
console.error("\n\u274C Unknown error occurred");
|
|
371
407
|
}
|
|
372
408
|
process.exit(1);
|