kavoru 0.9.8 → 0.9.9

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/package.json +1 -1
  2. package/src/features.ts +61 -29
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kavoru",
3
- "version": "0.9.8",
3
+ "version": "0.9.9",
4
4
  "description": "Scaffold a new Kavoru (Elysia + Bun) backend from the official template",
5
5
  "type": "module",
6
6
  "bin": {
package/src/features.ts CHANGED
@@ -495,41 +495,73 @@ async function patchEntryIndex(
495
495
  await writeText(projectDir, "src/index.ts", buildEntryIndex(selection));
496
496
  }
497
497
 
498
- async function patchServerIndex(
499
- projectDir: string,
500
- selection: FeatureSelection,
501
- ) {
502
- const relativePath = "src/server/index.ts";
503
- const current = await readText(projectDir, relativePath);
504
- if (!current) return;
498
+ export function buildServerIndex(selection: FeatureSelection): string {
499
+ const imports = [
500
+ 'import { Elysia } from "elysia";',
501
+ 'import { config } from "../config/index";',
502
+ 'import { logger } from "../common/logger";',
503
+ 'import { registerModules } from "../modules";',
504
+ selection.cron ? 'import { schedules } from "../schedules";' : null,
505
+ ]
506
+ .filter(Boolean)
507
+ .join("\n");
505
508
 
506
- let content = current;
509
+ const elysiaCtor = selection.websocket
510
+ ? `new Elysia({
511
+ websocket: {
512
+ idleTimeout: 120,
513
+ },
514
+ })`
515
+ : "new Elysia()";
516
+
517
+ const uses = [
518
+ " .use(registerModules)",
519
+ selection.cron ? " .use(schedules)" : null,
520
+ ]
521
+ .filter(Boolean)
522
+ .join("\n");
507
523
 
508
- if (selection.cron) {
509
- content = content.replace(
510
- /\/\/import \{ schedules \} from "\.\.\/schedules";.*\n/,
511
- 'import { schedules } from "../schedules";\n',
512
- );
513
- content = content.replace(
514
- /\/\/\.use\(schedules\);.*\n/,
515
- " .use(schedules);\n",
516
- );
517
- } else {
518
- content = content.replace(
519
- /^import \{ schedules \} from "\.\.\/schedules";\n/m,
520
- "",
521
- );
522
- content = content.replace(/^\s*\.use\(schedules\);\n/m, "");
524
+ return `${imports}
525
+
526
+ export class HttpServer {
527
+ private app: any;
528
+ private server?: ReturnType<Elysia["listen"]>;
529
+
530
+ constructor() {
531
+ this.app = ${elysiaCtor}
532
+ ${uses};
523
533
  }
524
534
 
525
- if (!selection.websocket) {
526
- content = content.replace(
527
- /new Elysia\(\{\s*websocket:\s*\{\s*idleTimeout:\s*120,\s*\},\s*\}\)/,
528
- "new Elysia()",
529
- );
535
+ async start() {
536
+ if (this.server) return;
537
+
538
+ await this.app.modules;
539
+
540
+ this.server = this.app.listen(config.env.server.port, () => {
541
+ logger.info(
542
+ \`API ready on port \${config.env.server.port}. Version: \${config.version}\`,
543
+ );
544
+ });
530
545
  }
531
546
 
532
- await writeText(projectDir, relativePath, content);
547
+ async stop() {
548
+ if (!this.server) return;
549
+ this.server.stop();
550
+ this.server = undefined;
551
+ }
552
+ }
553
+ `;
554
+ }
555
+
556
+ async function patchServerIndex(
557
+ projectDir: string,
558
+ selection: FeatureSelection,
559
+ ) {
560
+ await writeText(
561
+ projectDir,
562
+ "src/server/index.ts",
563
+ buildServerIndex(selection),
564
+ );
533
565
  }
534
566
 
535
567
  async function patchPackageJson(