|
| 1 | +import type { CommandModule } from 'yargs'; |
| 2 | +import * as core from '@actions/core'; |
| 3 | +import { |
| 4 | + PreflightService, |
| 5 | + listBuiltInChecks, |
| 6 | + builtInChecks, |
| 7 | +} from '../../model/orchestrator/services/preflight'; |
| 8 | + |
| 9 | +interface PreflightArguments { |
| 10 | + suite?: string; |
| 11 | + list?: boolean; |
| 12 | + check?: string; |
| 13 | +} |
| 14 | + |
| 15 | +const preflightCommand: CommandModule<object, PreflightArguments> = { |
| 16 | + command: 'preflight', |
| 17 | + describe: 'Run fast preflight validation checks before a build', |
| 18 | + builder: (yargs) => { |
| 19 | + return yargs |
| 20 | + .option('suite', { |
| 21 | + type: 'string', |
| 22 | + description: |
| 23 | + 'Path to a preflight suite YAML. Defaults to .game-ci/preflight-suite.yml when omitted.', |
| 24 | + }) |
| 25 | + .option('list', { |
| 26 | + type: 'boolean', |
| 27 | + description: 'List all built-in preflight checks and exit', |
| 28 | + default: false, |
| 29 | + }) |
| 30 | + .option('check', { |
| 31 | + type: 'string', |
| 32 | + description: 'Run a single check by ID (built-in or defined in the suite)', |
| 33 | + }) |
| 34 | + .example('game-ci preflight', 'Run the default preflight suite') |
| 35 | + .example('game-ci preflight --suite ./custom-suite.yml', 'Run a specific suite file') |
| 36 | + .example('game-ci preflight --list', 'List built-in preflight checks') |
| 37 | + .example('game-ci preflight --check runner-health', 'Run a single built-in check') as any; |
| 38 | + }, |
| 39 | + handler: async (cliArguments) => { |
| 40 | + try { |
| 41 | + if (cliArguments.list) { |
| 42 | + printBuiltInList(); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + if (cliArguments.check) { |
| 47 | + await runSingleCheck(cliArguments.check, cliArguments.suite); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + const suite = PreflightService.loadSuite(cliArguments.suite); |
| 52 | + core.info(`Running preflight suite: ${suite.name}`); |
| 53 | + |
| 54 | + const results = await PreflightService.executeSuite(suite); |
| 55 | + PreflightService.reportResults(results); |
| 56 | + |
| 57 | + if (!results.passed) { |
| 58 | + core.setFailed(`Preflight suite '${suite.name}' failed.`); |
| 59 | + process.exit(1); |
| 60 | + } |
| 61 | + } catch (error: any) { |
| 62 | + core.setFailed(`Preflight failed: ${error.message}`); |
| 63 | + throw error; |
| 64 | + } |
| 65 | + }, |
| 66 | +}; |
| 67 | + |
| 68 | +function printBuiltInList(): void { |
| 69 | + const checks = listBuiltInChecks(); |
| 70 | + core.info(`Built-in preflight checks (${checks.length}):\n`); |
| 71 | + core.info('| ID | Category | Name |'); |
| 72 | + core.info('|----|----------|------|'); |
| 73 | + for (const check of checks) { |
| 74 | + core.info(`| ${check.id} | ${check.category} | ${check.name} |`); |
| 75 | + } |
| 76 | + core.info('\nReference any of these IDs as a string entry in a suite file.'); |
| 77 | +} |
| 78 | + |
| 79 | +async function runSingleCheck(checkId: string, suitePath?: string): Promise<void> { |
| 80 | + // First, try the built-in registry. If not found, fall back to the suite |
| 81 | + // file (the user may be running a custom check defined inline there). |
| 82 | + let check = builtInChecks.get(checkId); |
| 83 | + |
| 84 | + if (!check && suitePath) { |
| 85 | + const suite = PreflightService.loadSuite(suitePath); |
| 86 | + const resolved = PreflightService.resolveChecks(suite); |
| 87 | + check = resolved.find((c) => c.id === checkId); |
| 88 | + } |
| 89 | + |
| 90 | + if (!check) { |
| 91 | + throw new Error( |
| 92 | + `Check '${checkId}' not found. Use 'game-ci preflight --list' to see built-in checks, ` + |
| 93 | + `or pass --suite <path> if the check is defined in a custom suite.`, |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + const result = await PreflightService.executeCheck(check); |
| 98 | + PreflightService.reportResults({ |
| 99 | + passed: result.passed, |
| 100 | + results: [result], |
| 101 | + duration: result.duration, |
| 102 | + failedAt: result.passed ? undefined : 0, |
| 103 | + }); |
| 104 | + |
| 105 | + if (!result.passed) { |
| 106 | + core.setFailed(`Check '${checkId}' failed.`); |
| 107 | + process.exit(1); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +export default preflightCommand; |
0 commit comments