policy.mjs 1.1 KB

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/env node
  2. import fs from 'node:fs'
  3. import process from 'node:process'
  4. import { pathToFileURL } from 'node:url'
  5. /** Command-line dispatch for PR policy checks and Issue lifecycle events. */
  6. import { runLifecycle } from './lifecycle.mjs'
  7. import { runPullRequestCheck, runPullRequestPreflight } from './pull-request.mjs'
  8. function readEvent() {
  9. if (!process.env.GITHUB_EVENT_PATH) throw new Error('GITHUB_EVENT_PATH 未设置')
  10. return JSON.parse(fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8'))
  11. }
  12. async function main(argv) {
  13. const [command] = argv
  14. if (command === 'pr-preflight') await runPullRequestPreflight(readEvent())
  15. else if (command === 'pr') await runPullRequestCheck(readEvent())
  16. else if (command === 'lifecycle') await runLifecycle(process.env.GITHUB_EVENT_NAME, readEvent())
  17. else throw new Error('用法:policy.mjs pr-preflight|pr|lifecycle')
  18. }
  19. if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
  20. main(process.argv.slice(2)).catch((error) => {
  21. process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`)
  22. process.exitCode = 1
  23. })
  24. }