-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: restructuring
util/util.ts
(part 1) (#5431)
* chore: remove empty dangling comment * chore: deduplicate interface * chore: remove undocumented params from jsdocs * chore: move platform and arch utils to own module
- Loading branch information
1 parent
22f0015
commit c67f242
Showing
10 changed files
with
74 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (C) 2018-2023 Garden Technologies, Inc. <[email protected]> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import { memoize } from "lodash-es" | ||
import { execSync } from "child_process" | ||
|
||
const platformMap = { | ||
win32: "windows" as const, | ||
} | ||
const archMap = { | ||
x32: "386" as const, | ||
x64: "amd64" as const, | ||
} | ||
export type Architecture = Exclude<NodeJS.Architecture, keyof typeof archMap> | (typeof archMap)[keyof typeof archMap] | ||
export type Platform = | ||
| Exclude<NodeJS.Platform, keyof typeof platformMap> | ||
| (typeof platformMap)[keyof typeof platformMap] | ||
|
||
export function getPlatform(): Platform { | ||
return platformMap[process.platform] || process.platform | ||
} | ||
|
||
export function getArchitecture(): Architecture { | ||
// Note: When node is running a x64 build, | ||
// process.arch is always x64 even though the underlying CPU architecture may be arm64 | ||
// To check if we are running under Rosetta, | ||
// use the `isDarwinARM` function below | ||
const arch = process.arch | ||
return archMap[arch] || arch | ||
} | ||
|
||
export const isDarwinARM = memoize(() => { | ||
if (process.platform !== "darwin") { | ||
return false | ||
} | ||
|
||
if (process.arch === "arm64") { | ||
return true | ||
} else if (process.arch === "x64") { | ||
// detect rosetta on Apple M cpu family macs | ||
// see also https://developer.apple.com/documentation/apple-silicon/about-the-rosetta-translation-environment | ||
// We use execSync here, because this function is called in a constructor | ||
// otherwise we'd make the function async and call `spawn` | ||
try { | ||
execSync("sysctl -n -q sysctl.proc_translated", { encoding: "utf-8" }) | ||
} catch (err) { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
return false | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters