-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: restructuring util/util.ts
(part 1)
#5431
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a refactoring so definitely not a blocker, but I guess we could replace that with
Record<string, T>
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Maybe, this type was introduced as a short-handed type for
string
-keyedRecord
. In that case, we can just re-declare it astype Dictionary<T> = Record<string, T>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be addressed in the next PR, there will be a series of PRs to refactor
util/util.ts
:)