chore: avoid execSync when determining hostPlatform (#7386)

This commit is contained in:
Dmitry Gozman 2021-06-29 13:39:30 -07:00 committed by GitHub
parent 6b3614fd4c
commit 3ce1f5c33e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,7 +15,6 @@
* limitations under the License. * limitations under the License.
*/ */
import { execSync } from 'child_process';
import * as os from 'os'; import * as os from 'os';
import path from 'path'; import path from 'path';
import * as util from 'util'; import * as util from 'util';
@ -176,28 +175,25 @@ const DOWNLOAD_URLS = {
export const hostPlatform = ((): BrowserPlatform => { export const hostPlatform = ((): BrowserPlatform => {
const platform = os.platform(); const platform = os.platform();
if (platform === 'darwin') { if (platform === 'darwin') {
const [major, minor] = execSync('sw_vers -productVersion', { const ver = os.release().split('.').map((a: string) => parseInt(a, 10));
stdio: ['ignore', 'pipe', 'ignore'] let macVersion = '';
}).toString('utf8').trim().split('.').map(x => parseInt(x, 10)); if (ver[0] < 18) {
let arm64 = false; // Everything before 10.14 is considered 10.13.
// BigSur is the first version that might run on Apple Silicon. macVersion = 'mac10.13';
if (major >= 11) { } else if (ver[0] === 18) {
arm64 = execSync('/usr/sbin/sysctl -in hw.optional.arm64', { macVersion = 'mac10.14';
stdio: ['ignore', 'pipe', 'ignore'] } else if (ver[0] === 19) {
}).toString().trim() === '1'; macVersion = 'mac10.15';
} } else {
// ver[0] >= 20
const LAST_STABLE_MAC_MAJOR_VERSION = 11; const LAST_STABLE_MAC_MAJOR_VERSION = 11;
// All new MacOS releases increase major version.
let macVersion = `${major}`;
if (major === 10) {
// Pre-BigSur MacOS was increasing minor version every release.
macVersion = `${major}.${minor}`;
} else if (major > LAST_STABLE_MAC_MAJOR_VERSION) {
// Best-effort support for MacOS beta versions. // Best-effort support for MacOS beta versions.
macVersion = LAST_STABLE_MAC_MAJOR_VERSION + ''; macVersion = 'mac' + Math.min(ver[0] - 9, LAST_STABLE_MAC_MAJOR_VERSION);
// BigSur is the first version that might run on Apple Silicon.
if (os.cpus().some(cpu => cpu.model.includes('Apple')))
macVersion += '-arm64';
} }
const archSuffix = arm64 ? '-arm64' : ''; return macVersion as BrowserPlatform;
return `mac${macVersion}${archSuffix}` as BrowserPlatform;
} }
if (platform === 'linux') { if (platform === 'linux') {
const ubuntuVersion = getUbuntuVersionSync(); const ubuntuVersion = getUbuntuVersionSync();