Skip to content

Commit 2806c64

Browse files
committed
refactor(grep): replace glob dependency with fs.readdirSync
- Add findFileRecursive function using native Node.js fs API - Remove glob package from dependencies - Add unit tests for findFileRecursive 🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
1 parent ed76c50 commit 2806c64

File tree

4 files changed

+126
-29
lines changed

4 files changed

+126
-29
lines changed

bun.lock

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"@code-yeongyu/comment-checker": "^0.5.0",
1111
"@openauthjs/openauth": "^0.4.3",
1212
"@opencode-ai/plugin": "^1.0.150",
13-
"glob": "^13.0.0",
1413
"hono": "^4.10.4",
1514
"picomatch": "^4.0.2",
1615
"xdg-basedir": "^5.1.0",
@@ -71,10 +70,6 @@
7170

7271
"@code-yeongyu/comment-checker": ["@code-yeongyu/[email protected]", "", { "os": [ "linux", "win32", "darwin", ], "cpu": [ "x64", "arm64", ], "bin": { "comment-checker": "bin/comment-checker" } }, "sha512-rKD2qQnTVUacsVQtpu3I5Sxi09X/XpOwS9fcmbUv1yfUL6llraaPuLmmxMBMRcmm7Zu31yEPVKCeUkVODfRL1g=="],
7372

74-
"@isaacs/balanced-match": ["@isaacs/[email protected]", "", {}, "sha512-yzMTt9lEb8Gv7zRioUilSglI0c0smZ9k5D65677DLWLtWJaXIS3CqcGyUFByYKlnUj6TkjLVs54fBl6+TiGQDQ=="],
75-
76-
"@isaacs/brace-expansion": ["@isaacs/[email protected]", "", { "dependencies": { "@isaacs/balanced-match": "^4.0.1" } }, "sha512-ZT55BDLV0yv0RBm2czMiZ+SqCGO7AvmOM3G/w2xhVPH+te0aKgFjmBvGlL1dH+ql2tgGO3MVrbb3jCKyvpgnxA=="],
77-
7873
"@openauthjs/openauth": ["@openauthjs/[email protected]", "", { "dependencies": { "@standard-schema/spec": "1.0.0-beta.3", "aws4fetch": "1.0.20", "jose": "5.9.6" }, "peerDependencies": { "arctic": "^2.2.2", "hono": "^4.0.0" } }, "sha512-RlnjqvHzqcbFVymEwhlUEuac4utA5h4nhSK/i2szZuQmxTIqbGUxZ+nM+avM+VV4Ing+/ZaNLKILoXS3yrkOOw=="],
7974

8075
"@opencode-ai/plugin": ["@opencode-ai/[email protected]", "", { "dependencies": { "@opencode-ai/sdk": "1.0.150", "zod": "4.1.8" } }, "sha512-XmY3yydk120GBv2KeLxSZlElFx4Zx9TYLa3bS9X1TxXot42UeoMLEi3Xa46yboYnWwp4bC9Fu+Gd1E7hypG8Jw=="],
@@ -129,22 +124,12 @@
129124

130125
"detect-libc": ["[email protected]", "", {}, "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ=="],
131126

132-
"glob": ["[email protected]", "", { "dependencies": { "minimatch": "^10.1.1", "minipass": "^7.1.2", "path-scurry": "^2.0.0" } }, "sha512-tvZgpqk6fz4BaNZ66ZsRaZnbHvP/jG3uKJvAZOwEVUL4RTA5nJeeLYfyN9/VA8NX/V3IBG+hkeuGpKjvELkVhA=="],
133-
134127
"hono": ["[email protected]", "", {}, "sha512-DDT0A0r6wzhe8zCGoYOmMeuGu3dyTAE40HHjwUsWFTEy5WxK1x2WDSsBPlEXgPbRIFY6miDualuUDbasPogIww=="],
135128

136129
"jose": ["[email protected]", "", {}, "sha512-AMlnetc9+CV9asI19zHmrgS/WYsWUwCn2R7RzlbJWD7F9eWYUTGyBmU9o6PxngtLGOiDGPRu+Uc4fhKzbpteZQ=="],
137130

138-
"lru-cache": ["[email protected]", "", {}, "sha512-B5Y16Jr9LB9dHVkh6ZevG+vAbOsNOYCX+sXvFWFu7B3Iz5mijW3zdbMyhsh8ANd2mSWBYdJgnqi+mL7/LrOPYg=="],
139-
140-
"minimatch": ["[email protected]", "", { "dependencies": { "@isaacs/brace-expansion": "^5.0.0" } }, "sha512-enIvLvRAFZYXJzkCYG5RKmPfrFArdLv+R+lbQ53BmIMLIry74bjKzX6iHAm8WYamJkhSSEabrWN5D97XnKObjQ=="],
141-
142-
"minipass": ["[email protected]", "", {}, "sha512-qOOzS1cBTWYF4BH8fVePDBOO9iptMnGUEZwNc/cMWnTV2nVLZ7VoNWEPHkYczZA0pdoA7dl6e7FL659nX9S2aw=="],
143-
144131
"oh-my-opencode": ["[email protected]", "", { "dependencies": { "@ast-grep/cli": "^0.40.0", "@ast-grep/napi": "^0.40.0", "@code-yeongyu/comment-checker": "^0.4.1", "@opencode-ai/plugin": "^1.0.7", "xdg-basedir": "^5.1.0", "zod": "^4.1.8" }, "peerDependencies": { "bun": ">=1.0.0" } }, "sha512-pXGGgL/7Jcz3yuGJJTI72BKern2egwfRz2LQZTBq+jl+pNCybOvGvXtFmR+WGlF8O3ZjL1wIHypBbIVuHOBzxg=="],
145132

146-
"path-scurry": ["[email protected]", "", { "dependencies": { "lru-cache": "^11.0.0", "minipass": "^7.1.2" } }, "sha512-oWyT4gICAu+kaA7QWk/jvCHWarMKNs6pXOGWKDTr7cw4IGcUbW+PeTfbaQiLGheFRpjo6O9J0PmyMfQPjH71oA=="],
147-
148133
"picomatch": ["[email protected]", "", {}, "sha512-5gTmgEY/sqK6gFXLIsQNH19lWb4ebPDLA4SdLP7dsWkIXHWlG66oPuVvXSGFPppYZz8ZDZq0dYYrbHfBCVUb1Q=="],
149134

150135
"typescript": ["[email protected]", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
"@code-yeongyu/comment-checker": "^0.5.0",
5252
"@openauthjs/openauth": "^0.4.3",
5353
"@opencode-ai/plugin": "^1.0.150",
54-
"glob": "^13.0.0",
5554
"hono": "^4.10.4",
5655
"picomatch": "^4.0.2",
5756
"xdg-basedir": "^5.1.0",

src/tools/grep/downloader.test.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { describe, test, expect, beforeEach, afterEach } from "bun:test"
2+
import { mkdirSync, rmSync, writeFileSync, existsSync } from "node:fs"
3+
import { join } from "node:path"
4+
import { tmpdir } from "node:os"
5+
6+
// Import the function we'll create to replace glob
7+
import { findFileRecursive } from "./downloader"
8+
9+
describe("findFileRecursive", () => {
10+
let testDir: string
11+
12+
beforeEach(() => {
13+
// #given - create temp directory for testing
14+
testDir = join(tmpdir(), `downloader-test-${Date.now()}`)
15+
mkdirSync(testDir, { recursive: true })
16+
})
17+
18+
afterEach(() => {
19+
// cleanup
20+
if (existsSync(testDir)) {
21+
rmSync(testDir, { recursive: true, force: true })
22+
}
23+
})
24+
25+
test("should find file in root directory", () => {
26+
// #given
27+
const targetFile = join(testDir, "rg.exe")
28+
writeFileSync(targetFile, "dummy content")
29+
30+
// #when
31+
const result = findFileRecursive(testDir, "rg.exe")
32+
33+
// #then
34+
expect(result).toBe(targetFile)
35+
})
36+
37+
test("should find file in nested directory (ripgrep release structure)", () => {
38+
// #given - simulate ripgrep release zip structure
39+
const nestedDir = join(testDir, "ripgrep-14.1.1-x86_64-pc-windows-msvc")
40+
mkdirSync(nestedDir, { recursive: true })
41+
const targetFile = join(nestedDir, "rg.exe")
42+
writeFileSync(targetFile, "dummy content")
43+
44+
// #when
45+
const result = findFileRecursive(testDir, "rg.exe")
46+
47+
// #then
48+
expect(result).toBe(targetFile)
49+
})
50+
51+
test("should find file in deeply nested directory", () => {
52+
// #given
53+
const deepDir = join(testDir, "level1", "level2", "level3")
54+
mkdirSync(deepDir, { recursive: true })
55+
const targetFile = join(deepDir, "rg")
56+
writeFileSync(targetFile, "dummy content")
57+
58+
// #when
59+
const result = findFileRecursive(testDir, "rg")
60+
61+
// #then
62+
expect(result).toBe(targetFile)
63+
})
64+
65+
test("should return null when file not found", () => {
66+
// #given - empty directory
67+
68+
// #when
69+
const result = findFileRecursive(testDir, "nonexistent.exe")
70+
71+
// #then
72+
expect(result).toBeNull()
73+
})
74+
75+
test("should find first match when multiple files exist", () => {
76+
// #given
77+
const dir1 = join(testDir, "dir1")
78+
const dir2 = join(testDir, "dir2")
79+
mkdirSync(dir1, { recursive: true })
80+
mkdirSync(dir2, { recursive: true })
81+
writeFileSync(join(dir1, "rg"), "first")
82+
writeFileSync(join(dir2, "rg"), "second")
83+
84+
// #when
85+
const result = findFileRecursive(testDir, "rg")
86+
87+
// #then
88+
expect(result).not.toBeNull()
89+
expect(result!.endsWith("rg")).toBe(true)
90+
})
91+
92+
test("should match exact filename, not partial", () => {
93+
// #given
94+
writeFileSync(join(testDir, "rg.exe.bak"), "backup file")
95+
writeFileSync(join(testDir, "not-rg.exe"), "wrong file")
96+
97+
// #when
98+
const result = findFileRecursive(testDir, "rg.exe")
99+
100+
// #then
101+
expect(result).toBeNull()
102+
})
103+
})

src/tools/grep/downloader.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1-
import { existsSync, mkdirSync, chmodSync, unlinkSync } from "node:fs"
1+
import { existsSync, mkdirSync, chmodSync, unlinkSync, readdirSync } from "node:fs"
22
import { join } from "node:path"
33
import { spawn } from "bun"
44

5+
export function findFileRecursive(dir: string, filename: string): string | null {
6+
try {
7+
const entries = readdirSync(dir, { withFileTypes: true, recursive: true })
8+
for (const entry of entries) {
9+
if (entry.isFile() && entry.name === filename) {
10+
return join(entry.parentPath ?? dir, entry.name)
11+
}
12+
}
13+
} catch {
14+
return null
15+
}
16+
return null
17+
}
18+
519
const RG_VERSION = "14.1.1"
620

721
const PLATFORM_CONFIG: Record<string, { platform: string; extension: "tar.gz" | "zip" } | undefined> = {
@@ -70,14 +84,12 @@ async function extractZipWindows(archivePath: string, destDir: string): Promise<
7084
throw new Error("Failed to extract zip with PowerShell")
7185
}
7286

73-
const { globSync } = await import("glob")
74-
const rgFiles = globSync("**/rg.exe", { cwd: destDir })
75-
if (rgFiles.length > 0) {
76-
const srcPath = join(destDir, rgFiles[0])
87+
const foundPath = findFileRecursive(destDir, "rg.exe")
88+
if (foundPath) {
7789
const destPath = join(destDir, "rg.exe")
78-
if (srcPath !== destPath) {
90+
if (foundPath !== destPath) {
7991
const { renameSync } = await import("node:fs")
80-
renameSync(srcPath, destPath)
92+
renameSync(foundPath, destPath)
8193
}
8294
}
8395
}
@@ -92,14 +104,12 @@ async function extractZipUnix(archivePath: string, destDir: string): Promise<voi
92104
throw new Error("Failed to extract zip")
93105
}
94106

95-
const { globSync } = await import("glob")
96-
const rgFiles = globSync("**/rg", { cwd: destDir })
97-
if (rgFiles.length > 0) {
98-
const srcPath = join(destDir, rgFiles[0])
107+
const foundPath = findFileRecursive(destDir, "rg")
108+
if (foundPath) {
99109
const destPath = join(destDir, "rg")
100-
if (srcPath !== destPath) {
110+
if (foundPath !== destPath) {
101111
const { renameSync } = await import("node:fs")
102-
renameSync(srcPath, destPath)
112+
renameSync(foundPath, destPath)
103113
}
104114
}
105115
}

0 commit comments

Comments
 (0)