server.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { spawn, type ChildProcessWithoutNullStreams } from "child_process"
  2. import type { App } from "../app/app"
  3. import path from "path"
  4. import { Global } from "../global"
  5. import { Log } from "../util/log"
  6. export namespace LSPServer {
  7. const log = Log.create({ service: "lsp.server" })
  8. export interface Info {
  9. id: string
  10. extensions: string[]
  11. initialization?: Record<string, any>
  12. spawn(app: App.Info): Promise<ChildProcessWithoutNullStreams | undefined>
  13. }
  14. export const All: Info[] = [
  15. {
  16. id: "typescript",
  17. extensions: [
  18. ".ts",
  19. ".tsx",
  20. ".js",
  21. ".jsx",
  22. ".mjs",
  23. ".cjs",
  24. ".mts",
  25. ".cts",
  26. ],
  27. async spawn() {
  28. const root =
  29. process.argv0 !== "bun"
  30. ? path.resolve(process.cwd(), process.argv0)
  31. : process.argv0
  32. return spawn(root, ["x", "typescript-language-server", "--stdio"], {
  33. env: {
  34. BUN_BE_BUN: "1",
  35. },
  36. })
  37. },
  38. },
  39. {
  40. id: "golang",
  41. extensions: [".go"],
  42. async spawn() {
  43. let bin = Bun.which("gopls", {
  44. PATH: process.env["PATH"] + ":" + Global.Path.bin,
  45. })
  46. if (!bin) {
  47. log.info("installing gopls")
  48. const proc = Bun.spawn({
  49. cmd: ["go", "install", "golang.org/x/tools/gopls@latest"],
  50. env: { ...process.env, GOBIN: Global.Path.bin },
  51. })
  52. const exit = await proc.exited
  53. if (exit !== 0) {
  54. log.error("Failed to install gopls")
  55. return
  56. }
  57. bin = path.join(
  58. Global.Path.bin,
  59. "gopls" + (process.platform === "win32" ? ".exe" : ""),
  60. )
  61. log.info(`installed gopls`, {
  62. bin,
  63. })
  64. }
  65. return spawn(bin!)
  66. },
  67. },
  68. ]
  69. }