bin.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. })
  32. break
  33. }
  34. case 'run': {
  35. const { runProfile } = await import('./profile-boot.ts')
  36. await runProfile({
  37. environment: loadLayeredEnv('dsh'),
  38. profile: invocation.profile,
  39. patchFiles: invocation.patches,
  40. task: invocation.task,
  41. })
  42. break
  43. }
  44. case 'web': {
  45. const { runWeb } = await import('./web.ts')
  46. await runWeb(invocation, loadLayeredEnv('dsh'))
  47. break
  48. }
  49. case 'plugin': {
  50. const { runPlugin } = await import('./plugin.ts')
  51. process.exit(runPlugin(invocation.profile, invocation.args))
  52. break
  53. }
  54. case 'dump-config': {
  55. const { runDumpConfig } = await import('./dump-config.ts')
  56. runDumpConfig(invocation.profile, invocation.defaultOnly, invocation.patches)
  57. break
  58. }
  59. default:
  60. invocation satisfies never
  61. throw new Error(`dsh: unhandled invocation mode ${JSON.stringify(invocation)}`)
  62. }