@veducio/keja 0.5.0 → 0.6.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
package/src/cli/dev-reload.ts
CHANGED
|
@@ -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
|
+
}
|