|
| 1 | +import fetch from 'node-fetch'; |
| 2 | + |
| 3 | +const owner = 'LineIndent'; |
| 4 | +const repoName = 'reflexify'; |
| 5 | + |
| 6 | +// GitHub REST API URL to get the repository information |
| 7 | +const apiUrl = `https://api.github.com/repos/${owner}/${repoName}`; |
| 8 | + |
| 9 | +// Function to format a number in shorthand like 'k' |
| 10 | +function formatNumberShorthand(number) { |
| 11 | + if (number >= 1000) { |
| 12 | + return (number / 1000).toFixed(1) + 'k'; |
| 13 | + } |
| 14 | + return number.toString(); |
| 15 | +} |
| 16 | + |
| 17 | + |
| 18 | +// Function to fetch and display repository information, including the latest release |
| 19 | +async function getRepoInfo() { |
| 20 | + try { |
| 21 | + // Dynamically import the 'node-fetch' module as an ES module |
| 22 | + // const fetch = (await import('../node-fetch')).default; |
| 23 | + |
| 24 | + |
| 25 | + // Send an HTTP GET request to the GitHub API |
| 26 | + const response = await fetch(apiUrl); |
| 27 | + const repoInfo = await response.json(); |
| 28 | + |
| 29 | + const name = document.getElementById("name") |
| 30 | + console.log(name) |
| 31 | + |
| 32 | + |
| 33 | + // NOTE * KEEP BELOW CODE * |
| 34 | + // const response_html = await fetch(apiUrl); |
| 35 | + // const html = await response_html.text(); |
| 36 | + |
| 37 | + // // Create a DOM using jsdom |
| 38 | + // const { window } = new (await import('jsdom')).JSDOM(html); |
| 39 | + // const document = window.document; |
| 40 | + |
| 41 | + |
| 42 | + // Check if the response contains the desired information |
| 43 | + if (response.status === 200) { |
| 44 | + const starsCount = repoInfo.stargazers_count; |
| 45 | + const forksCount = repoInfo.forks_count; |
| 46 | + |
| 47 | + // NOTE * KEEP THE BELOW CODE * |
| 48 | + // // Extract the latest release version |
| 49 | + // const releaseElement = document.querySelector('.css-truncate.css-truncate-target.text-bold.mr-2'); |
| 50 | + // const latestRelease = releaseElement ? releaseElement.textContent.trim() : 'N/A'; |
| 51 | + |
| 52 | + |
| 53 | + // Fetch the latest release information |
| 54 | + const releaseUrl = `${apiUrl}/releases/latest`; |
| 55 | + const releaseResponse = await fetch(releaseUrl); |
| 56 | + const releaseInfo = await releaseResponse.json(); |
| 57 | + const latestRelease = releaseInfo.tag_name; |
| 58 | + |
| 59 | + console.log(`Stars: ${formatNumberShorthand(starsCount)}`); |
| 60 | + console.log(`Forks: ${formatNumberShorthand(forksCount)}`); |
| 61 | + console.log(`Latest Release: ${latestRelease}`); |
| 62 | + } else { |
| 63 | + console.log('Error fetching repository information.'); |
| 64 | + } |
| 65 | + } catch (error) { |
| 66 | + console.error("Error fetching data:", error); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// Call the function to get and display repository information |
| 71 | +getRepoInfo(); |
0 commit comments