milkio 0.0.31 → 0.0.32

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/c.ts CHANGED
@@ -3,7 +3,6 @@
3
3
  /* eslint-disable no-constant-condition, @typescript-eslint/no-misused-promises, no-console, @typescript-eslint/no-explicit-any */
4
4
 
5
5
  import { argv, exit } from "node:process"
6
- import process from "process"
7
6
  import { $ } from "bun"
8
7
 
9
8
  const method = argv[2] as keyof typeof commands
@@ -0,0 +1,46 @@
1
+ /* eslint-disable no-console, @typescript-eslint/no-explicit-any */
2
+ import type { MilkioApp } from ".."
3
+
4
+ export type CommandOptions = {
5
+ notFoundHandler?: (event: { name: string, path: string, commands: Array<string>, options: Record<string, string | true> }) => void | Promise<void>;
6
+ }
7
+
8
+ export function defineCommandHandler(app: MilkioApp, options: CommandOptions = {}) {
9
+ const call = async (argv: Array<string>) => {
10
+ const params = {
11
+ path: 'default',
12
+ commands: [] as Array<string>,
13
+ options: {} as Record<string, string | true>
14
+ }
15
+ for (const v of argv.slice(3)) {
16
+ if (v.startsWith("--") && v.includes('=')) {
17
+ const vSplited = v.split("=")
18
+ params.options[vSplited[0].slice(2)] = vSplited.slice(1, vSplited.length).join("=")
19
+ } else if (v.startsWith("--")) {
20
+ params.options[v.slice(2)] = true
21
+ } else if (v.startsWith("-") && v.includes('=')) {
22
+ const vSplited = v.split("=")
23
+ params.options[vSplited[0].slice(1)] = vSplited.slice(1, vSplited.length).join("=")
24
+ } else if (v.startsWith("-")) {
25
+ params.options[v.slice(1)] = true
26
+ } else {
27
+ params.commands.push(v)
28
+ }
29
+ }
30
+ if (argv.length === 2) params.path = `$/default`
31
+ else params.path = `$/${argv[2]}`
32
+
33
+ // @ts-ignore
34
+ const result = await app.execute(params.path as any, params)
35
+ if (!result.success) {
36
+ if (result.fail.code === 'NOT_FOUND') {
37
+ if (options.notFoundHandler) await options.notFoundHandler({ ...params, name: argv.length === 2 ? 'default' : argv[2] })
38
+ return
39
+ }
40
+ console.log(result.fail.message)
41
+ }
42
+ else if (result.data) console.log(result.data)
43
+ }
44
+
45
+ return call
46
+ }
@@ -75,7 +75,7 @@ export function defineHttpHandler(app: MilkioApp, options: ExecuteHttpServerOpti
75
75
  let pathstr = path.join("/") as keyof (typeof schema)["apiMethodsSchema"]
76
76
 
77
77
  // Special processing: do not run middleware when encountering 404 and return quickly
78
- if (!(pathstr in schema.apiMethodsSchema)) {
78
+ if (!(pathstr in schema.apiMethodsSchema) || pathstr.startsWith('$/')) {
79
79
  // @ts-ignore
80
80
  const redirectPath = await routerHandler(pathstr, fullurl)
81
81
  if (!redirectPath) {
package/index.ts CHANGED
@@ -27,6 +27,7 @@ export * from "./kernel/middleware"
27
27
 
28
28
  // handler
29
29
  export * from "./defines/define-http-handler"
30
+ export * from "./defines/define-command-handler"
30
31
 
31
32
  // api test
32
33
  export * from "./api-test/index"
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "milkio",
3
3
  "type": "module",
4
4
  "module": "index.ts",
5
- "version": "0.0.31",
5
+ "version": "0.0.32",
6
6
  "peerDependencies": {
7
7
  "typescript": "^5.4.2"
8
8
  },
@@ -60,12 +60,12 @@ export default async () => {
60
60
  if (module?.api?.isApi === true) {
61
61
  // Exclude disallowed characters
62
62
  if (path.includes("_")) {
63
- console.error(`\n\nPath: ` + `"${path}"`)
63
+ console.error(`\n\nPath: ` + `"${path.slice(0, -3)}"`)
64
64
  console.error(`Do not use "_" in the path. If you want to add a separator between words, please use "-".\n`)
65
65
  exit(1)
66
66
  }
67
- if (!/^[a-z0-9/-]+$/.test(path.slice(0, -3))) {
68
- console.error(`\n\nPath: ` + `"${path}"`)
67
+ if (!/^[a-z0-9/$/-]+$/.test(path.slice(0, -3))) {
68
+ console.error(`\n\nPath: ` + `"${path.slice(0, -3)}"`)
69
69
  console.error(`The path can only contain lowercase letters, numbers, and "-".\n`)
70
70
  exit(1)
71
71
  }