refactor(dev-infra): use Node gate argv parsing

This commit is contained in:
Tianyi Cui
2026-07-28 00:13:11 +08:00
parent 55d6ab5220
commit 6b19be2843
+13 -25
View File
@@ -9,6 +9,7 @@ import { spawn } from 'node:child_process'
import { availableParallelism } from 'node:os' import { availableParallelism } from 'node:os'
import { resolve } from 'node:path' import { resolve } from 'node:path'
import { performance } from 'node:perf_hooks' import { performance } from 'node:perf_hooks'
import { parseArgs } from 'node:util'
const MODE_SCRIPTS = { const MODE_SCRIPTS = {
'ci-primary': 'check:ci', 'ci-primary': 'check:ci',
@@ -96,14 +97,12 @@ interface RunRequest {
only?: string only?: string
} }
type ListedEnvironmentOverride = GateEnvironmentOverride
interface ListedGate { interface ListedGate {
id: string id: string
label: string label: string
command: string command: string
needs: string[] needs: string[]
env: Record<string, ListedEnvironmentOverride> env: Record<string, GateEnvironmentOverride>
blocking: boolean blocking: boolean
} }
@@ -161,28 +160,17 @@ async function main(args: string[]): Promise<number> {
*/ */
export function parseCliRequest(args: readonly string[]): RunRequest { export function parseCliRequest(args: readonly string[]): RunRequest {
const mode = parseMode(args[0]) const mode = parseMode(args[0])
let list = false const optionArgs = args[1] === '--' ? args.slice(2) : args.slice(1)
let json = false const { values: { list, json, only } } = parseArgs({
let only: string | undefined args: optionArgs,
const firstOption = args[1] === '--' ? 2 : 1 options: {
for (let index = firstOption; index < args.length; index += 1) { list: { type: 'boolean', default: false },
const arg = args[index] json: { type: 'boolean', default: false },
if (arg === '--list') { only: { type: 'string' },
if (list) throw new Error('run-gates: --list may be specified only once.') },
list = true strict: true,
} else if (arg === '--json') { allowPositionals: false,
if (json) throw new Error('run-gates: --json may be specified only once.') })
json = true
} else if (arg === '--only') {
if (only !== undefined) throw new Error('run-gates: --only may be specified only once.')
const id = args[index + 1]
if (id === undefined || id.startsWith('--')) throw new Error('run-gates: --only requires a gate id.')
only = id
index += 1
} else {
throw new Error(`run-gates: unsupported argument ${JSON.stringify(arg)}.`)
}
}
if (json && !list) throw new Error('run-gates: --json requires --list.') if (json && !list) throw new Error('run-gates: --json requires --list.')
if (list && only !== undefined) throw new Error('run-gates: --list and --only are mutually exclusive.') if (list && only !== undefined) throw new Error('run-gates: --list and --only are mutually exclusive.')
return { mode, list, json, ...only === undefined ? {} : { only } } return { mode, list, json, ...only === undefined ? {} : { only } }