diff --git a/.github/actions/update-stars/main.js b/.github/actions/update-stars/main.js index d2dfc62..a10690e 100644 --- a/.github/actions/update-stars/main.js +++ b/.github/actions/update-stars/main.js @@ -1,5 +1,8 @@ import core from '@actions/core'; -import { readFileSync, writeFileSync } from 'node:fs'; +import { writeFileSync } from 'node:fs'; +import components from '../src/routes/components/components.json' assert { type: 'json' }; +import templates from '../src/routes/templates/templates.json' assert { type: 'json' }; +import tools from '../src/routes/tools/tools.json' assert { type: 'json' }; import { fetch } from 'undici'; const ghGraphQlUrl = 'https://api.github.com/graphql'; @@ -28,10 +31,6 @@ async function doGraphQlQuery(url, query, headers = {}) { } function gatherUrls() { - let components = JSON.parse(readFileSync('src/routes/components/components.json')); - let tools = JSON.parse(readFileSync('src/routes/tools/tools.json')); - let templates = JSON.parse(readFileSync('src/routes/templates/templates.json')); - return [ ...components.map((component) => component.repository), ...tools.map((tool) => tool.repository), @@ -179,7 +178,7 @@ async function main() { 0 )} stars)` ); - writeFileSync('src/lib/stars.json', JSON.stringify({ github, gitlab })); + writeFileSync('src/lib/data/stars.json', JSON.stringify({ github, gitlab })); } try { diff --git a/.github/workflows/fetch-latest-data.yml b/.github/workflows/fetch-latest-data.yml index a02f5e3..2b99b84 100644 --- a/.github/workflows/fetch-latest-data.yml +++ b/.github/workflows/fetch-latest-data.yml @@ -1,4 +1,4 @@ -name: Fetch latest data +name: Update data on: schedule: @@ -30,10 +30,10 @@ jobs: - name: Create Pull Request uses: peter-evans/create-pull-request@v5 with: - commit-message: "(AUTO) Update stars" - title: "🤖 Update stars" - body: Update all stars count from Github and Gitlab - branch: ci-update-stars - add-paths: src/lib/stars.json + commit-message: "(AUTO) Update data" + title: "🤖 Update data" + body: Automatically fetch latest data from NPM, GitHub and GitLab + branch: ci-update-data + add-paths: src/lib/data/npm.json,src/lib/data/stars.json delete-branch: true token: ${{ secrets.GITHUB_TOKEN }} diff --git a/scripts/updateNpm.js b/scripts/updateNpm.js index c956483..4fff505 100644 --- a/scripts/updateNpm.js +++ b/scripts/updateNpm.js @@ -17,7 +17,7 @@ const npm = await Promise.all( return values.reduce((result, value) => Object.assign(result, value), {}); }); -writeFileSync('src/lib/npm.json', JSON.stringify(npm)); +writeFileSync('src/lib/data/npm.json', JSON.stringify(npm)); /** @param {ReturnType[0]} pkg */ async function processPackage(pkg) { diff --git a/scripts/updateStars.js b/scripts/updateStars.js deleted file mode 100644 index 28c761c..0000000 --- a/scripts/updateStars.js +++ /dev/null @@ -1,121 +0,0 @@ -/** - * Script for updating github stars of repositories - * Usage: GH_TOKEN=secret node scripts/updateStars.js - * - * GH_TOKEN - personal access token with repo scope - */ - -import fs from 'fs'; -import { request, gql } from 'graphql-request'; -import prettier from 'prettier'; - -const files = [ - 'src/routes/components/components.json', - 'src/routes/templates/templates.json', - 'src/routes/tools/tools.json' -]; - -if (!process.env.GH_TOKEN) { - console.error('Error: env variable GH_TOKEN not set'); - process.exit(1); -} - -const getRepo = ({ url }) => { - const match = url.match(/(github.com|gitlab.com)\/([^#/]+)\/([^#/]+)/); - if (match) { - const [, site, owner, name] = match; - const id = `${site}/${owner}/${name}`; - return { site, owner, name, id }; - } -}; - -const reposWithDuplicates = files.flatMap( - (file) => - JSON.parse(fs.readFileSync(file)) - .map(getRepo) - .filter((x) => x) // filter undefined -); -const repos = getUnique(reposWithDuplicates); - -const githubRepos = repos.filter((repo) => repo.site === 'github.com'); -const gitlabRepos = repos.filter((repo) => repo.site === 'gitlab.com'); - -const gitlabRepoQuery = (repo, idx) => ` - r${idx}: project(fullPath: "${repo.owner}/${repo.name}") { - ...frag - } -`; - -const gitlabQuery = gql` - { - ${gitlabRepos.map(gitlabRepoQuery).join('')} - } - fragment frag on Project { - starCount - fullPath - } -`; - -const gitlabResponse = await request('https://gitlab.com/api/graphql', gitlabQuery); - -const ghRepoQuery = (repo, idx) => ` - r${idx}: repository(owner: "${repo.owner}", name: "${repo.name}") { - ...frag - } -`; - -const ghQuery = gql` - { - ${githubRepos.map(ghRepoQuery).join('')} - } - fragment frag on Repository { - resourcePath - stargazerCount - } -`; - -const ghResponse = await request( - 'https://api.github.com/graphql', - ghQuery, - {}, - { authorization: `token ${process.env.GH_TOKEN}` } -); - -const repoData = {}; - -for (const repo of Object.values(gitlabResponse)) { - repoData[`gitlab.com/${repo.fullPath}`] = { stars: repo.starCount }; -} - -for (const repo of Object.values(ghResponse)) { - repoData[`github.com${repo.resourcePath}`] = { stars: repo.stargazerCount }; -} - -for (const file of files) { - const data = JSON.parse(fs.readFileSync(file)); - for (const item of data) { - const repo = getRepo(item); - if (repo && repoData[repo.id]) { - item.stars = repoData[repo.id].stars; - } - } - prettySave(file, JSON.stringify(data), 'json'); -} - -/** - * Format with prettier and save - */ -function prettySave(filePath, text, parser = 'babel') { - prettier.resolveConfig(filePath).then((options) => { - const formatted = prettier.format(text, { ...options, parser }); - fs.writeFileSync(filePath, formatted); - }); -} - -function getUnique(repos) { - const urls = repos.map((repo) => `${repo.site}/${repo.owner}/${repo.name}`); - return [...new Set(urls)].map((url) => { - const [site, owner, name] = url.split('/'); - return { site, owner, name }; - }); -} diff --git a/src/lib/npm.json b/src/lib/data/npm.json similarity index 100% rename from src/lib/npm.json rename to src/lib/data/npm.json diff --git a/src/lib/stars.json b/src/lib/data/stars.json similarity index 100% rename from src/lib/stars.json rename to src/lib/data/stars.json diff --git a/src/lib/utils/injectNpmData.ts b/src/lib/utils/injectNpmData.ts index ba3ac7d..812fab5 100644 --- a/src/lib/utils/injectNpmData.ts +++ b/src/lib/utils/injectNpmData.ts @@ -1,4 +1,4 @@ -import npm from '$lib/npm.json'; +import npm from '$lib/data/npm.json'; import type { z } from 'zod'; import type { componentsSchema } from '$lib/schemas'; diff --git a/src/lib/utils/stars.ts b/src/lib/utils/stars.ts index 823e9a8..c18bf31 100644 --- a/src/lib/utils/stars.ts +++ b/src/lib/utils/stars.ts @@ -1,4 +1,4 @@ -import stars from '$lib/stars.json'; +import stars from '$lib/data/stars.json'; type RepoInfo = { type: 'Github' | 'Gitlab';