11# Requires -PSEdition Core -Version 7.2
2- [SemVer ]$RequireVersionMinimum = [SemVer ]::Parse(' 16.13.0' )
2+ [SemVer ]$NodeJsVersionMinimum = [SemVer ]::Parse(' 16.13.0' )
33[String ]$WrapperRoot = Join-Path - Path $PSScriptRoot - ChildPath ' ..\nodejs-wrapper'
44[String ]$WrapperPackageMetaFilePath = Join-Path - Path $WrapperRoot - ChildPath ' package.json'
55[String ]$WrapperScriptFilePath = Join-Path - Path $WrapperRoot - ChildPath ' main.js'
66<#
7- . SYNOPSIS
8- GitHub Actions - Internal - Invoke NodeJS Wrapper
97. DESCRIPTION
108Invoke NodeJS wrapper.
119. PARAMETER Name
@@ -27,50 +25,37 @@ Function Invoke-NodeJsWrapper {
2725 [Parameter (Mandatory = $True , Position = 1 )][Alias (' Arguments' )][Hashtable ]$Argument
2826 )
2927 Try {
30- $CommandMeta = Get-Command - Name ' node' - CommandType ' Application' - ErrorAction ' SilentlyContinue'
31- If ($Null -ieq $CommandMeta ) {
32- Throw ' NodeJS is not exist, or not accessible and usable!'
28+ If ($Null -eq (Get-Command - Name ' node' - CommandType ' Application' - ErrorAction ' SilentlyContinue' )) {
29+ Throw ' NodeJS is not exist, accessible, or usable!'
3330 }
3431 Try {
35- [PSCustomObject ] $VersionsTable = node -- no - deprecation -- no - warnings ' --eval=console.log(JSON.stringify(process.versions)); ' * > & 1 |
36- Join-String - Separator " `n " |
37- ConvertFrom-Json - Depth 100
38- [ SemVer ] $CurrentVersion = [ SemVer ]::Parse( $VersionsTable .node )
32+ [SemVer ] $NodeJsVersionCurrent = [ SemVer ]::Parse((
33+ node -- no - deprecation -- no - warnings ' --eval=console.log(process.versions.node); ' * > & 1 |
34+ Join-String - Separator " `n "
35+ ) )
3936 }
4037 Catch {
41- Throw ' NodeJS versions table is not parsable !'
38+ Throw ' unable to get NodeJS version !'
4239 }
43- If ($RequireVersionMinimum -gt $CurrentVersion ) {
44- Throw ' NodeJS is not fulfill the requirement!'
40+ If ($NodeJsVersionMinimum -gt $NodeJsVersionCurrent ) {
41+ Throw " NodeJS version is not fulfill the requirement! Current: $ ( $NodeJsVersionCurrent .ToString () ) ; Expect: >= $ ( $NodeJsVersionMinimum .ToString () ) "
4542 }
46- ForEach ($FilePath In @ ($WrapperPackageMetaFilePath , $WrapperScriptFilePath )) {
47- If (! (Test-Path - LiteralPath $FilePath - PathType ' Leaf' )) {
48- Throw " wrapper resource `" $FilePath `" is missing!"
49- }
43+ If (! (Test-Path - LiteralPath $WrapperPackageMetaFilePath - PathType ' Leaf' )) {
44+ Throw " NodeJS wrapper package meta file ($WrapperPackageMetaFilePath ) is missing!"
5045 }
51- If ([String ]::IsNullOrEmpty($Env: RUNNER_TEMP )) {
52- Throw ' environment variable `RUNNER_TEMP` is not defined!'
53- }
54- If (! [System.IO.Path ]::IsPathFullyQualified($Env: RUNNER_TEMP )) {
55- Throw " `` $Env: RUNNER_TEMP `` (environment variable `` RUNNER_TEMP`` ) is not a valid absolute path!"
56- }
57- If (! (Test-Path - LiteralPath $Env: RUNNER_TEMP - PathType ' Container' )) {
58- Throw " path `` $Env: RUNNER_TEMP `` is not initialized!"
46+ If (! (Test-Path - LiteralPath $WrapperScriptFilePath - PathType ' Leaf' )) {
47+ Throw " NodeJS wrapper script file ($WrapperScriptFilePath ) is missing!"
5948 }
6049 }
6150 Catch {
6251 Write-Error - Message " This function depends and requires to invoke with the compatible NodeJS environment, but $_ " - Category ' ResourceUnavailable'
6352 Return
6453 }
65- Do {
66- [String ]$ExchangeFilePath = Join-Path - Path $Env: RUNNER_TEMP - ChildPath ([System.IO.Path ]::GetRandomFileName())
67- }
68- While (Test-Path - LiteralPath $ExchangeFilePath - PathType ' Leaf' )
54+ [String ]$ArgumentStringify = $Argument |
55+ ConvertTo-Json - Depth 100 - Compress
56+ [String ]$Token = (New-Guid ).Guid.ToLower() -ireplace ' -' , ' '
6957 Try {
70- @ { ' $name' = $Name } + $Argument |
71- ConvertTo-Json - Depth 100 - Compress |
72- Set-Content - LiteralPath $ExchangeFilePath - Confirm:$False - Encoding ' UTF8NoBOM'
73- [String ]$StdOut = node -- no- deprecation -- no- warnings $WrapperScriptFilePath $ExchangeFilePath * > & 1 |
58+ [String ]$StdOut = node -- no- deprecation -- no- warnings $WrapperScriptFilePath $Name $ArgumentStringify $Token * > & 1 |
7459 Where-Object - FilterScript {
7560 If ($_ -imatch ' ^::.+?::.*$' ) {
7661 Write-Host - Object $_
@@ -82,22 +67,19 @@ Function Invoke-NodeJsWrapper {
8267 If ($LASTEXITCODE -ne 0 ) {
8368 Throw " [Exit Code $LASTEXITCODE ] $StdOut "
8469 }
85- [PSCustomObject ]$Result = Get-Content - LiteralPath $ExchangeFilePath - Raw - Encoding ' UTF8NoBOM' |
70+ If ($StdOut -inotmatch " (?<=$ ( $Token ) \r?\n)(?:.|\r?\n)*?(?=\r?\n$ ( $Token ) )" ) {
71+ Throw ' No data return.'
72+ }
73+ [PSCustomObject ]$Result = $Matches [0 ] |
8674 ConvertFrom-Json - Depth 100
8775 If (! $Result.IsSuccess ) {
8876 Throw $Result.Reason
8977 }
90- $Result.Result |
91- Write-Output
78+ Write-Output - InputObject $Result.Result
9279 }
9380 Catch {
9481 Write-Error - Message " Unable to successfully invoke the NodeJS wrapper `` $Name `` : $_ " - Category ' InvalidData'
9582 }
96- Finally {
97- If (Test-Path - LiteralPath $ExchangeFilePath - PathType ' Leaf' ) {
98- Remove-Item - LiteralPath $ExchangeFilePath - Force - Confirm:$False - ErrorAction ' Continue'
99- }
100- }
10183}
10284Export-ModuleMember - Function @ (
10385 ' Invoke-NodeJsWrapper'
0 commit comments