Skip to content

Commit

Permalink
ci: add workflow for testing required Node.js semver range
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed May 30, 2024
1 parent f7e4106 commit 6c226c5
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .changeset/light-eagles-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
29 changes: 29 additions & 0 deletions .github/workflows/node-range.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Test & Release

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
test-node-range:
name: Test Node.js version range
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- uses: actions/cache@v4
id: cache-node_modules
with:
path: node_modules
key: ubuntu-latest-20-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
ubuntu-latest-20-${{ hashFiles('**/package-lock.json') }}
- if: steps.cache-node_modules.outputs.cache-hit != 'true'
run: npm ci
- run: node ./scripts/test-node-range.js
24 changes: 0 additions & 24 deletions scripts/list-dependency-node-versions.js

This file was deleted.

40 changes: 40 additions & 0 deletions scripts/test-node-range.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { readFile } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'

import chalk from 'chalk'
import { execa } from 'execa'
import { subset } from 'semver'

const packageJson = JSON.parse(
await readFile(fileURLToPath(new URL('../package.json', import.meta.url)))
)

const lintStagedRequires = packageJson.engines.node

console.log(`Currently required Node.js version: ${lintStagedRequires}`)

console.log(chalk.dim('-----------------------------'))
console.log(chalk.dim('Testing current dependencies:'))
console.log(chalk.dim('-----------------------------'))

let allDependenciesSupported = true

for (const dependency of Object.keys(packageJson.dependencies)) {
const { stdout: dependencyRequires } = await execa('npm', ['info', dependency, 'engines.node'])

/** True if currently-required range is inside the dependency's required range */
const isSubset = subset(lintStagedRequires, dependencyRequires)

if (!isSubset) {
allDependenciesSupported = false
}

const color = isSubset ? chalk.greenBright : chalk.redBright
const symbol = isSubset ? `✓` : '×'

console.log(`${color(`${symbol} ${dependency}`)}:`, dependencyRequires)
}

if (!allDependenciesSupported) {
process.exit(1)
}

0 comments on commit 6c226c5

Please sign in to comment.