bin.ts 2.0 KB

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