Skip to content

Commit

Permalink
feat: category option and count summary
Browse files Browse the repository at this point in the history
  • Loading branch information
hyoban committed Jan 17, 2024
1 parent 01eaf48 commit 055599e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
12 changes: 11 additions & 1 deletion src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,27 @@ async function main() {
type: "boolean",
default: false,
})
.option("category", {
alias: "c",
type: "string",
default: "repository" as const,
choices: ["repository", "package"] as const,
})
.parse()

const targets = argv._.map(String)

for (const target of targets) {
try {
const { result } = await getDependents(target, {
const { result, total } = await getDependents(target, {
limit: argv.limit,
timeout: argv.timeout,
silent: argv.silent,
category: argv.category,
})
consola.success(
`${target} Dependents Summary: ${total.repositories} repositories, ${total.packages} packages`,
)
console.table(result)
} catch (error) {
consola.error(error)
Expand Down
50 changes: 42 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export type DependentInfo = {
export type ParseResult = {
nextUrl?: string
result: DependentInfo[]
total: {
repositories: number
packages: number
}
}

function toNumber(value?: string): number {
Expand All @@ -21,6 +25,19 @@ function toNumber(value?: string): number {
export async function parseDependents(url: string): Promise<ParseResult> {
const html = await $fetch<string>(url)
const $ = load(html)
const totalRepositories = toNumber(
$(`#dependents .Box-header .btn-link[href*="dependent_type=REPOSITORY"]`)
.text()
.trim()
.split(" ")[0],
)
const totalPackages = toNumber(
$(`#dependents .Box-header .btn-link[href*="dependent_type=PACKAGE"]`)
.text()
.trim()
.split(" ")[0],
)

const dependentDomList = $(
"#dependents .Box [data-test-id='dg-repo-pkg-dependent']",
)
Expand Down Expand Up @@ -53,24 +70,29 @@ export async function parseDependents(url: string): Promise<ParseResult> {
return {
result: dependentList,
nextUrl,
total: {
repositories: totalRepositories,
packages: totalPackages,
},
}
}

export type CliOptions = {
limit?: number
timeout?: number
silent?: boolean
category?: "repository" | "package"
}

export type GetDependentsOptions = CliOptions & {
filter?: (item: DependentInfo) => boolean
resume?: ParseResult | null
silent?: boolean
}

export async function getDependents(
target: string,
options?: GetDependentsOptions,
) {
): Promise<ParseResult> {
const user = target.split("/")[0]
const repo = target.split("/")[1]
if (!user || !repo) {
Expand All @@ -82,6 +104,7 @@ export async function getDependents(
const timeout = options?.timeout ?? 8000
const progressCache = options?.resume
const enableConsole = !options?.silent
const category = options?.category

const hasCache = !!progressCache

Expand All @@ -90,7 +113,13 @@ export async function getDependents(
result: [],
nextUrl: hasCache
? progressCache.nextUrl
: `https://github.com/${target}/network/dependents`,
: `https://github.com/${target}/network/dependents${
category ? `?dependent_type=${category.toUpperCase()}` : ""
}`,
total: {
repositories: 0,
packages: 0,
},
}

if (enableConsole) {
Expand All @@ -106,19 +135,24 @@ export async function getDependents(
}
currentParseResult = await parseDependents(currentParseResult.nextUrl)
finalResult.push(...currentParseResult.result)
finalResult = finalResult
.filter(
(element, index, array) =>
array.findIndex((item) => item.repository === element.repository) ===
index,
)
.sort((a, b) => b.stars - a.stars)
.filter((element) => filter(element))
.slice(0, limit)
}

finalResult = finalResult
.sort((a, b) => b.stars - a.stars)
.filter((element) => filter(element))
.slice(0, limit)

if (currentParseResult.nextUrl) {
consola.info("Exceed timeout, stop parsing")
}

return {
result: finalResult,
nextUrl: currentParseResult.nextUrl,
total: currentParseResult.total,
}
}

0 comments on commit 055599e

Please sign in to comment.