serve.ts 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. import { Server } from "../../server/server"
  2. import { cmd } from "./cmd"
  3. import { withNetworkOptions, resolveNetworkOptions } from "../network"
  4. import { Flag } from "../../flag/flag"
  5. import { Workspace } from "../../control-plane/workspace"
  6. import { Project } from "../../project/project"
  7. import { Installation } from "../../installation"
  8. export const ServeCommand = cmd({
  9. command: "serve",
  10. builder: (yargs) => withNetworkOptions(yargs),
  11. describe: "starts a headless opencode server",
  12. handler: async (args) => {
  13. if (!Flag.OPENCODE_SERVER_PASSWORD) {
  14. console.log("Warning: OPENCODE_SERVER_PASSWORD is not set; server is unsecured.")
  15. }
  16. const opts = await resolveNetworkOptions(args)
  17. const server = Server.listen(opts)
  18. console.log(`opencode server listening on http://${server.hostname}:${server.port}`)
  19. let workspaceSync: Array<ReturnType<typeof Workspace.startSyncing>> = []
  20. // Only available in development right now
  21. if (Installation.isLocal()) {
  22. workspaceSync = Project.list().map((project) => Workspace.startSyncing(project))
  23. }
  24. await new Promise(() => {})
  25. await server.stop()
  26. await Promise.all(workspaceSync.map((item) => item.stop()))
  27. },
  28. })