Skip to content

Commit

Permalink
fix: validate using semver.coerce() (#3477)
Browse files Browse the repository at this point in the history
Close #3475
  • Loading branch information
zhaoterryy authored and develar committed Nov 15, 2018
1 parent 9422251 commit a46f79a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/app-builder-lib/src/electron/electronVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { httpExecutor } from "builder-util/out/nodeHttpExecutor"
import { readJson } from "fs-extra-p"
import { Lazy } from "lazy-val"
import * as path from "path"
import * as semver from "semver"
import { orNullIfFileNotExist } from "read-config-file"
import { Configuration } from "../configuration"
import { getConfig } from "../util/config"
import { versionFromDependencyRange } from "../util/packageMetadata"

export type MetadataValue = Lazy<{ [key: string]: any } | null>

Expand Down Expand Up @@ -60,7 +60,8 @@ export async function computeElectronVersion(projectDir: string, projectMetadata
throw new Error(`Cannot find electron dependency to get electron version in the '${path.join(projectDir, "package.json")}'`)
}

return versionFromDependencyRange(electronPrebuiltDep)
const version = semver.coerce(electronPrebuiltDep)
return version == null ? Promise.reject() : version.toString()
}

function findFromElectronPrebuilt(packageData: any): any {
Expand Down
17 changes: 15 additions & 2 deletions packages/app-builder-lib/src/util/packageMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,32 @@ export function versionFromDependencyRange(version: string) {
return firstChar === "^" || firstChar === "~" ? version.substring(1) : version
}

function versionSatisfies(version: string | semver.SemVer | null, range: string | semver.Range, loose?: boolean): boolean {
if (version == null) {
return false
}

const coerced = semver.coerce(version)
if (coerced == null) {
return false
}

return semver.satisfies(coerced, range, loose)
}

function checkDependencies(dependencies: { [key: string]: string } | null | undefined, errors: Array<string>) {
if (dependencies == null) {
return
}

const updaterVersion = dependencies["electron-updater"]
const requiredElectronUpdaterVersion = "4.0.0"
if (updaterVersion != null && !semver.satisfies(versionFromDependencyRange(updaterVersion), `>=${requiredElectronUpdaterVersion}`)) {
if (updaterVersion != null && !versionSatisfies(updaterVersion, `>=${requiredElectronUpdaterVersion}`)) {
errors.push(`At least electron-updater ${requiredElectronUpdaterVersion} is recommended by current electron-builder version. Please set electron-updater version to "^${requiredElectronUpdaterVersion}"`)
}

const swVersion = dependencies["electron-builder-squirrel-windows"]
if (swVersion != null && !semver.satisfies(versionFromDependencyRange(swVersion), ">=20.32.0")) {
if (swVersion != null && !versionSatisfies(swVersion, ">=20.32.0")) {
errors.push(`At least electron-builder-squirrel-windows 20.32.0 is required by current electron-builder version. Please set electron-builder-squirrel-windows to "^20.32.0"`)
}

Expand Down

0 comments on commit a46f79a

Please sign in to comment.