command.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Internal dsh-sdk command composition used by the package bin.
  3. *
  4. * @module @deepseek-ai/dsh-scripts/command
  5. */
  6. import { parseDshSdkArgs } from './args.ts'
  7. import { runProjectBuild } from './build.ts'
  8. import { runConfigCommand, type ConfigCommandContext } from './config.ts'
  9. import { runSDK } from './runtime.ts'
  10. import { DSH_SDK_TEMPLATES } from './templates/dsh-sdk-templates.ts'
  11. /** Injectable process and command boundaries used by the dsh-sdk bin. */
  12. export interface DshSdkCommandContext extends ConfigCommandContext {
  13. cwd: string
  14. stdin: NodeJS.ReadStream
  15. stdout: NodeJS.WriteStream
  16. stderr: NodeJS.WriteStream
  17. run?: typeof runSDK
  18. build?: typeof runProjectBuild
  19. config?: typeof runConfigCommand
  20. }
  21. /** Run one parsed dsh-sdk command and return its process exit code. */
  22. export async function runDshSdkCommand(
  23. argv: readonly string[] = process.argv.slice(2),
  24. context: DshSdkCommandContext = {
  25. cwd: process.cwd(),
  26. stdin: process.stdin,
  27. stdout: process.stdout,
  28. stderr: process.stderr,
  29. },
  30. ): Promise<number> {
  31. try {
  32. const args = parseDshSdkArgs(argv)
  33. if (args.help || !args.command) {
  34. context.stdout.write(DSH_SDK_TEMPLATES.usage.render({}))
  35. return 0
  36. }
  37. const run = context.run ?? runSDK
  38. const build = context.build ?? runProjectBuild
  39. const config = context.config ?? runConfigCommand
  40. switch (args.command) {
  41. case 'start': await run(args.target, { cwd: context.cwd, argv: args.forwarded }); break
  42. case 'dev': await run(args.target, { cwd: context.cwd, dev: true, argv: args.forwarded }); break
  43. case 'build': await build(args.forwarded, context.cwd); break
  44. case 'config': {
  45. const result = await config(context)
  46. if (result.installError) return 1
  47. break
  48. }
  49. }
  50. return 0
  51. } catch (error) {
  52. context.stderr.write(`dsh-sdk: ${error instanceof Error ? error.message : String(error)}\n`)
  53. return 1
  54. }
  55. }