Skip to content

Commit

Permalink
feat: add splat routes to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
mewhhaha committed Jun 20, 2024
1 parent d788de9 commit 68d90ac
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
18 changes: 18 additions & 0 deletions .changeset/real-ligers-attend.md
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions examples/example-worker/app/routes/_router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"] : []
>()
Expand All @@ -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;

Expand All @@ -25,6 +27,7 @@ declare module "@mewhhaha/little-worker" {
| "/example-post"
| "/example-advanced"
| "/example-query-params"
| "/example-get";
| "/example-get"
| "/*";
}
}
5 changes: 5 additions & 0 deletions examples/example-worker/app/routes/get.$.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { route, text } from "@mewhhaha/little-worker";

export default route("/*", [], () => {
return text(200, "Hello fetch!");
});
3 changes: 3 additions & 0 deletions packages/little-worker-cli/src/generate-file-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 => {
Expand Down
32 changes: 26 additions & 6 deletions packages/little-worker-cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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();

0 comments on commit 68d90ac

Please sign in to comment.