Skip to content

Commit 02a99cd

Browse files
committed
Move lint to a custom script to avoid hardcoding dependency versions
1 parent 9e9e5e6 commit 02a99cd

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ jobs:
3030
with:
3131
node-version: lts/*
3232

33+
- uses: pnpm/action-setup@1e1c8eafbd745f64b1ef30a7d7ed7965034c486c
34+
name: Install pnpm
35+
3336
- name: 🔠 Lint project
34-
run: node --run lint:ci
37+
run: node scripts/lint.ts
3538

3639
test:
3740
runs-on: ubuntu-latest

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"knip": "knip",
2020
"knip:fix": "knip --fix",
2121
"lint": "vite lint && vite fmt --check",
22-
"lint:ci": "npx oxlint@1.42.0 & npx oxfmt@0.27.0 --check",
22+
"lint:ci": "node scripts/lint.ts",
2323
"lint:fix": "vite lint --fix && vite fmt",
2424
"generate": "nuxt generate",
2525
"npmx-connector": "pnpm --filter npmx-connector dev",

scripts/lint.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* This script runs oxlint and oxfmt in a CI environment, without the need to install the entire
3+
* project. It reads the required versions from vite-plus entry in pnpm-lock.yaml and executes
4+
* the linters accordingly. It's "stupid by design" so it could work in minimal Node.js environments.
5+
*/
6+
7+
import { spawnSync } from 'node:child_process'
8+
import { readFileSync } from 'node:fs'
9+
10+
const lockfileContent = readFileSync('pnpm-lock.yaml', 'utf8')
11+
const lines = lockfileContent.split(/\n/)
12+
13+
const startIndex = lines.findIndex(line => line.includes('vite-plus@'))
14+
15+
if (startIndex === -1) {
16+
throw new Error('Could not find "vite-plus@" in pnpm-lock.yaml.')
17+
}
18+
19+
function getDependencyVersion(dependencyName: string): string {
20+
const versionRegex = new RegExp(`^\\s+${dependencyName}: ([^(\\s]+)`)
21+
22+
for (let index = startIndex + 1; index < lines.length; index += 1) {
23+
const match = lines[index].match(versionRegex)
24+
25+
if (match) {
26+
return match[1]
27+
}
28+
}
29+
30+
throw new Error(`Could not resolve ${dependencyName} version from pnpm-lock.yaml.`)
31+
}
32+
33+
const oxlintVersion = getDependencyVersion('oxlint')
34+
const oxfmtVersion = getDependencyVersion('oxfmt')
35+
36+
const runCommand = (command: string, args: string[]) => {
37+
const result = spawnSync(command, args, { stdio: 'inherit' })
38+
39+
if (result.status) {
40+
process.exit(result.status)
41+
}
42+
}
43+
44+
runCommand('pnpx', [`oxlint@${oxlintVersion}`])
45+
runCommand('pnpx', [`oxfmt@${oxfmtVersion}`, '--check'])

0 commit comments

Comments
 (0)