|
| 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