diff --git a/package.json b/package.json index 5dd01a2..9837001 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4320f21..4dfdb8e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,7 +5,6 @@ specifiers: '@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 @@ -16,7 +15,6 @@ devDependencies: '@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 typescript: 4.5.4 diff --git a/src/commands/list.ts b/src/commands/list.ts index 73b91dc..722cdf3 100644 --- a/src/commands/list.ts +++ b/src/commands/list.ts @@ -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) { @@ -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 ( @@ -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)) } } }) diff --git a/src/fs.ts b/src/fs.ts index 57f4bcb..603e3a8 100644 --- a/src/fs.ts +++ b/src/fs.ts @@ -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 }) @@ -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 +}