@veducio/keja 0.5.0 → 0.7.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veducio/keja",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "packageManager": "bun@1.4.4",
5
5
  "description": "Veducio Keja - RESTFUL Web Server, Based on Lightweight Hono Framework, Written in TypeScript, Powered by Bun",
6
6
  "license": "MIT",
@@ -10,6 +10,7 @@ import {
10
10
  printServers,
11
11
  readProjectFailure,
12
12
  } from "./run-project.ts";
13
+ import { scaffoldEmptyRoutes } from "./scaffold-empty-route.ts";
13
14
 
14
15
  export const reloadDebounceMs = 50;
15
16
 
@@ -111,6 +112,7 @@ export async function runDev(root = process.cwd()) {
111
112
  const token = String(generation);
112
113
  if (running) {
113
114
  printReloading();
115
+ await scaffoldEmptyRoutes(running.paths.public, running.paths.service);
114
116
  }
115
117
  const result = await refreshDevProject({
116
118
  running: running !== null,
@@ -0,0 +1,67 @@
1
+ import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
2
+ import { mkdtemp, readFile, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import path from "node:path";
5
+ import { describe, expect, it } from "vitest";
6
+ import {
7
+ emptyRouteSource,
8
+ routePathFromFile,
9
+ scaffoldEmptyRouteFile,
10
+ scaffoldEmptyRoutes,
11
+ } from "./scaffold-empty-route.ts";
12
+
13
+ describe("scaffold empty route", () => {
14
+ it("derives a path from the file name", () => {
15
+ expect(routePathFromFile("src/public/notes.ts")).toBe("/notes");
16
+ expect(routePathFromFile("hello.world.ts")).toBe("/hello.world");
17
+ expect(routePathFromFile("bad name.ts")).toBe("/route");
18
+ });
19
+
20
+ it("writes a public get stub and a service post stub", () => {
21
+ expect(emptyRouteSource("public", "/items")).toContain('method: "get"');
22
+ expect(emptyRouteSource("public", "/items")).toContain('path: "/items"');
23
+ expect(emptyRouteSource("service", "/jobs")).toContain('method: "post"');
24
+ });
25
+
26
+ it("fills an empty route file once", async () => {
27
+ const directory = await mkdtemp(path.join(tmpdir(), "keja-scaffold-"));
28
+ const file = path.join(directory, "items.ts");
29
+ await writeFile(file, "");
30
+ expect(await scaffoldEmptyRouteFile(file, "public")).toBe(true);
31
+ const first = await readFile(file, "utf8");
32
+ expect(first).toContain('path: "/items"');
33
+ expect(await scaffoldEmptyRouteFile(file, "public")).toBe(false);
34
+ expect(await readFile(file, "utf8")).toBe(first);
35
+ });
36
+
37
+ it("fills empty route modules under public and service folders", async () => {
38
+ const root = mkdtempSync(path.join(tmpdir(), "keja-scaffold-dirs-"));
39
+ const publicDir = path.join(root, "public");
40
+ const serviceDir = path.join(root, "service");
41
+ mkdirSync(publicDir);
42
+ mkdirSync(serviceDir);
43
+ writeFileSync(path.join(publicDir, "notes.ts"), " \n");
44
+ writeFileSync(path.join(publicDir, "_middleware.ts"), "");
45
+ writeFileSync(path.join(serviceDir, "jobs.ts"), "");
46
+ writeFileSync(path.join(publicDir, "ready.ts"), "export {}\n");
47
+ const written = await scaffoldEmptyRoutes(publicDir, serviceDir);
48
+ expect(written.sort()).toEqual(
49
+ [
50
+ path.join(publicDir, "notes.ts"),
51
+ path.join(serviceDir, "jobs.ts"),
52
+ ].sort(),
53
+ );
54
+ expect(await readFile(path.join(publicDir, "notes.ts"), "utf8")).toContain(
55
+ 'method: "get"',
56
+ );
57
+ expect(await readFile(path.join(serviceDir, "jobs.ts"), "utf8")).toContain(
58
+ 'method: "post"',
59
+ );
60
+ expect(await readFile(path.join(publicDir, "_middleware.ts"), "utf8")).toBe(
61
+ "",
62
+ );
63
+ expect(await readFile(path.join(publicDir, "ready.ts"), "utf8")).toBe(
64
+ "export {}\n",
65
+ );
66
+ });
67
+ });
@@ -0,0 +1,72 @@
1
+ import { existsSync } from "node:fs";
2
+ import { readFile, writeFile } from "node:fs/promises";
3
+ import path from "node:path";
4
+ import { collectRouteFiles } from "../project/load.ts";
5
+
6
+ export function routePathFromFile(file: string) {
7
+ const base = path.basename(file, ".ts");
8
+ if (/^[A-Za-z0-9._~-]+$/.test(base)) {
9
+ return `/${base}`;
10
+ }
11
+ return "/route";
12
+ }
13
+
14
+ export function emptyRouteSource(
15
+ surface: "public" | "service",
16
+ routePath: string,
17
+ ) {
18
+ const method = surface === "service" ? "post" : "get";
19
+ return `import { defineRoute, response } from "@veducio/keja";
20
+ import { z } from "zod";
21
+
22
+ export default defineRoute({
23
+ method: "${method}",
24
+ path: "${routePath}",
25
+ version: "v1",
26
+ responses: {
27
+ Ok: response({
28
+ code: 200,
29
+ json: z.object({ ok: z.literal(true) }),
30
+ }),
31
+ },
32
+ handler: ({ respond }) => respond.Ok({ ok: true }),
33
+ });
34
+ `;
35
+ }
36
+
37
+ export async function scaffoldEmptyRouteFile(
38
+ file: string,
39
+ surface: "public" | "service",
40
+ ) {
41
+ if (!existsSync(file)) {
42
+ return false;
43
+ }
44
+ const content = await readFile(file, "utf8");
45
+ if (content.trim() !== "") {
46
+ return false;
47
+ }
48
+ await writeFile(file, emptyRouteSource(surface, routePathFromFile(file)));
49
+ return true;
50
+ }
51
+
52
+ export async function scaffoldEmptyRoutes(
53
+ publicDir: string,
54
+ serviceDir: string,
55
+ ) {
56
+ const written: string[] = [];
57
+ for (const [surface, directory] of [
58
+ ["public", publicDir],
59
+ ["service", serviceDir],
60
+ ] as const) {
61
+ if (!existsSync(directory)) {
62
+ continue;
63
+ }
64
+ for (const relative of await collectRouteFiles(directory)) {
65
+ const file = path.join(directory, relative);
66
+ if (await scaffoldEmptyRouteFile(file, surface)) {
67
+ written.push(file);
68
+ }
69
+ }
70
+ }
71
+ return written;
72
+ }
@@ -1,3 +1,5 @@
1
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2
+ import { tmpdir } from "node:os";
1
3
  import path from "node:path";
2
4
  import { describe, expect, it } from "vitest";
3
5
  import { freshImportUrl, resolveProjectImport } from "./reload-graph.ts";
@@ -26,6 +28,32 @@ describe("project reload imports", () => {
26
28
  });
27
29
  });
28
30
 
31
+ it("adds a TypeScript extension to an extensionless relative import", () => {
32
+ const project = mkdtempSync(path.join(tmpdir(), "keja-reload-"));
33
+ const repositories = path.join(project, "src", "db", "repositories.ts");
34
+ const importer = path.join(
35
+ project,
36
+ "src",
37
+ "public",
38
+ "config",
39
+ "tenants.ts",
40
+ );
41
+ mkdirSync(path.dirname(repositories), { recursive: true });
42
+ mkdirSync(path.dirname(importer), { recursive: true });
43
+ writeFileSync(repositories, "export {}\n");
44
+ try {
45
+ const resolved = resolveProjectImport({
46
+ importer: `keja:${importer}`,
47
+ specifier: "../../db/repositories",
48
+ projectRoot: project,
49
+ generation: "4",
50
+ });
51
+ expect(resolved?.file).toBe(repositories);
52
+ } finally {
53
+ rmSync(project, { recursive: true, force: true });
54
+ }
55
+ });
56
+
29
57
  it("follows the next file imported by that dependency", () => {
30
58
  const data = path.join(root, "src", "data.ts");
31
59
  const resolved = resolveProjectImport({
@@ -1,4 +1,4 @@
1
- import { readFileSync } from "node:fs";
1
+ import { existsSync, readFileSync, statSync } from "node:fs";
2
2
  import path from "node:path";
3
3
  import { fileURLToPath } from "node:url";
4
4
 
@@ -35,7 +35,9 @@ export function resolveProjectImport(input: {
35
35
  if (!inProject && !inGraph) {
36
36
  return null;
37
37
  }
38
- const resolved = path.resolve(path.dirname(importerFile), specifier);
38
+ const resolved = resolveRelativeFile(
39
+ path.resolve(path.dirname(importerFile), specifier),
40
+ );
39
41
  if (isNodeModules(resolved)) {
40
42
  return null;
41
43
  }
@@ -148,6 +150,46 @@ function isRelativeSpecifier(specifier: string) {
148
150
  return specifier.startsWith("./") || specifier.startsWith("../");
149
151
  }
150
152
 
153
+ const sourceExtensions = [
154
+ ".ts",
155
+ ".tsx",
156
+ ".js",
157
+ ".jsx",
158
+ ".mjs",
159
+ ".cjs",
160
+ ".json",
161
+ ] as const;
162
+
163
+ function resolveRelativeFile(resolved: string) {
164
+ if (fileExists(resolved)) {
165
+ return resolved;
166
+ }
167
+ if (path.extname(resolved) !== "") {
168
+ return resolved;
169
+ }
170
+ for (const extension of sourceExtensions) {
171
+ const candidate = `${resolved}${extension}`;
172
+ if (fileExists(candidate)) {
173
+ return candidate;
174
+ }
175
+ }
176
+ for (const extension of sourceExtensions) {
177
+ const candidate = path.join(resolved, `index${extension}`);
178
+ if (fileExists(candidate)) {
179
+ return candidate;
180
+ }
181
+ }
182
+ return resolved;
183
+ }
184
+
185
+ function fileExists(file: string) {
186
+ try {
187
+ return existsSync(file) && statSync(file).isFile();
188
+ } catch {
189
+ return false;
190
+ }
191
+ }
192
+
151
193
  function filePath(importer: string) {
152
194
  const withoutQuery = importer.split("?")[0] ?? importer;
153
195
  const clean = withoutQuery.startsWith(`${namespace}:`)