@kevisual/router 0.1.0 → 0.1.1

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/dist/app.js CHANGED
@@ -19582,7 +19582,7 @@ app
19582
19582
  10. **中间件找不到会返回 404**,错误信息中会包含找不到的中间件列表。
19583
19583
  `;
19584
19584
  // package.json
19585
- var version2 = "0.1.0";
19585
+ var version2 = "0.1.1";
19586
19586
 
19587
19587
  // agent/routes/route-create.ts
19588
19588
  app.route({
package/dist/commander.js CHANGED
@@ -2148,7 +2148,14 @@ var parseArgs = (args) => {
2148
2148
  for (const pair of pairs) {
2149
2149
  const idx = pair.indexOf("=");
2150
2150
  const key = pair.slice(0, idx);
2151
- const value = pair.slice(idx + 1);
2151
+ const raw = pair.slice(idx + 1);
2152
+ let value = raw;
2153
+ if (raw === "true")
2154
+ value = true;
2155
+ else if (raw === "false")
2156
+ value = false;
2157
+ else if (raw !== "" && !isNaN(Number(raw)))
2158
+ value = Number(raw);
2152
2159
  result[key] = value;
2153
2160
  }
2154
2161
  return result;
@@ -2200,7 +2207,7 @@ var createCommand2 = (opts) => {
2200
2207
  if (!route.key)
2201
2208
  return;
2202
2209
  const description = parseDescription(route);
2203
- subProgram.command(route.key).description(description || "").option("--args <args>", "JSON字符串参数,传递给命令执行").action(async (options) => {
2210
+ subProgram.command(route.key).description(description || "").option("--args <args>", `命令参数,支持 JSON 格式或 key=value 形式,例如: --args '{"a":1}' 或 --args 'a=1 b=2'`).argument("[args...]", `位置参数(推荐通过 -- 分隔传入),支持 JSON 或 key=value 格式,例如: -- a=1 b=2 或 -- '{"a":1}'`).action(async (passedArgs, options, _command) => {
2204
2211
  const output = (data) => {
2205
2212
  if (typeof data === "object") {
2206
2213
  process.stdout.write(JSON.stringify(data, null, 2) + `
@@ -2211,7 +2218,12 @@ var createCommand2 = (opts) => {
2211
2218
  }
2212
2219
  };
2213
2220
  try {
2214
- const args = options.args ? parseArgs(options.args) : {};
2221
+ let args = {};
2222
+ if (options.args) {
2223
+ args = parseArgs(options.args);
2224
+ } else if (passedArgs.length > 0) {
2225
+ args = parseArgs(passedArgs.join(" "));
2226
+ }
2215
2227
  const res = await app.run({ path, key: route.key, payload: args }, { appId: app.appId });
2216
2228
  if (res.code === 200) {
2217
2229
  output(res.data);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@kevisual/router",
4
- "version": "0.1.0",
4
+ "version": "0.1.1",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
package/src/commander.ts CHANGED
@@ -22,8 +22,12 @@ export const parseArgs = (args: string) => {
22
22
  for (const pair of pairs) {
23
23
  const idx = pair.indexOf('=');
24
24
  const key = pair.slice(0, idx);
25
- const value = pair.slice(idx + 1);
26
- result[key] = value;
25
+ const raw = pair.slice(idx + 1);
26
+ let value: string | number | boolean = raw;
27
+ if (raw === 'true') value = true;
28
+ else if (raw === 'false') value = false;
29
+ else if (raw !== '' && !isNaN(Number(raw))) value = Number(raw);
30
+ result[key] = value as string;
27
31
  }
28
32
  return result;
29
33
  }
@@ -77,8 +81,9 @@ export const createCommand = (opts: { app: App, program: Command }) => {
77
81
  const description = parseDescription(route);
78
82
  subProgram.command(route.key)
79
83
  .description(description || '')
80
- .option('--args <args>', 'JSON字符串参数,传递给命令执行')
81
- .action(async (options) => {
84
+ .option('--args <args>', '命令参数,支持 JSON 格式或 key=value 形式,例如: --args \'{"a":1}\' 或 --args \'a=1 b=2\'')
85
+ .argument('[args...]', '位置参数(推荐通过 -- 分隔传入),支持 JSON 或 key=value 格式,例如: -- a=1 b=2 或 -- \'{"a":1}\'')
86
+ .action(async (passedArgs: string[], options, _command) => {
82
87
  const output = (data: any) => {
83
88
  if (typeof data === 'object') {
84
89
  process.stdout.write(JSON.stringify(data, null, 2) + '\n');
@@ -87,7 +92,12 @@ export const createCommand = (opts: { app: App, program: Command }) => {
87
92
  }
88
93
  }
89
94
  try {
90
- const args = options.args ? parseArgs(options.args) : {};
95
+ let args: Record<string, any> = {};
96
+ if (options.args) {
97
+ args = parseArgs(options.args);
98
+ } else if (passedArgs.length > 0) {
99
+ args = parseArgs(passedArgs.join(' '));
100
+ }
91
101
  // 这里可以添加实际的命令执行逻辑,例如调用对应的路由处理函数
92
102
  const res = await app.run({ path, key: route.key, payload: args }, { appId: app.appId });
93
103
  if (res.code === 200) {