simplify: extract timeAgo and fetchGitHubActivity to shared helpers

This commit is contained in:
Rohit Ghumare
2026-04-11 10:50:59 +01:00
parent 8cfae7e105
commit a4540c5fc1
3 changed files with 39 additions and 71 deletions
+4 -36
View File
@@ -1,8 +1,8 @@
import { existsSync } from 'node:fs';
import { existsSync, statSync } from 'node:fs';
import { join } from 'node:path';
import { Command, Option } from 'clipanion';
import { findAllSkills, findSkill, detectProvider, isLocalPath, evaluateSkillDirectory } from '@skillkit/core';
import { getSearchDirs, loadSkillMetadata, formatCount } from '../helpers.js';
import { getSearchDirs, loadSkillMetadata, formatCount, timeAgo, fetchGitHubActivity } from '../helpers.js';
import {
colors,
symbols,
@@ -133,7 +133,6 @@ export class CheckCommand extends Command {
const installedSkillMd = join(skill.path, 'SKILL.md');
if (existsSync(sourceSkillMd) && existsSync(installedSkillMd)) {
const { statSync } = await import('node:fs');
const sourceTime = statSync(sourceSkillMd).mtime;
const installedTime = statSync(installedSkillMd).mtime;
@@ -168,7 +167,7 @@ export class CheckCommand extends Command {
let pushedAt: string | null = null;
if (parsed && provider.name === 'GitHub') {
const activity = await this.fetchGitHubActivity(parsed.owner, parsed.repo);
const activity = await fetchGitHubActivity(parsed.owner, parsed.repo);
if (activity) {
stars = activity.stars;
pushedAt = activity.pushedAt;
@@ -266,41 +265,10 @@ export class CheckCommand extends Command {
parts.push(`${formatCount(r.stars)} stars`);
}
if (r.pushedAt) {
parts.push(`pushed ${this.timeAgo(r.pushedAt)}`);
parts.push(`pushed ${timeAgo(r.pushedAt)}`);
}
if (parts.length === 0) return '';
return ` ${colors.muted(`(${parts.join(', ')})`)}`;
}
private timeAgo(dateStr: string): string {
const now = Date.now();
const then = new Date(dateStr).getTime();
if (Number.isNaN(then)) return 'unknown';
const days = Math.floor((now - then) / 86_400_000);
if (days === 0) return 'today';
if (days === 1) return 'yesterday';
if (days < 30) return `${days}d ago`;
if (days < 365) return `${Math.floor(days / 30)}mo ago`;
return `${Math.floor(days / 365)}y ago`;
}
private async fetchGitHubActivity(
owner: string,
repo: string,
): Promise<{ stars: number; pushedAt: string | null } | null> {
try {
const response = await fetch(
`https://api.github.com/repos/${owner}/${repo}`,
{ signal: AbortSignal.timeout(5000) },
);
if (!response.ok) return null;
const data = (await response.json()) as Record<string, unknown>;
return {
stars: typeof data.stargazers_count === 'number' ? data.stargazers_count : 0,
pushedAt: typeof data.pushed_at === 'string' ? data.pushed_at : null,
};
} catch {
return null;
}
}
}
+3 -35
View File
@@ -25,7 +25,7 @@ import {
Severity,
evaluateSkillDirectory,
} from "@skillkit/core";
import { formatCount } from "../helpers.js";
import { formatCount, timeAgo, fetchGitHubActivity } from "../helpers.js";
function sanitizeSkillName(name: string): string | null {
if (!name || typeof name !== "string") return null;
@@ -496,7 +496,7 @@ export class PublishSubmitCommand extends Command {
const qualityGrade = qualityScore !== null ? getQualityGradeFromScore(qualityScore) : null;
const qualityBadge = qualityScore !== null ? formatQualityBadge(qualityScore) : "N/A";
const activity = await this.fetchGitHubActivity(repoInfo.owner, repoInfo.repo);
const activity = await fetchGitHubActivity(repoInfo.owner, repoInfo.repo);
const skillEntry = {
id: `${repoInfo.owner}/${repoInfo.repo}/${skillSlug}`,
@@ -517,7 +517,7 @@ export class PublishSubmitCommand extends Command {
console.log(colors.muted(` Quality: ${qualityBadge}${qualityScore !== null ? ` (${qualityScore}/100)` : ""}`));
if (activity) {
const stars = formatCount(activity.stars);
const pushed = activity.pushedAt ? this.timeAgo(activity.pushedAt) : "unknown";
const pushed = activity.pushedAt ? timeAgo(activity.pushedAt) : "unknown";
console.log(colors.muted(` Stars: ${stars}`));
console.log(colors.muted(` Last push: ${pushed}`));
}
@@ -660,38 +660,6 @@ export class PublishSubmitCommand extends Command {
.join(" ");
}
private async fetchGitHubActivity(
owner: string,
repo: string,
): Promise<{ stars: number; pushedAt: string | null } | null> {
try {
const response = await fetch(
`https://api.github.com/repos/${owner}/${repo}`,
{ signal: AbortSignal.timeout(5000) },
);
if (!response.ok) return null;
const data = (await response.json()) as Record<string, unknown>;
return {
stars: typeof data.stargazers_count === "number" ? data.stargazers_count : 0,
pushedAt: typeof data.pushed_at === "string" ? data.pushed_at : null,
};
} catch {
return null;
}
}
private timeAgo(dateStr: string): string {
const now = Date.now();
const then = new Date(dateStr).getTime();
if (Number.isNaN(then)) return "unknown";
const days = Math.floor((now - then) / 86_400_000);
if (days === 0) return "today";
if (days === 1) return "yesterday";
if (days < 30) return `${days}d ago`;
if (days < 365) return `${Math.floor(days / 30)}mo ago`;
return `${Math.floor(days / 365)}y ago`;
}
private createIssueBody(
skill: {
id: string;
+32
View File
@@ -48,3 +48,35 @@ export function formatCount(count: number): string {
if (count >= 1_000) return `${(count / 1_000).toFixed(1)}K`;
return String(count);
}
export function timeAgo(dateStr: string): string {
const now = Date.now();
const then = new Date(dateStr).getTime();
if (Number.isNaN(then)) return 'unknown';
const days = Math.floor((now - then) / 86_400_000);
if (days === 0) return 'today';
if (days === 1) return 'yesterday';
if (days < 30) return `${days}d ago`;
if (days < 365) return `${Math.floor(days / 30)}mo ago`;
return `${Math.floor(days / 365)}y ago`;
}
export async function fetchGitHubActivity(
owner: string,
repo: string,
): Promise<{ stars: number; pushedAt: string | null } | null> {
try {
const response = await fetch(
`https://api.github.com/repos/${owner}/${repo}`,
{ signal: AbortSignal.timeout(5000) },
);
if (!response.ok) return null;
const data = (await response.json()) as Record<string, unknown>;
return {
stars: typeof data.stargazers_count === 'number' ? data.stargazers_count : 0,
pushedAt: typeof data.pushed_at === 'string' ? data.pushed_at : null,
};
} catch {
return null;
}
}