From 7a730bea41ce282a88d53b7d25f03fbfec98c7d0 Mon Sep 17 00:00:00 2001 From: Superchupu <53496941+SuperchupuDev@users.noreply.github.com> Date: Wed, 31 Jul 2024 10:53:23 +0000 Subject: [PATCH] Replace `glob` with faster alternative --- package.json | 4 +++- src/cli.ts | 50 ++++++++++++++++++++++++++------------------------ yarn.lock | 49 +++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 72 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 06d73b4d..64785279 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "@types/mocha": "^9.1.1", "@types/mz": "^2.7.4", "@types/node": "^20.3.2", + "@types/picomatch": "^3.0.0", "@typescript-eslint/eslint-plugin": "^5.60.1", "@typescript-eslint/parser": "^5.60.1", "chalk": "^4", @@ -73,9 +74,10 @@ "dependencies": { "@jridgewell/gen-mapping": "^0.3.2", "commander": "^4.0.0", - "glob": "^10.3.10", + "fdir": "^6.2.0", "lines-and-columns": "^1.1.6", "mz": "^2.7.0", + "picomatch": "^4.0.2", "pirates": "^4.0.1", "ts-interface-checker": "^0.1.9" }, diff --git a/src/cli.ts b/src/cli.ts index cda0011f..84453f8c 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,8 +1,9 @@ /* eslint-disable no-console */ import commander from "commander"; -import {glob} from "glob"; +import {fdir as Fdir} from "fdir"; import {exists, mkdir, readdir, readFile, stat, writeFile} from "mz/fs"; import {dirname, join, relative} from "path"; +import picomatch from "picomatch"; import {type Options, transform} from "./index"; @@ -212,30 +213,31 @@ async function runGlob(options: CLIOptions): Promise> { } } if (include) { - for (const pattern of include) { - const globFiles = await glob(join(absProject, pattern)); - for (const file of globFiles) { - if (!file.endsWith(".ts") && !file.endsWith(".js")) { - continue; - } - if (file.endsWith(".d.ts")) { - continue; - } - - const relativeFile = relative(absProject, file); - const outFile = join(options.outDirPath, relativeFile); - const outPath = outFile.replace(/\.\w+$/, `.${options.outExtension}`); - - const outDir = dirname(outPath); - if (!outDirs.includes(outDir)) { - outDirs.push(outDir); - } - - foundFiles.push({ - srcPath: file, - outPath, - }); + const matcher = picomatch( + include.map((p: string) => join(absProject, p)), + { + ignore: ["**/*.!(js|ts)|(d.ts)"], + }, + ); + const globFiles = await new Fdir() + .withBasePath() + .filter((file) => matcher(file)) + .crawl(process.cwd()) + .withPromise(); + for (const file of globFiles) { + const relativeFile = relative(absProject, file); + const outFile = join(options.outDirPath, relativeFile); + const outPath = outFile.replace(/\.\w+$/, `.${options.outExtension}`); + + const outDir = dirname(outPath); + if (!outDirs.includes(outDir)) { + outDirs.push(outDir); } + + foundFiles.push({ + srcPath: file, + outPath, + }); } } diff --git a/yarn.lock b/yarn.lock index 3c4d5629..f43d85c0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -387,6 +387,11 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-20.3.2.tgz#fa6a90f2600e052a03c18b8cb3fd83dd4e599898" integrity sha512-vOBLVQeCQfIcF/2Y7eKFTqrMnizK5lRNQ7ykML/5RuwVXVWxYkgwS7xbt4B6fKCUPgbSL5FSsjHQpaGQP/dQmw== +"@types/picomatch@^3.0.0": + version "3.0.0" + resolved "https://registry.yarnpkg.com/@types/picomatch/-/picomatch-3.0.0.tgz#7b746b59c4a72b4f123a5a2a011ed69ddb6b1dba" + integrity sha512-iX/Qwk9vU17N/5Q7QrV46wzciloTdCqTRt6z8A7uFFADM2+Sy5oQh9ldZhAiTXH+l0sM/EkXatEhJIs8FUyOBQ== + "@types/semver@^7.3.12": version "7.5.0" resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.5.0.tgz#591c1ce3a702c45ee15f47a42ade72c2fd78978a" @@ -1329,6 +1334,11 @@ fastq@^1.6.0: dependencies: reusify "^1.0.4" +fdir@^6.2.0: + version "6.2.0" + resolved "https://registry.yarnpkg.com/fdir/-/fdir-6.2.0.tgz#9120f438d566ef3e808ca37864d9dd18e1a4f9b5" + integrity sha512-9XaWcDl0riOX5j2kYfy0kKdg7skw3IY6kA4LFT8Tk2yF9UdrADUy8D6AJuBLtf7ISm/MksumwAHE3WVbMRyCLw== + file-entry-cache@^6.0.1: version "6.0.1" resolved "https://registry.yarnpkg.com/file-entry-cache/-/file-entry-cache-6.0.1.tgz#211b2dd9659cb0394b073e7323ac3c933d522027" @@ -2515,6 +2525,11 @@ picomatch@^2.0.4, picomatch@^2.2.1, picomatch@^2.3.1: resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42" integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA== +picomatch@^4.0.2: + version "4.0.2" + resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-4.0.2.tgz#77c742931e8f3b8820946c76cd0c1f13730d1dab" + integrity sha512-M7BAV6Rlcy5u+m6oPhAPFgJTzAioX/6B0DxyvDlo9l8+T3nLKbrczg2WLUyzd45L8RqfUMyGPzekbMvX2Ldkwg== + pirates@^4.0.1: version "4.0.6" resolved "https://registry.yarnpkg.com/pirates/-/pirates-4.0.6.tgz#3018ae32ecfcff6c29ba2267cbf21166ac1f36b9" @@ -2844,8 +2859,7 @@ stream-events@^1.0.5: dependencies: stubs "^3.0.0" -"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0: - name string-width-cjs +"string-width-cjs@npm:string-width@^4.2.0": version "4.2.3" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== @@ -2863,6 +2877,15 @@ string-width@^3.0.0, string-width@^3.1.0: is-fullwidth-code-point "^2.0.0" strip-ansi "^5.1.0" +string-width@^4.1.0, string-width@^4.2.0: + version "4.2.3" + resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" + integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== + dependencies: + emoji-regex "^8.0.0" + is-fullwidth-code-point "^3.0.0" + strip-ansi "^6.0.1" + string-width@^5.0.1, string-width@^5.1.2: version "5.1.2" resolved "https://registry.yarnpkg.com/string-width/-/string-width-5.1.2.tgz#14f8daec6d81e7221d2a357e668cab73bdbca794" @@ -2906,8 +2929,7 @@ string_decoder@~1.1.1: dependencies: safe-buffer "~5.1.0" -"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1: - name strip-ansi-cjs +"strip-ansi-cjs@npm:strip-ansi@^6.0.1": version "6.0.1" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== @@ -2921,6 +2943,13 @@ strip-ansi@^5.0.0, strip-ansi@^5.1.0, strip-ansi@^5.2.0: dependencies: ansi-regex "^4.1.0" +strip-ansi@^6.0.0, strip-ansi@^6.0.1: + version "6.0.1" + resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" + integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== + dependencies: + ansi-regex "^5.0.1" + strip-ansi@^7.0.1: version "7.1.0" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45" @@ -3268,8 +3297,7 @@ workerpool@6.2.1: resolved "https://registry.yarnpkg.com/workerpool/-/workerpool-6.2.1.tgz#46fc150c17d826b86a008e5a4508656777e9c343" integrity sha512-ILEIE97kDZvF9Wb9f6h5aXK4swSlKGUcOEGiIYb2OOu/IrDU9iwj0fD//SsA6E5ibwJxpEvhullJY4Sl4GcpAw== -"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0: - name wrap-ansi-cjs +"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0": version "7.0.0" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== @@ -3296,6 +3324,15 @@ wrap-ansi@^6.2.0: string-width "^4.1.0" strip-ansi "^6.0.0" +wrap-ansi@^7.0.0: + version "7.0.0" + resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" + integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== + dependencies: + ansi-styles "^4.0.0" + string-width "^4.1.0" + strip-ansi "^6.0.0" + wrap-ansi@^8.1.0: version "8.1.0" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"