generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract platform-specific details into platform.ts
- Loading branch information
1 parent
5239d5b
commit 1b1cf9d
Showing
3 changed files
with
109 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import os from 'node:os' | ||
|
||
enum Platform { | ||
Linux, | ||
MacOS, | ||
Windows | ||
} | ||
|
||
enum Architecture { | ||
arm64, | ||
x64 | ||
} | ||
|
||
type PlatformInfo = { | ||
plat: Platform | ||
arch: Architecture | ||
githubSourceAssetName: string | ||
targetFileName: string | ||
} | ||
|
||
export function determinePlatformInfo(): PlatformInfo { | ||
const plat = determineOS() | ||
const arch = determineArch() | ||
|
||
return { | ||
plat, | ||
arch, | ||
githubSourceAssetName: determineGithubAsset(plat, arch), | ||
targetFileName: determineTargetFileName(plat) | ||
} | ||
} | ||
|
||
function determineOS(): Platform { | ||
switch (os.platform()) { | ||
case 'linux': | ||
return Platform.Linux | ||
case 'darwin': | ||
return Platform.MacOS | ||
case 'win32': | ||
return Platform.Windows | ||
default: | ||
throw new Error('Unsupported platform') | ||
} | ||
} | ||
|
||
function determineArch(): Architecture { | ||
switch (os.arch()) { | ||
case 'arm64': | ||
return Architecture.arm64 | ||
case 'x64': | ||
return Architecture.x64 | ||
default: | ||
throw new Error('Unsupported architecture') | ||
} | ||
} | ||
|
||
function determineGithubAsset(plat: Platform, arch: Architecture): string { | ||
switch (plat) { | ||
case Platform.Linux: | ||
switch (arch) { | ||
case Architecture.arm64: | ||
return 'pkl-linux-aarch64' | ||
case Architecture.x64: | ||
return 'pkl-linux-amd64' | ||
default: | ||
throw new Error('Unsupported architecture') | ||
} | ||
case Platform.MacOS: | ||
switch (arch) { | ||
case Architecture.arm64: | ||
return 'pkl-macos-aarch64' | ||
case Architecture.x64: | ||
return 'pkl-macos-amd64' | ||
default: | ||
throw new Error('Unsupported architecture') | ||
} | ||
case Platform.Windows: | ||
switch (arch) { | ||
case Architecture.arm64: | ||
throw new Error('Windows arm not yet supported') | ||
case Architecture.x64: | ||
return 'pkl-windows-amd64.exe' | ||
default: | ||
throw new Error('Unsupported architecture') | ||
} | ||
default: | ||
throw new Error('Unsupported platform') | ||
} | ||
} | ||
|
||
function determineTargetFileName(plat: Platform): string { | ||
switch (plat) { | ||
case Platform.Windows: | ||
return 'pkl.exe' | ||
default: | ||
return 'pkl' | ||
} | ||
} |