electron-builder.config.ts 4.5 KB

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