From 68d90ace43fbd84a4b997c3869d800fd9d719290 Mon Sep 17 00:00:00 2001 From: mewhhaha Date: Thu, 20 Jun 2024 22:44:12 +0200 Subject: [PATCH] feat: add splat routes to cli --- .changeset/real-ligers-attend.md | 18 +++++++++++ examples/example-worker/app/routes/_router.ts | 7 ++-- examples/example-worker/app/routes/get.$.ts | 5 +++ .../src/generate-file-routes.ts | 3 ++ packages/little-worker-cli/src/index.ts | 32 +++++++++++++++---- 5 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 .changeset/real-ligers-attend.md create mode 100644 examples/example-worker/app/routes/get.$.ts diff --git a/.changeset/real-ligers-attend.md b/.changeset/real-ligers-attend.md new file mode 100644 index 0000000..0bed8b4 --- /dev/null +++ b/.changeset/real-ligers-attend.md @@ -0,0 +1,18 @@ +--- +"@mewhhaha/little-worker-cli": patch +"example-worker": patch +"benchmark": patch +"example-router": patch +"@mewhhaha/json-string": patch +"@mewhhaha/little-fetcher": patch +"@mewhhaha/little-router": patch +"@mewhhaha/little-router-plugin-data": patch +"@mewhhaha/little-router-plugin-query": patch +"@mewhhaha/little-worker": patch +"@mewhhaha/typed-request": patch +"@mewhhaha/typed-response": patch +"@mewhhaha/config": patch +"@mewhhaha/testing": patch +--- + +add splat routes for cli diff --git a/examples/example-worker/app/routes/_router.ts b/examples/example-worker/app/routes/_router.ts index a7810bc..6b3fbbb 100644 --- a/examples/example-worker/app/routes/_router.ts +++ b/examples/example-worker/app/routes/_router.ts @@ -5,6 +5,7 @@ import route_cG9zdC5leGFtcGxlLXBvc3QudHM from "./post.example-post.js"; import route_cG9zdC5leGFtcGxlLWFkdmFuY2VkLnRz from "./post.example-advanced.js"; import route_Z2V0LmV4YW1wbGUtcXVlcnktcGFyYW1zLnRz from "./get.example-query-params.js"; import route_Z2V0LmV4YW1wbGUtZ2V0LnRz from "./get.example-get.js"; +import route_Z2V0LiQudHM from "./get.$.js"; export const router = Router< RouteData["extra"] extends unknown[] ? RouteData["extra"] : [] >() @@ -13,7 +14,8 @@ export const router = Router< .post(...route_cG9zdC5leGFtcGxlLXBvc3QudHM) .post(...route_cG9zdC5leGFtcGxlLWFkdmFuY2VkLnRz) .get(...route_Z2V0LmV4YW1wbGUtcXVlcnktcGFyYW1zLnRz) - .get(...route_Z2V0LmV4YW1wbGUtZ2V0LnRz); + .get(...route_Z2V0LmV4YW1wbGUtZ2V0LnRz) + .get(...route_Z2V0LiQudHM); const routes = router.infer; export type Routes = typeof routes; @@ -25,6 +27,7 @@ declare module "@mewhhaha/little-worker" { | "/example-post" | "/example-advanced" | "/example-query-params" - | "/example-get"; + | "/example-get" + | "/*"; } } diff --git a/examples/example-worker/app/routes/get.$.ts b/examples/example-worker/app/routes/get.$.ts new file mode 100644 index 0000000..e846a03 --- /dev/null +++ b/examples/example-worker/app/routes/get.$.ts @@ -0,0 +1,5 @@ +import { route, text } from "@mewhhaha/little-worker"; + +export default route("/*", [], () => { + return text(200, "Hello fetch!"); +}); diff --git a/packages/little-worker-cli/src/generate-file-routes.ts b/packages/little-worker-cli/src/generate-file-routes.ts index 156ead4..474fa09 100644 --- a/packages/little-worker-cli/src/generate-file-routes.ts +++ b/packages/little-worker-cli/src/generate-file-routes.ts @@ -24,6 +24,8 @@ const dotRegex = /\./g; const dollarRegex = /\$/g; +const splatRegex = /\$$/g; + const methodRegex = /^(post)|(get)|(delete)|(put)|(options)|(all)|(patch)/; const isRouteFile = (f: string) => f.match(methodRegex); @@ -78,6 +80,7 @@ export const fileToPath = (file: string) => .replace(tsRegex, "") .replace(methodRegex, "") .replace(dotRegex, "/") + .replace(splatRegex, "*") .replace(dollarRegex, ":"); export const orderRoutes = (a: string, b: string): number => { diff --git a/packages/little-worker-cli/src/index.ts b/packages/little-worker-cli/src/index.ts index 0b5053b..797bddc 100644 --- a/packages/little-worker-cli/src/index.ts +++ b/packages/little-worker-cli/src/index.ts @@ -1,6 +1,7 @@ #!/usr/bin/env node import yargs from "yargs"; +import fs from "fs/promises"; import { hideBin } from "yargs/helpers"; import { main as generateRoutes } from "./generate-file-routes.js"; @@ -9,15 +10,34 @@ await yargs(hideBin(process.argv)) "routes", "CLI to generate routes and API in a little-worker project", (yargs) => { - return yargs.option("target", { - alias: "t", - type: "string", - description: "Target folder to generate router from", - default: "app/routes", - }); + return yargs + .option("target", { + alias: "t", + type: "string", + description: "Target folder to generate router from", + default: "app/routes", + }) + .option("watch", { + alias: "w", + type: "boolean", + description: "Watch for changes", + }); }, async (argv) => { await generateRoutes(argv.target); + + if (argv.watch) { + console.log("Watching for changes..."); + const watcher = fs.watch(argv.target); + + for await (const event of watcher) { + switch (event.eventType) { + case "rename": { + await generateRoutes(argv.target); + } + } + } + } }, ) .parse();