Skip to content

Commit 84a8457

Browse files
authored
fix: no fast-glob (#8)
1 parent 0a7a61e commit 84a8457

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"@types/node": "^18.0.3",
3232
"cac": "^6.7.12",
3333
"esno": "^0.16.3",
34-
"fast-glob": "^3.2.11",
3534
"prettier": "2.5.1",
3635
"tsup": "6.0.1",
3736
"typescript": "4.5.4",

pnpm-lock.yaml

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/list.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { PluginApi } from '../types'
2-
import fg from 'fast-glob'
32
import path from 'path'
43
import { rootPath } from '../shared'
54
import { analyzeUrl } from '../shared/url'
6-
import { Entry } from 'fast-glob/out/types'
5+
import { collectDirs } from '../fs'
6+
import { PathLike } from 'fs'
77

88
export const list: PluginApi = {
99
extend(api) {
@@ -13,16 +13,18 @@ export const list: PluginApi = {
1313
default: false,
1414
})
1515
.action(async (query, { fullPath }) => {
16-
let entries = await fg(path.join(rootPath, '*', '*', '*'), {
17-
onlyDirectories: true,
18-
objectMode: true,
19-
dot: true,
20-
})
16+
/**
17+
* deep `4` means:
18+
* 1 ~/ghq
19+
* 2 github.com
20+
* 3 user
21+
* 4 repo
22+
*/
23+
let entries = await collectDirs(rootPath, 4)
2124

2225
if (query) {
2326
entries = entries.filter((entry) => {
24-
// github.com/uer/name
25-
const repoPath = relativeRootPath(entry.path)
27+
const repoPath = relativeRootPath(entry)
2628
const repo = analyzeUrl(repoPath)
2729

2830
return (
@@ -36,15 +38,15 @@ export const list: PluginApi = {
3638
print(entry)
3739
}
3840

39-
function relativeRootPath(entryPath: Entry['path']) {
40-
return path.relative(rootPath, entryPath)
41+
function relativeRootPath(entryPath: PathLike) {
42+
return path.relative(rootPath, entryPath.toString())
4143
}
4244

43-
function print(entry: Entry) {
45+
function print(entry: PathLike) {
4446
if (fullPath) {
45-
console.info(entry.path)
47+
console.info(entry)
4648
} else {
47-
console.info(relativeRootPath(entry.path))
49+
console.info(relativeRootPath(entry))
4850
}
4951
}
5052
})

src/fs.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { PathLike } from 'fs'
22
import fs from 'fs/promises'
3+
import path from 'path'
34

45
export async function makeDir(dirPath: PathLike) {
56
return await fs.mkdir(dirPath, { recursive: true })
@@ -11,3 +12,43 @@ export async function existsDir(dirPath: PathLike) {
1112
.then(() => true)
1213
.catch(() => false)
1314
}
15+
16+
export async function collectDirs(dirPath: PathLike, deep = 1) {
17+
let currentDeep = 0
18+
19+
const resCollect: PathLike[] = []
20+
21+
await read(dirPath)
22+
23+
async function read(currentDirPath: typeof dirPath) {
24+
currentDeep += 1
25+
26+
if (currentDeep === deep) {
27+
resCollect.push(currentDirPath)
28+
return
29+
}
30+
31+
const dirDirents = (
32+
await fs.readdir(currentDirPath, { withFileTypes: true })
33+
).filter((dirent) => dirent.isDirectory())
34+
35+
for (let index = 0; index < dirDirents.length; index++) {
36+
const dirent = dirDirents[index]
37+
38+
await read(path.join(currentDirPath.toString(), dirent.name))
39+
40+
/**
41+
* if this dirent is the last directory
42+
* then END THIS loop, mark `currentDeep` to
43+
* previous deep
44+
*/
45+
if (dirDirents.length >= index + 1) {
46+
currentDeep -= 1
47+
48+
continue
49+
}
50+
}
51+
}
52+
53+
return resCollect
54+
}

0 commit comments

Comments
 (0)