bin.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 and PTY tests exercise this self-executing dispatch. */
  9. import { readFileSync } from 'node:fs'
  10. import { fileURLToPath } from 'node:url'
  11. import { loadEnv } 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. loadEnv('dsh')
  24. // The env opt-in is read at the process boundary; `1` is the documented value.
  25. const invocation = parseDshArgs(process.argv.slice(2), readVersion(), process.env.DSH_EXPERIMENTAL === '1')
  26. switch (invocation.mode) {
  27. case 'web': {
  28. const { runWeb } = await import('./web.ts')
  29. await runWeb(invocation.host, invocation.port, invocation.dev, invocation.workspaceRoot, invocation.trustedHosts, invocation.config)
  30. break
  31. }
  32. case 'headless': {
  33. const { runHeadless } = await import('./headless.ts')
  34. await runHeadless(invocation.prompt)
  35. break
  36. }
  37. case 'tui': {
  38. const { runTui } = await import('./tui.ts')
  39. await runTui(invocation.config, invocation.resume, undefined, undefined, invocation.configReplace)
  40. break
  41. }
  42. case 'dump-config': {
  43. const { runDumpConfig } = await import('./dump-config.ts')
  44. runDumpConfig(invocation.surface, invocation.defaultOnly, invocation.config)
  45. break
  46. }
  47. case 'meta': {
  48. const { runTui, SOURCE_ROOT } = await import('./tui.ts')
  49. await runTui(undefined, undefined, SOURCE_ROOT)
  50. break
  51. }
  52. case 'upgrade': {
  53. const { runTui } = await import('./tui.ts')
  54. await runTui(undefined, undefined, undefined, `dsh-${invocation.mode}`)
  55. break
  56. }
  57. default:
  58. invocation satisfies never
  59. throw new Error(`dsh: unhandled invocation mode ${JSON.stringify(invocation)}`)
  60. }