Skip to content

Commit d3feb6a

Browse files
committed
feat: remove code duplicate
1 parent 7185b22 commit d3feb6a

File tree

2 files changed

+2
-57
lines changed

2 files changed

+2
-57
lines changed

scripts/merge.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function extractGitHubInfo(url) {
3232
}
3333

3434
// Function to fetch GitHub repository data
35-
async function fetchGitHubRepoData(url) {
35+
export async function fetchGitHubRepoData(url) {
3636
try {
3737
const repoInfo = extractGitHubInfo(url);
3838
if (!repoInfo) return null;

scripts/update-authors.js

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'fs';
22
import path from 'path';
33
import { fileURLToPath } from 'url';
4-
import fetch from 'node-fetch';
4+
import { fetchGitHubRepoData } from './merge.js'; // Import the function to fetch GitHub data
55

66
// Get directory name in ES modules
77
const __filename = fileURLToPath(import.meta.url);
@@ -11,61 +11,6 @@ const __dirname = path.dirname(__filename);
1111
const rootDir = path.resolve(__dirname, '..');
1212
const dataDir = path.join(rootDir, 'data');
1313

14-
// Helper function to extract GitHub info from URL
15-
function extractGitHubInfo(url) {
16-
if (!url || !url.includes('github.com')) return null;
17-
18-
try {
19-
const urlObj = new URL(url);
20-
if (urlObj.hostname !== 'github.com') return null;
21-
22-
const path = urlObj.pathname.replace(/^\/|\/$/g, '');
23-
const [owner, repo] = path.split('/');
24-
25-
if (!owner || !repo) return null;
26-
27-
return { owner, repo };
28-
} catch (e) {
29-
return null;
30-
}
31-
}
32-
33-
// Function to fetch GitHub repository data
34-
async function fetchGitHubRepoData(url) {
35-
try {
36-
const repoInfo = extractGitHubInfo(url);
37-
if (!repoInfo) return null;
38-
39-
const { owner, repo } = repoInfo;
40-
41-
// Add a small random delay to avoid hitting rate limits
42-
await new Promise(resolve => setTimeout(resolve, 500 + Math.random() * 500));
43-
44-
console.log(`Fetching GitHub data for ${owner}/${repo}...`);
45-
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}`);
46-
47-
if (!response.ok) {
48-
if (response.status === 403) {
49-
console.warn('GitHub API rate limit exceeded');
50-
}
51-
console.error(`Error fetching data for ${owner}/${repo}: HTTP ${response.status}`);
52-
return null;
53-
}
54-
55-
const data = await response.json();
56-
57-
return {
58-
authorName: data.owner?.login || owner,
59-
authorLink: data.owner?.html_url || `https://github.com/${owner}`,
60-
stars: data.stargazers_count || 0,
61-
forks: data.forks_count || 0
62-
};
63-
} catch (error) {
64-
console.error('Error fetching GitHub repo data:', error);
65-
return null;
66-
}
67-
}
68-
6914
// Get all JSON files in the data directory (excluding index.json and _template.json)
7015
const jsonFiles = fs.readdirSync(dataDir)
7116
.filter(file => file.endsWith('.json') && file !== 'index.json' && file !== '_template.json');

0 commit comments

Comments
 (0)