From 5430dd99a36779b67e2e193d8b1d71cbefc4325c Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 21 Oct 2024 10:29:03 -0700 Subject: [PATCH] create cron job to cache cycle data --- .github/workflows/cacheCycles.yml | 51 +++++++++++++++++++++++++++++++ .gitignore | 3 ++ cacheCycles.mjs | 13 ++++++++ getCycles.mjs | 14 +++++++++ getProducts.mjs | 3 ++ package.json | 6 ++-- 6 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/cacheCycles.yml create mode 100644 cacheCycles.mjs create mode 100644 getCycles.mjs create mode 100644 getProducts.mjs diff --git a/.github/workflows/cacheCycles.yml b/.github/workflows/cacheCycles.yml new file mode 100644 index 0000000..0474f99 --- /dev/null +++ b/.github/workflows/cacheCycles.yml @@ -0,0 +1,51 @@ +name: cache EOL data + +on: + workflow_dispatch: + schedule: + # https://crontab.guru/#55_11,23_*_*_* + - cron: '55 11,23 * * *' + +permissions: + contents: read + +jobs: + cache: + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: step-security/harden-runner@v1 + with: + egress-policy: block + allowed-endpoints: > + github.com:443 + nodejs.org:443 + prod.api.stepsecurity.io:443 + registry.npmjs.org:443 + endoflife.date:443 + + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - uses: ljharb/actions/node/install@main + + - run: node cacheCycles.mjs + + - run: git checkout data + + - run: git add -f out + + - run: git mv -f out/* ./ + + - name: GIT commit and push all changed files + env: + CI_COMMIT_MESSAGE: '[cron] update data' + CI_COMMIT_AUTHOR: Github Actions + CI_COMMIT_EMAIL: actions@users.noreply.github.com + run: | + git config --global user.name "${{ env.CI_COMMIT_AUTHOR }}" + git config --global user.email "${{ env.CI_COMMIT_EMAIL }}" + git commit -a -m "${{ env.CI_COMMIT_MESSAGE }}" + git push diff --git a/.gitignore b/.gitignore index c6bba59..65ca61b 100644 --- a/.gitignore +++ b/.gitignore @@ -128,3 +128,6 @@ dist .yarn/build-state.yml .yarn/install-state.gz .pnp.* + +# data output +out/ \ No newline at end of file diff --git a/cacheCycles.mjs b/cacheCycles.mjs new file mode 100644 index 0000000..49a9864 --- /dev/null +++ b/cacheCycles.mjs @@ -0,0 +1,13 @@ +import { mkdir, writeFile } from 'fs/promises'; +import { join } from 'path'; + +import getCycles from './getCycles.mjs'; + +const cycles = await getCycles(); + +const outDir = join(process.cwd(), 'out'); +const outFile = join(outDir, 'data.json'); + +await mkdir(outDir, { recursive: true }); + +await writeFile(outFile, JSON.stringify(cycles, null, '\t')); \ No newline at end of file diff --git a/getCycles.mjs b/getCycles.mjs new file mode 100644 index 0000000..dae3eb5 --- /dev/null +++ b/getCycles.mjs @@ -0,0 +1,14 @@ +import getProducts from "./getProducts.mjs"; + +export default async function getCycles() { + const products = await getProducts(); + + return Object.fromEntries( + await Promise.all( + products.map(async (product) => [ + product, + await fetch(`https://endoflife.date/api/${product}.json`).then(x => x.json()) + ]), + ), + ); +}; \ No newline at end of file diff --git a/getProducts.mjs b/getProducts.mjs new file mode 100644 index 0000000..69e61ac --- /dev/null +++ b/getProducts.mjs @@ -0,0 +1,3 @@ +export default function getProjects() { + return fetch('https://endoflife.date/api/all.json').then(x => x.json()); +}; diff --git a/package.json b/package.json index 0a8cde2..a0df020 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,6 @@ "name": "eol-data", "version": "0.0.0", "description": "Cache and process EOL data", - "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, @@ -16,5 +15,8 @@ "bugs": { "url": "https://github.com/herodevs/eol-data/issues" }, - "homepage": "https://github.com/herodevs/eol-data#readme" + "homepage": "https://github.com/herodevs/eol-data#readme", + "engines": { + "node": ">=22" + } }