bin.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env node
  2. /**
  3. * dsh — command-line entry. Dynamic imports per mode keep unrelated modes out
  4. * of each dispatch path; the adapter prints and exits for
  5. * `--help`/`--version`/a parse error, so only a valid mode reaches the switch.
  6. * @module @deepseek-ai/dsh/bin
  7. */
  8. /* v8 ignore file -- built-bin acceptance exercises this self-executing dispatch. */
  9. import { readFileSync } from 'node:fs'
  10. import { fileURLToPath } from 'node:url'
  11. import { loadLayeredEnv } from '@deepseek-ai/dsh-app-boot'
  12. import { parseDshArgs } from './args.ts'
  13. // Both the source tree (apps/cli/src) and the bundled bin (apps/cli/lib) sit
  14. // one directory under apps/cli, so the checked-in manifest resolves with the
  15. // same relative hop from either artifact.
  16. /** This app's version, read from its checked-in package.json. */
  17. function readVersion(): string {
  18. const manifest = JSON.parse(
  19. readFileSync(fileURLToPath(new URL('../package.json', import.meta.url)), 'utf8'),
  20. ) as { version?: unknown }
  21. return typeof manifest.version === 'string' ? manifest.version : '0.0.0'
  22. }
  23. const invocation = parseDshArgs(process.argv.slice(2), readVersion())
  24. switch (invocation.mode) {
  25. case 'profile': {
  26. const { runProfile } = await import('./profile-boot.ts')
  27. await runProfile({
  28. environment: loadLayeredEnv('dsh'),
  29. profile: invocation.profile,
  30. patchFiles: invocation.patches,
  31. args: invocation.args,
  32. })
  33. break
  34. }
  35. case 'plugin': {
  36. const { runPlugin } = await import('./plugin.ts')
  37. process.exit(runPlugin(invocation.profile, invocation.args))
  38. break
  39. }
  40. case 'dump-config': {
  41. const { runDumpConfig } = await import('./dump-config.ts')
  42. runDumpConfig(invocation.profile, invocation.defaultOnly, invocation.patches)
  43. break
  44. }
  45. default:
  46. invocation satisfies never
  47. throw new Error(`dsh: unhandled invocation mode ${JSON.stringify(invocation)}`)
  48. }