Skip to content

Commit

Permalink
chore: update nodejs script
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Nov 15, 2024
1 parent c6d360b commit 42fce04
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/sync-modules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ env:

jobs:
sync-modules:
if: ${{ github.repository_owner == 'bitfocus' }}

runs-on: ubuntu-latest
timeout-minutes: 5

Expand Down
56 changes: 56 additions & 0 deletions .github/workflows/update-nodejs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Check for NodeJS updates
on:
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# Run every week
schedule:
- cron: '43 5 * * 0'

env:
COMPANION_BRANCH: master

permissions:
contents: write
pull-requests: write

jobs:
update-nodejs:
if: ${{ github.repository_owner == 'bitfocus' }}

runs-on: ubuntu-latest
timeout-minutes: 5

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: true
ref: ${{ env.COMPANION_BRANCH }}
token: ${{ secrets.SYNC_MODULES_PAT }}

- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version-file: '.node-version'

- name: Setup environment
run: |
corepack enable
yarn
- name: Run script
run: |
node tools/update_nodejs_versions.mjs
- name: Create Pull Request
uses: peter-evans/create-pull-request@v7
with:
commit-message: 'chore: update Node.js versions'
title: 'chore: update Node.js versions'
body: 'This PR updates the Node.js versions in the Companion.'
delete-branch: true
branch: 'chore/update-nodejs'
committer: 'Companion Bot <[email protected]>'
author: 'Companion Bot <[email protected]>'
add-paths: 'nodejs-versions.json'
token: ${{ secrets.SYNC_MODULES_PAT }}
32 changes: 32 additions & 0 deletions tools/update_nodejs_versions.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import fs from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { SemVer } from 'semver'

const nodejsVersionsPath = fileURLToPath(new URL('../nodejs-versions.json', import.meta.url))

const existingVersionsStr = await fs.readFile(nodejsVersionsPath, 'utf8')
const existingVersions = JSON.parse(existingVersionsStr)

console.log('existing versions:', existingVersions)

const apiReleases = await fetch('https://nodejs.org/download/release/index.json').then((res) => res.json())
console.log(`Found ${apiReleases.length} nodejs releases!`)

const newVersions = { ...existingVersions }

for (const [versionName, currentVersion] of Object.entries(existingVersions)) {
if (!versionName.startsWith('node')) continue

let latestVersion = new SemVer(currentVersion)
for (const apiRelease of apiReleases) {
const apiSemver = new SemVer(apiRelease.version)
if (apiSemver.major === latestVersion.major && apiSemver.compare(latestVersion) > 0) {
latestVersion = apiSemver
}
}

console.log(`Latest version for ${versionName}: ${latestVersion}`)
newVersions[versionName] = latestVersion.version
}

await fs.writeFile(nodejsVersionsPath, JSON.stringify(newVersions, null, '\t') + '\n')

0 comments on commit 42fce04

Please sign in to comment.