From 430c73b1dbb9d0c1c704fca199823b52d4f2d651 Mon Sep 17 00:00:00 2001 From: "loong.woo" Date: Thu, 28 Nov 2024 13:52:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20filter=20=E9=80=89=E9=A1=B9=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20micromatch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/core.ts | 29 ++++++++--------------------- test/cli.test.ts | 8 ++++---- 3 files changed, 13 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index bcedefd..ad50b76 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,7 @@ App({ | client | | string | axios | http request client. When `lang` is `ts/js`, the possible values ​​are: `axios`, `wechat`, `fetch`. | | lang | l | string | ts | Language, used for output file suffix. | | tag | | number | | Specify the tag from the interface url. By default, the first tag is read for the file name. | -| filter | f | string[] | | Filter interfaces. Interfaces that meet the filter conditions will be generated. Example: `--filter "/pet/*"`, generate an interface for `/pet`, and support multiple `--filter` | +| filter | f | string[] | | Filter interfaces. Interfaces that meet the filter conditions will be generated. Example: `--filter "/pet*"`, generate an interface for `/pet`, and support multiple `--filter`. For more usage information, please refer to [micromatch](https://github.com/micromatch/micromatch) | | conjunction | c | string | By | The method's connector, the default value is `By`. | | version | v | boolean | | Output version information. | | help | h | boolean | | Output help information. | diff --git a/src/core.ts b/src/core.ts index 3e4689a..3e666c7 100644 --- a/src/core.ts +++ b/src/core.ts @@ -1,3 +1,5 @@ +import micromatch from "npm:micromatch"; + import Logs from "./console.ts"; import type { IDefaultObject, @@ -411,21 +413,6 @@ const getPathVirtualProperty = ( return value; }; -const parserFilter = (filters: string[]) => { - if (!filters.length) return undefined; - - const regStr = filters.reduce((prev, current) => { - const _str = current.replace(/\//g, "\\/").replace(/\*/g, ".*").replace( - /\?/g, - "\\?", - ); - - return `${prev ? `${prev}|` : ""}(${_str})`; - }, ""); - - return new RegExp(regStr); -}; - /** * 获取接口地址对象 * @param paths - 接口地址 @@ -439,18 +426,18 @@ export const getApiPath = ( Object.keys(paths).forEach((url) => { // 过滤接口,符合过滤条件的接口会被生成 - if (options?.filter?.length && !parserFilter(options.filter)?.test(url)) { - return; - } + if ( + options?.filter?.length && !micromatch.all(url, options.filter, { + bash: true, + }) + ) return; // 请求方式 const methods = paths[url]; Object.keys(methods).forEach((method) => { // url去除 `?` 之后的字符 - if (url.includes("?")) { - url = url.slice(0, url.indexOf("?")); - } + if (url.includes("?")) url = url.slice(0, url.indexOf("?")); const currentMethod = methods[method]; // 方法名 diff --git a/test/cli.test.ts b/test/cli.test.ts index 024f71a..4a3bccb 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -37,13 +37,13 @@ Deno.test("filter", async () => { "-A", "src/main.ts", "--url=https://petstore3.swagger.io/api/v3/openapi.json", - "--filter=/pet/*", - "-f=/user/*", + "--filter=/user*", + "-f=!/user/createWithList", ], }); - const { code } = await command.output(); + const { code, stdout } = await command.output(); - // console.log(new TextDecoder().decode(stdout)); + console.log(new TextDecoder().decode(stdout)); assertEquals(0, code); });