-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathlint.ts
More file actions
55 lines (45 loc) · 1.96 KB
/
lint.ts
File metadata and controls
55 lines (45 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* This script runs oxlint and oxfmt in a CI environment, without the need to install the entire
* project. It reads the required version from pnpm-lock.yaml and executes the linters accordingly.
* It's "stupid by design" so it could work in minimal Node.js environments.
*/
import { cpSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { spawnSync } from 'node:child_process'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
const projectDir = process.cwd()
function getDependencyVersion(dependencyName: string): string {
const result = spawnSync('npm', ['pkg', 'get', `devDependencies.${dependencyName}`], {
encoding: 'utf8',
})
if (result.status) {
throw new Error(`Command failed: pnpm info ${dependencyName} version`)
}
return JSON.parse(result.stdout)
}
function runCommand(command: string, args: string[], cwd?: string) {
const result = spawnSync(command, args, { stdio: 'inherit', cwd })
if (result.status) {
throw new Error(`Command failed: ${command} ${args.join(' ')}`)
}
}
const oxlintVersion = getDependencyVersion('oxlint')
const oxfmtVersion = getDependencyVersion('oxfmt')
const e18eVersion = getDependencyVersion('@e18e/eslint-plugin')
// Create a temp dir because:
// 1. oxlint seems to try to resolve plugins from the dir of the config file
// 2. pnpx/pnpm dlx doesn't have a clue about peers (plugins here), so doesn't have a way to install them
const tempDir = mkdtempSync(join(tmpdir(), 'oxlint-'))
try {
writeFileSync(join(tempDir, 'package.json'), '{"name": "temp", "version": "1.0.0"}')
cpSync('.oxlintrc.json', join(tempDir, '.oxlintrc.json'))
runCommand(
'pnpm',
['install', '-D', `oxlint@${oxlintVersion}`, `@e18e/eslint-plugin@${e18eVersion}`],
tempDir,
)
runCommand('pnpm', ['exec', 'oxlint', '-c', join(tempDir, '.oxlintrc.json'), projectDir], tempDir)
} finally {
rmSync(tempDir, { recursive: true, force: true })
}
runCommand('pnpx', [`oxfmt@${oxfmtVersion}`, '--check'])