bakit 2.0.2 → 2.1.0

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.
Files changed (2) hide show
  1. package/dist/cli.js +73 -0
  2. package/package.json +9 -6
package/dist/cli.js ADDED
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env -S npx tsx
2
+ import { fileURLToPath, pathToFileURL } from 'url';
3
+ import { fork } from 'child_process';
4
+ import { config } from 'dotenv';
5
+ import { program } from 'commander';
6
+ import glob from 'tiny-glob';
7
+ import { getInternalService, ServiceRole } from '@bakit/service';
8
+
9
+ // package.json
10
+ var package_default = {
11
+ name: "bakit",
12
+ version: "2.1.0"};
13
+
14
+ // src/lib/cli/index.ts
15
+ config();
16
+ program.name(package_default.name).description("Bakit CLI tool").version(package_default.version);
17
+ program.command("start").description("Start the services").argument("[name...]", "Name of the service to start").action(start);
18
+ program.parse();
19
+ var cliPath = fileURLToPath(import.meta.url);
20
+ async function start(names) {
21
+ await runSupervisor(names);
22
+ }
23
+ async function runSupervisor(names = []) {
24
+ if (names.length === 1) {
25
+ await runServiceWorker(names[0]);
26
+ return;
27
+ }
28
+ let services = await loadServices();
29
+ if (names.length === 0)
30
+ return spawnServices(services);
31
+ let nameSet = new Set(names), toStart = services.filter((s) => nameSet.has(s.name));
32
+ if (toStart.length !== nameSet.size) {
33
+ let found = new Set(toStart.map((s) => s.name)), missing = names.filter((n) => !found.has(n));
34
+ console.error(`Service(s) not found: ${missing.join(", ")}`), process.exit(1);
35
+ }
36
+ return spawnServices(toStart);
37
+ }
38
+ function spawnServices(services) {
39
+ let children = [];
40
+ process.on("SIGINT", shutdown), process.on("SIGTERM", shutdown);
41
+ for (let service of services) {
42
+ let child = fork(cliPath, ["start", service.name], {
43
+ env: {
44
+ ...process.env,
45
+ BAKIT_SERVICE_NAME: service.name
46
+ },
47
+ stdio: "inherit"
48
+ });
49
+ child.on("exit", (code) => {
50
+ console.log(`Service "${service.name}" exited with code ${code}`);
51
+ }), children.push(child);
52
+ }
53
+ async function shutdown() {
54
+ await Promise.all(
55
+ children.map((child) => new Promise((resolve) => {
56
+ child.once("exit", resolve), child.kill("SIGTERM"), setTimeout(() => child.kill("SIGKILL"), 5e3);
57
+ }))
58
+ ), process.exit(0);
59
+ }
60
+ }
61
+ async function loadServices() {
62
+ let paths = await glob("src/services/**/*.{js,ts}");
63
+ return await Promise.all(
64
+ paths.map(async (path) => (await import(pathToFileURL(path).href)).default)
65
+ );
66
+ }
67
+ async function runServiceWorker(name) {
68
+ let service = (await loadServices()).find((s) => s.name === name);
69
+ service || (console.error(`Service "${name}" not found.`), process.exit(1)), await new Promise((resolve) => {
70
+ let internal = getInternalService(service);
71
+ internal.bind(ServiceRole.Server), internal.runtime.role !== "server" && (console.error(`Service "${name}" is not a server.`), process.exit(1)), internal.runtime.transport.once("listen", resolve);
72
+ }), console.log(`Service "${name}" started as server`);
73
+ }
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "bakit",
3
- "version": "2.0.2",
3
+ "version": "2.1.0",
4
4
  "description": "A framework for discordeno",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": {
8
8
  "import": "./dist/index.js",
9
9
  "types": "./dist/index.d.ts"
10
- }
10
+ },
11
+ "./cli": "./dist/cli.js"
11
12
  },
12
13
  "bin": {
13
14
  "bakit": "./dist/cli.js"
@@ -32,12 +33,14 @@
32
33
  "license": "MIT",
33
34
  "dependencies": {
34
35
  "commander": "^14.0.2",
36
+ "dotenv": "^17.2.3",
35
37
  "tiny-glob": "^0.2.9",
38
+ "tsx": "^4.21.0",
36
39
  "type-fest": "^4.41.0",
37
- "@bakit/rest": "^2.0.2",
38
- "@bakit/gateway": "^2.1.2",
39
- "@bakit/service": "^3.0.0",
40
- "@bakit/utils": "^2.0.0"
40
+ "@bakit/rest": "^2.0.3",
41
+ "@bakit/utils": "^2.0.0",
42
+ "@bakit/gateway": "^2.1.3",
43
+ "@bakit/service": "^3.1.0"
41
44
  },
42
45
  "scripts": {
43
46
  "build": "tsup",