electron-builder.config.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { existsSync } from "node:fs"
  2. import { execFile } from "node:child_process"
  3. import path from "node:path"
  4. import { fileURLToPath } from "node:url"
  5. import { promisify } from "node:util"
  6. import type { Configuration } from "electron-builder"
  7. const execFileAsync = promisify(execFile)
  8. const packageDir = path.dirname(fileURLToPath(import.meta.url))
  9. const rootDir = path.resolve(packageDir, "../..")
  10. const signScript = path.join(rootDir, "script", "sign-windows.ps1")
  11. const metainfoFpm = (appId: string) =>
  12. `${path.join(packageDir, "resources", `${appId}.metainfo.xml`)}=/usr/share/metainfo/${appId}.metainfo.xml`
  13. async function signWindows(configuration: { path: string }) {
  14. if (process.platform !== "win32") return
  15. if (process.env.GITHUB_ACTIONS !== "true") return
  16. await execFileAsync(
  17. "pwsh",
  18. ["-NoLogo", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", signScript, configuration.path],
  19. { cwd: rootDir },
  20. )
  21. }
  22. const channel = (() => {
  23. const raw = process.env.OPENCODE_CHANNEL
  24. if (raw === "dev" || raw === "beta" || raw === "prod") return raw
  25. return "dev"
  26. })()
  27. const APP_IDS = {
  28. dev: "org.lambda-scientist.desktop.dev",
  29. beta: "org.lambda-scientist.desktop.beta",
  30. prod: "org.lambda-scientist.desktop",
  31. } as const
  32. const getBase = (appId: string): Configuration => ({
  33. artifactName: "lambda-agent-${version}-${os}-${arch}.${ext}",
  34. directories: {
  35. output: "dist",
  36. buildResources: "resources",
  37. },
  38. // Linux launchers are .desktop files, so this is the desktop file name,
  39. // not just the app id. For prod, app id "org.lambda-scientist.desktop" becomes
  40. // "org.lambda-scientist.desktop.desktop".
  41. // https://developer.gnome.org/documentation/guidelines/maintainer/integrating.html
  42. // https://www.electron.build/docs/linux/
  43. extraMetadata: {
  44. desktopName: `${appId}.desktop`,
  45. },
  46. files: ["out/**/*", "resources/**/*", "!resources/opencode-cli*", "!resources/agentpaas-runtime/**/*"],
  47. extraResources: [
  48. ...(process.env.LAMBDA_AGENT_EXTERNAL_SERVICES !== "1" &&
  49. existsSync(path.join(packageDir, "resources/agentpaas-runtime/desktop-runtime.json"))
  50. ? [{ from: "resources/agentpaas-runtime", to: "agentpaas-runtime" }]
  51. : []),
  52. ...(channel === "dev"
  53. ? [
  54. {
  55. from: "resources/",
  56. to: "",
  57. filter: ["opencode-cli*"],
  58. },
  59. ]
  60. : []),
  61. ],
  62. mac: {
  63. category: "public.app-category.developer-tools",
  64. icon: `resources/icons/icon.icns`,
  65. hardenedRuntime: true,
  66. gatekeeperAssess: false,
  67. entitlements: "resources/entitlements.plist",
  68. entitlementsInherit: "resources/entitlements.plist",
  69. notarize: true,
  70. target: ["dmg", "zip"],
  71. },
  72. dmg: {
  73. sign: true,
  74. },
  75. protocols: {
  76. name: "λ-Agent",
  77. schemes: ["lambda-scientist"],
  78. },
  79. win: {
  80. ...(channel === "beta" && process.env.LAMBDA_AGENT_WINDOWS_RUNTIME
  81. ? {
  82. extraResources: ["git", "ripgrep"].map((tool) => ({
  83. from: path.join(process.env.LAMBDA_AGENT_WINDOWS_RUNTIME!, tool),
  84. to: `windows-runtime/${tool}`,
  85. })),
  86. }
  87. : {}),
  88. icon: `resources/icons/icon.ico`,
  89. signtoolOptions: {
  90. sign: signWindows,
  91. },
  92. target: ["nsis"],
  93. verifyUpdateCodeSignature: true,
  94. },
  95. nsis: {
  96. oneClick: true,
  97. perMachine: false,
  98. installerIcon: `resources/icons/icon.ico`,
  99. installerHeaderIcon: `resources/icons/icon.ico`,
  100. },
  101. linux: {
  102. icon: `resources/icons`,
  103. category: "Development",
  104. executableName: appId,
  105. desktop: {
  106. entry: {
  107. // Match the installed .desktop file and hicolor icon basename so
  108. // Linux shells can associate the running Electron window with its launcher.
  109. StartupWMClass: appId,
  110. },
  111. },
  112. target: ["AppImage", "deb", "rpm"],
  113. },
  114. })
  115. function updateSource(): Configuration["publish"] {
  116. const url = process.env.LAMBDA_SCIENTIST_UPDATE_URL
  117. if (!url) return null
  118. if (new URL(url).protocol !== "https:") throw new Error("Update URL must use HTTPS")
  119. return { provider: "generic", url, channel: channel === "beta" ? "beta" : "latest" }
  120. }
  121. function getConfig() {
  122. const appId = APP_IDS[channel]
  123. const base = getBase(appId)
  124. switch (channel) {
  125. case "dev": {
  126. return {
  127. ...base,
  128. appId,
  129. productName: "λ-Agent Dev",
  130. deb: { fpm: [metainfoFpm(appId)] },
  131. rpm: { packageName: "lambda-scientist-dev", fpm: [metainfoFpm(appId)] },
  132. }
  133. }
  134. case "beta": {
  135. return {
  136. ...base,
  137. appId,
  138. productName: "λ-Agent Beta",
  139. protocols: { name: "λ-Agent Beta", schemes: ["lambda-scientist"] },
  140. publish: updateSource(),
  141. deb: { fpm: [metainfoFpm(appId)] },
  142. rpm: { packageName: "lambda-scientist-beta", fpm: [metainfoFpm(appId)] },
  143. }
  144. }
  145. case "prod": {
  146. return {
  147. ...base,
  148. appId,
  149. productName: "λ-Agent",
  150. protocols: { name: "λ-Agent", schemes: ["lambda-scientist"] },
  151. publish: updateSource(),
  152. deb: { fpm: [metainfoFpm(appId)] },
  153. rpm: { packageName: "lambda-scientist", fpm: [metainfoFpm(appId)] },
  154. }
  155. }
  156. }
  157. }
  158. export default getConfig()