Skip to content

Commit a826df2

Browse files
committed
feat: grab latest electron version from github if not specified
1 parent 9014d74 commit a826df2

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

packages/electron-builder/src/util/readPackageJson.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Ajv from "ajv"
2+
import { CancellationToken } from "electron-builder-http/out/CancellationToken"
23
import { debug } from "electron-builder-util"
34
import { log, warn } from "electron-builder-util/out/log"
5+
import { httpExecutor } from "electron-builder-util/out/nodeHttpExecutor"
46
import { readFile, readJson } from "fs-extra-p"
57
import { safeLoad } from "js-yaml"
68
import JSON5 from "json5"
@@ -112,6 +114,20 @@ export async function getElectronVersion(config: Config | null | undefined, proj
112114
const packageJsonPath = path.join(projectDir, "package.json")
113115
const electronPrebuiltDep = findFromElectronPrebuilt(projectMetadata || await readJson(packageJsonPath))
114116
if (electronPrebuiltDep == null) {
117+
try {
118+
const releaseInfo = await httpExecutor.request<any>({
119+
hostname: "github.com",
120+
path: "/electron/electron/releases/latest",
121+
headers: {
122+
Accept: "application/json",
123+
},
124+
}, new CancellationToken())
125+
return (releaseInfo.tag_name.startsWith("v")) ? releaseInfo.tag_name.substring(1) : releaseInfo.tag_name
126+
}
127+
catch (e) {
128+
warn(e)
129+
}
130+
115131
throw new Error(`Cannot find electron dependency to get electron version in the '${packageJsonPath}'`)
116132
}
117133

test/out/__snapshots__/ExtraBuildTest.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ Object {
7979
}
8080
`;
8181

82+
exports[`retrieve latest electron version 1`] = `
83+
Object {
84+
"linux": Array [],
85+
}
86+
`;
87+
8288
exports[`scheme validation 1`] = `
8389
"Config is invalid:
8490
{

test/src/ExtraBuildTest.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { move, readFile } from "fs-extra-p"
44
import { safeLoad } from "js-yaml"
55
import * as path from "path"
66
import { assertThat } from "./helpers/fileAssert"
7-
import { app, appThrows, assertPack } from "./helpers/packTester"
7+
import { app, appThrows, assertPack, modifyPackageJson } from "./helpers/packTester"
88
import { expectUpdateMetadata } from "./helpers/winHelper"
99

1010
function createBuildResourcesTest(platform: Platform) {
@@ -56,6 +56,14 @@ test.ifAll.ifLinuxOrDevMac("prepackaged", app({
5656
}
5757
}))
5858

59+
test.ifAll.ifLinuxOrDevMac("retrieve latest electron version", app({
60+
targets: linuxDirTarget,
61+
}, {
62+
projectDirCreated: projectDir => modifyPackageJson(projectDir, data => {
63+
delete data.build.electronVersion
64+
}),
65+
}))
66+
5967
test.ifAll.ifDevOrLinuxCi("override targets in the config", app({
6068
targets: linuxDirTarget,
6169
}, {

0 commit comments

Comments
 (0)