build-host-shells.mjs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env node
  2. import path from 'node:path'
  3. import { fileURLToPath } from 'node:url'
  4. import { writeHostShells, generateHostShells } from '../src/host-shells/generate.js'
  5. import { driftCheck } from '../src/host-shells/validator.js'
  6. // scripts/ → 上一级是 v7 包根
  7. const v7 = path.join(path.dirname(fileURLToPath(import.meta.url)), '..')
  8. const args = process.argv.slice(2)
  9. const has = (f) => args.includes(f)
  10. const getOpt = (k) => {
  11. const a = args.find((x) => x.startsWith(`${k}=`))
  12. return a ? a.slice(k.length + 1) : undefined
  13. }
  14. async function main() {
  15. if (has('--check')) {
  16. const r = await driftCheck(v7)
  17. if (!r.ok) {
  18. console.error(`drift check 失败:${r.error}`)
  19. process.exit(1)
  20. }
  21. console.log('drift check 通过:生成器确定 + validator 通过')
  22. return
  23. }
  24. const target = getOpt('--target') || 'all'
  25. const distDir = path.join(v7, 'dist')
  26. const all = await generateHostShells(v7)
  27. const hosts = target === 'all' ? Object.keys(all) : [target]
  28. for (const host of hosts) {
  29. if (!all[host]) {
  30. console.error(`未知宿主:${target}(可选:${Object.keys(all).join('、')})`)
  31. process.exit(1)
  32. }
  33. }
  34. await writeHostShells(v7, distDir)
  35. console.log(`已生成宿主壳到 dist/:${hosts.join('、')}`)
  36. }
  37. main().catch((e) => {
  38. console.error(e.message)
  39. process.exit(1)
  40. })