Skip to content

Commit

Permalink
fix: no fast-glob (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
2nthony authored Jul 11, 2022
1 parent 0a7a61e commit 84a8457
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"@types/node": "^18.0.3",
"cac": "^6.7.12",
"esno": "^0.16.3",
"fast-glob": "^3.2.11",
"prettier": "2.5.1",
"tsup": "6.0.1",
"typescript": "4.5.4",
Expand Down
2 changes: 0 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 16 additions & 14 deletions src/commands/list.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { PluginApi } from '../types'
import fg from 'fast-glob'
import path from 'path'
import { rootPath } from '../shared'
import { analyzeUrl } from '../shared/url'
import { Entry } from 'fast-glob/out/types'
import { collectDirs } from '../fs'
import { PathLike } from 'fs'

export const list: PluginApi = {
extend(api) {
Expand All @@ -13,16 +13,18 @@ export const list: PluginApi = {
default: false,
})
.action(async (query, { fullPath }) => {
let entries = await fg(path.join(rootPath, '*', '*', '*'), {
onlyDirectories: true,
objectMode: true,
dot: true,
})
/**
* deep `4` means:
* 1 ~/ghq
* 2 github.com
* 3 user
* 4 repo
*/
let entries = await collectDirs(rootPath, 4)

if (query) {
entries = entries.filter((entry) => {
// github.com/uer/name
const repoPath = relativeRootPath(entry.path)
const repoPath = relativeRootPath(entry)
const repo = analyzeUrl(repoPath)

return (
Expand All @@ -36,15 +38,15 @@ export const list: PluginApi = {
print(entry)
}

function relativeRootPath(entryPath: Entry['path']) {
return path.relative(rootPath, entryPath)
function relativeRootPath(entryPath: PathLike) {
return path.relative(rootPath, entryPath.toString())
}

function print(entry: Entry) {
function print(entry: PathLike) {
if (fullPath) {
console.info(entry.path)
console.info(entry)
} else {
console.info(relativeRootPath(entry.path))
console.info(relativeRootPath(entry))
}
}
})
Expand Down
41 changes: 41 additions & 0 deletions src/fs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PathLike } from 'fs'
import fs from 'fs/promises'
import path from 'path'

export async function makeDir(dirPath: PathLike) {
return await fs.mkdir(dirPath, { recursive: true })
Expand All @@ -11,3 +12,43 @@ export async function existsDir(dirPath: PathLike) {
.then(() => true)
.catch(() => false)
}

export async function collectDirs(dirPath: PathLike, deep = 1) {
let currentDeep = 0

const resCollect: PathLike[] = []

await read(dirPath)

async function read(currentDirPath: typeof dirPath) {
currentDeep += 1

if (currentDeep === deep) {
resCollect.push(currentDirPath)
return
}

const dirDirents = (
await fs.readdir(currentDirPath, { withFileTypes: true })
).filter((dirent) => dirent.isDirectory())

for (let index = 0; index < dirDirents.length; index++) {
const dirent = dirDirents[index]

await read(path.join(currentDirPath.toString(), dirent.name))

/**
* if this dirent is the last directory
* then END THIS loop, mark `currentDeep` to
* previous deep
*/
if (dirDirents.length >= index + 1) {
currentDeep -= 1

continue
}
}
}

return resCollect
}

0 comments on commit 84a8457

Please sign in to comment.