Skip to content

Commit

Permalink
Move platform helper to helpers package
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolai-laevskii committed Aug 30, 2023
1 parent fdf7eec commit dcbbbfe
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 54 deletions.
19 changes: 0 additions & 19 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,22 +333,3 @@ toPlatformPath('/foo/bar') // => \foo\bar
// On a Linux runner.
toPlatformPath('\\foo\\bar') // => /foo/bar
```

#### Platform helper

Provides shorthands for getting information about platform action is running on.

```js
import { platform } from '@actions/core'
platform.platform // 'win32'
platform.arch // 'x64'
platform.isWindows // true
platform.isMacOS // false
platform.isLinux // false
const {
name, // Microsoft Windows 11 Enterprise
version, // 10.0.22621
} = await platform.getInfo()
```
27 changes: 0 additions & 27 deletions packages/core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"url": "https://github.com/actions/toolkit/issues"
},
"dependencies": {
"@actions/exec": "^1.1.1",
"@actions/http-client": "^2.0.1",
"uuid": "^8.3.2"
},
Expand Down
5 changes: 0 additions & 5 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,3 @@ export {markdownSummary} from './summary'
* Path exports
*/
export {toPosixPath, toWin32Path, toPlatformPath} from './path-utils'

/**
* Platform utils exports
*/
export * as platform from './platform'
19 changes: 19 additions & 0 deletions packages/helpers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

#### Platform helper

Provides shorthands for getting information about platform action is running on.

```js
import { platform } from '@actions/core'

platform.platform // 'win32'
platform.arch // 'x64'
platform.isWindows // true
platform.isMacOS // false
platform.isLinux // false

const {
name, // Microsoft Windows 11 Enterprise
version, // 10.0.22621
} = await platform.getInfo()
```
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/helpers/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@

export * as platform from './platform'
87 changes: 87 additions & 0 deletions packages/helpers/src/platform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os from 'os'
import * as exec from '@actions/exec'

const getWindowsInfo = async (): Promise<{name: string; version: string}> => {
const {stdout: version} = await exec.getExecOutput(
'powershell -command "(Get-CimInstance -ClassName Win32_OperatingSystem).Version"',
undefined,
{
silent: true
}
)

const {stdout: name} = await exec.getExecOutput(
'powershell -command "(Get-CimInstance -ClassName Win32_OperatingSystem).Caption"',
undefined,
{
silent: true
}
)

return {
name: name.trim(),
version: version.trim()
}
}

const getMacOsInfo = async (): Promise<{
name: string
version: string
}> => {
const {stdout} = await exec.getExecOutput('sw_vers', undefined, {
silent: true
})

const version = stdout.match(/ProductVersion:\s*(.+)/)?.[1] ?? ''
const name = stdout.match(/ProductName:\s*(.+)/)?.[1] ?? ''

return {
name,
version
}
}

const getLinuxInfo = async (): Promise<{
name: string
version: string
}> => {
const {stdout} = await exec.getExecOutput('lsb_release', ['-i', '-r', '-s'], {
silent: true
})

const [name, version] = stdout.trim().split('\n')

return {
name,
version
}
}

export const platform = os.platform()
export const arch = os.arch()
export const isWindows = platform === 'win32'
export const isMacOS = platform === 'darwin'
export const isLinux = platform === 'linux'

export async function getInfo(): Promise<{
name: string
platform: string
arch: string
version: string
isWindows: boolean
isMacOS: boolean
isLinux: boolean
}> {
return {
...(await (isWindows
? getWindowsInfo()
: isMacOS
? getMacOsInfo()
: getLinuxInfo())),
platform,
arch,
isWindows,
isMacOS,
isLinux
}
}
2 changes: 1 addition & 1 deletion packages/helpers/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
},
"include": [
"./src"
]
, "__tests__" ]
}

0 comments on commit dcbbbfe

Please sign in to comment.