Skip to content

Commit

Permalink
fix: avoid process hanging when trying to get Chrome version (#5315)
Browse files Browse the repository at this point in the history
* fix: add a timeout, avoid process hanging

fixes #5310

* fix: avoid accidentally trigerring the `installedBrowsers` getter

fixes #5286
  • Loading branch information
sodatea committed Mar 31, 2020
1 parent 4225c30 commit a1041a8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
7 changes: 7 additions & 0 deletions packages/@vue/cli-shared-utils/index.js
Expand Up @@ -19,3 +19,10 @@
exports.chalk = require('chalk')
exports.execa = require('execa')
exports.semver = require('semver')

Object.defineProperty(exports, 'installedBrowsers', {
enumerable: true,
get () {
return exports.getInstalledBrowsers()
}
})
76 changes: 37 additions & 39 deletions packages/@vue/cli-shared-utils/lib/env.js
Expand Up @@ -156,7 +156,8 @@ let hasCheckedBrowsers = false
function tryRun (cmd) {
try {
return execSync(cmd, {
stdio: [0, 'pipe', 'ignore']
stdio: [0, 'pipe', 'ignore'],
timeout: 10000
}).toString().trim()
} catch (e) {
return ''
Expand All @@ -177,44 +178,41 @@ function getMacAppVersion (bundleIdentifier) {
}
}

Object.defineProperty(exports, 'installedBrowsers', {
enumerable: true,
get () {
if (hasCheckedBrowsers) {
return browsers
}
hasCheckedBrowsers = true

if (exports.isLinux) {
browsers.chrome = getLinuxAppVersion('google-chrome')
browsers.firefox = getLinuxAppVersion('firefox')
} else if (exports.isMacintosh) {
browsers.chrome = getMacAppVersion('com.google.Chrome')
browsers.firefox = getMacAppVersion('org.mozilla.firefox')
} else if (exports.isWindows) {
// get chrome stable version
// https://stackoverflow.com/a/51773107/2302258
const chromeQueryResult = tryRun(
'reg query "HKLM\\Software\\Google\\Update\\Clients\\{8A69D345-D564-463c-AFF1-A69D9E530F96}" /v pv /reg:32'
) || tryRun(
'reg query "HKCU\\Software\\Google\\Update\\Clients\\{8A69D345-D564-463c-AFF1-A69D9E530F96}" /v pv /reg:32'
)
if (chromeQueryResult) {
const matched = chromeQueryResult.match(/REG_SZ\s+(\S*)$/)
browsers.chrome = matched && matched[1]
}

// get firefox version
// https://community.spiceworks.com/topic/111518-how-to-determine-version-of-installed-firefox-in-windows-batchscript
const ffQueryResult = tryRun(
'reg query "HKLM\\Software\\Mozilla\\Mozilla Firefox" /v CurrentVersion'
)
if (ffQueryResult) {
const matched = ffQueryResult.match(/REG_SZ\s+(\S*)$/)
browsers.firefox = matched && matched[1]
}
exports.getInstalledBrowsers = () => {
if (hasCheckedBrowsers) {
return browsers
}
hasCheckedBrowsers = true

if (exports.isLinux) {
browsers.chrome = getLinuxAppVersion('google-chrome')
browsers.firefox = getLinuxAppVersion('firefox')
} else if (exports.isMacintosh) {
browsers.chrome = getMacAppVersion('com.google.Chrome')
browsers.firefox = getMacAppVersion('org.mozilla.firefox')
} else if (exports.isWindows) {
// get chrome stable version
// https://stackoverflow.com/a/51773107/2302258
const chromeQueryResult = tryRun(
'reg query "HKLM\\Software\\Google\\Update\\Clients\\{8A69D345-D564-463c-AFF1-A69D9E530F96}" /v pv /reg:32'
) || tryRun(
'reg query "HKCU\\Software\\Google\\Update\\Clients\\{8A69D345-D564-463c-AFF1-A69D9E530F96}" /v pv /reg:32'
)
if (chromeQueryResult) {
const matched = chromeQueryResult.match(/REG_SZ\s+(\S*)$/)
browsers.chrome = matched && matched[1]
}

return browsers
// get firefox version
// https://community.spiceworks.com/topic/111518-how-to-determine-version-of-installed-firefox-in-windows-batchscript
const ffQueryResult = tryRun(
'reg query "HKLM\\Software\\Mozilla\\Mozilla Firefox" /v CurrentVersion'
)
if (ffQueryResult) {
const matched = ffQueryResult.match(/REG_SZ\s+(\S*)$/)
browsers.firefox = matched && matched[1]
}
}
})

return browsers
}

0 comments on commit a1041a8

Please sign in to comment.