bin.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env node
  2. /**
  3. * Command-line entry for dsh.
  4. * @module @deepseek-ai/dsh/bin
  5. */
  6. /* v8 ignore file -- built-bin acceptance exercises this self-executing dispatch. */
  7. import { readFileSync } from 'node:fs'
  8. import { fileURLToPath } from 'node:url'
  9. import { loadLayeredEnv } from '@deepseek-ai/dsh-app-boot'
  10. import { parseDshArgs } from './args.ts'
  11. // Both the source tree (apps/cli/src) and the bundled bin (apps/cli/lib) sit
  12. // one directory under apps/cli, so the checked-in manifest resolves with the
  13. // same relative hop from either artifact.
  14. function readVersion(): string {
  15. const manifest = JSON.parse(
  16. readFileSync(fileURLToPath(new URL('../package.json', import.meta.url)), 'utf8'),
  17. ) as { version?: unknown }
  18. return typeof manifest.version === 'string' ? manifest.version : '0.0.0'
  19. }
  20. /**
  21. * Run the public dsh command-line interface.
  22. * @returns a promise that settles when the selected command mode finishes.
  23. */
  24. export async function runCli(): Promise<void> {
  25. const invocation = parseDshArgs(process.argv.slice(2), readVersion())
  26. switch (invocation.mode) {
  27. case 'profile': {
  28. const { runProfile } = await import('./profile-boot.ts')
  29. await runProfile({
  30. environment: loadLayeredEnv('dsh'),
  31. profile: invocation.profile,
  32. patchFiles: invocation.patches,
  33. args: invocation.args,
  34. })
  35. break
  36. }
  37. case 'plugin': {
  38. const { runPlugin } = await import('./plugin.ts')
  39. process.exit(runPlugin(invocation.profile, invocation.args))
  40. break
  41. }
  42. case 'dump-config': {
  43. const { runDumpConfig } = await import('./dump-config.ts')
  44. runDumpConfig(invocation.profile, invocation.defaultOnly, invocation.patches)
  45. break
  46. }
  47. default:
  48. invocation satisfies never
  49. throw new Error(`dsh: unhandled invocation mode ${JSON.stringify(invocation)}`)
  50. }
  51. }
  52. if (import.meta.main) {
  53. await runCli()
  54. }