diff --git a/packages/electron-builder/src/util/readPackageJson.ts b/packages/electron-builder/src/util/readPackageJson.ts index b4e4941fd46..72296ff1c5d 100644 --- a/packages/electron-builder/src/util/readPackageJson.ts +++ b/packages/electron-builder/src/util/readPackageJson.ts @@ -1,6 +1,8 @@ import Ajv from "ajv" +import { CancellationToken } from "electron-builder-http/out/CancellationToken" import { debug } from "electron-builder-util" import { log, warn } from "electron-builder-util/out/log" +import { httpExecutor } from "electron-builder-util/out/nodeHttpExecutor" import { readFile, readJson } from "fs-extra-p" import { safeLoad } from "js-yaml" import JSON5 from "json5" @@ -112,6 +114,20 @@ export async function getElectronVersion(config: Config | null | undefined, proj const packageJsonPath = path.join(projectDir, "package.json") const electronPrebuiltDep = findFromElectronPrebuilt(projectMetadata || await readJson(packageJsonPath)) if (electronPrebuiltDep == null) { + try { + const releaseInfo = await httpExecutor.request({ + hostname: "github.com", + path: "/electron/electron/releases/latest", + headers: { + Accept: "application/json", + }, + }, new CancellationToken()) + return (releaseInfo.tag_name.startsWith("v")) ? releaseInfo.tag_name.substring(1) : releaseInfo.tag_name + } + catch (e) { + warn(e) + } + throw new Error(`Cannot find electron dependency to get electron version in the '${packageJsonPath}'`) } diff --git a/test/out/__snapshots__/ExtraBuildTest.js.snap b/test/out/__snapshots__/ExtraBuildTest.js.snap index 2e99f6e6154..15122860db4 100644 --- a/test/out/__snapshots__/ExtraBuildTest.js.snap +++ b/test/out/__snapshots__/ExtraBuildTest.js.snap @@ -79,6 +79,12 @@ Object { } `; +exports[`retrieve latest electron version 1`] = ` +Object { + "linux": Array [], +} +`; + exports[`scheme validation 1`] = ` "Config is invalid: { diff --git a/test/src/ExtraBuildTest.ts b/test/src/ExtraBuildTest.ts index a5215b332d8..4eb0e84a1b1 100644 --- a/test/src/ExtraBuildTest.ts +++ b/test/src/ExtraBuildTest.ts @@ -4,7 +4,7 @@ import { move, readFile } from "fs-extra-p" import { safeLoad } from "js-yaml" import * as path from "path" import { assertThat } from "./helpers/fileAssert" -import { app, appThrows, assertPack } from "./helpers/packTester" +import { app, appThrows, assertPack, modifyPackageJson } from "./helpers/packTester" import { expectUpdateMetadata } from "./helpers/winHelper" function createBuildResourcesTest(platform: Platform) { @@ -56,6 +56,14 @@ test.ifAll.ifLinuxOrDevMac("prepackaged", app({ } })) +test.ifAll.ifLinuxOrDevMac("retrieve latest electron version", app({ + targets: linuxDirTarget, +}, { + projectDirCreated: projectDir => modifyPackageJson(projectDir, data => { + delete data.build.electronVersion + }), +})) + test.ifAll.ifDevOrLinuxCi("override targets in the config", app({ targets: linuxDirTarget, }, {