@@ -26,82 +26,74 @@ Function Invoke-NodeJsWrapper {
2626 [Parameter (Mandatory = $True , Position = 0 )][ValidatePattern (' ^.+$' , ErrorMessage = ' Value is not a single line string!' )][String ]$Name ,
2727 [Parameter (Mandatory = $True , Position = 1 )][Alias (' Arguments' )][Hashtable ]$Argument
2828 )
29- Begin {
30- [Boolean ]$ShouldProceed = $True
29+ 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!'
33+ }
3134 Try {
32- $CommandMeta = Get-Command - Name ' node' - CommandType ' Application' - ErrorAction ' SilentlyContinue'
33- If ($Null -ieq $CommandMeta ) {
34- Throw ' NodeJS is not exist, or not accessible and usable!'
35- }
36- Try {
37- [PSCustomObject ]$VersionsTable = node -- no- deprecation -- no- warnings ' --eval=console.log(JSON.stringify(process.versions));' * > & 1 |
38- Join-String - Separator " `n " |
39- ConvertFrom-Json - Depth 100
40- [SemVer ]$CurrentVersion = [SemVer ]::Parse($VersionsTable.node )
41- }
42- Catch {
43- Throw ' NodeJS versions table is not parsable!'
44- }
45- If ($RequireVersionMinimum -gt $CurrentVersion ) {
46- Throw ' NodeJS is not fulfill the requirement!'
47- }
48- ForEach ($FilePath In @ ($WrapperPackageMetaFilePath , $WrapperScriptFilePath )) {
49- If (! (Test-Path - LiteralPath $FilePath - PathType ' Leaf' )) {
50- Throw " wrapper resource `" $FilePath `" is missing!"
51- }
52- }
53- If ([String ]::IsNullOrEmpty($Env: RUNNER_TEMP )) {
54- Throw ' environment variable `RUNNER_TEMP` is not defined!'
55- }
56- If (! [System.IO.Path ]::IsPathFullyQualified($Env: RUNNER_TEMP )) {
57- Throw " `` $Env: RUNNER_TEMP `` (environment variable `` RUNNER_TEMP`` ) is not a valid absolute path!"
58- }
59- If (! (Test-Path - LiteralPath $Env: RUNNER_TEMP - PathType ' Container' )) {
60- Throw " path `` $Env: RUNNER_TEMP `` is not initialized!"
61- }
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 )
6239 }
6340 Catch {
64- $ShouldProceed = $False
65- Write-Error - Message " This function depends and requires to invoke with the compatible NodeJS environment, but $_ " - Category ' ResourceUnavailable'
66- }
67- }
68- Process {
69- If (! $ShouldProceed ) {
70- Return
41+ Throw ' NodeJS versions table is not parsable!'
7142 }
72- Do {
73- [ String ] $ExchangeFilePath = Join-Path - Path $ Env: RUNNER_TEMP - ChildPath ([ System.IO.Path ]::GetRandomFileName())
43+ If ( $RequireVersionMinimum -gt $CurrentVersion ) {
44+ Throw ' NodeJS is not fulfill the requirement! '
7445 }
75- While (Test-Path - LiteralPath $ExchangeFilePath - PathType ' Leaf' )
76- Try {
77- @ { ' $name' = $Name } + $Argument |
78- ConvertTo-Json - Depth 100 - Compress |
79- Set-Content - LiteralPath $ExchangeFilePath - Confirm:$False - Encoding ' UTF8NoBOM'
80- [String ]$StdOut = node -- no- deprecation -- no- warnings $WrapperScriptFilePath $ExchangeFilePath * > & 1 |
81- Where-Object - FilterScript {
82- If ($_ -imatch ' ^::.+?::.*$' ) {
83- Write-Host - Object $_
84- Return $False
85- }
86- Return $True
87- } |
88- Join-String - Separator " `n "
89- If ($LASTEXITCODE -ne 0 ) {
90- Throw " [Exit Code $LASTEXITCODE ] $StdOut "
91- }
92- [PSCustomObject ]$Result = Get-Content - LiteralPath $ExchangeFilePath - Raw - Encoding ' UTF8NoBOM' |
93- ConvertFrom-Json - Depth 100
94- If (! $Result.IsSuccess ) {
95- Throw $Result.Reason
46+ ForEach ($FilePath In @ ($WrapperPackageMetaFilePath , $WrapperScriptFilePath )) {
47+ If (! (Test-Path - LiteralPath $FilePath - PathType ' Leaf' )) {
48+ Throw " wrapper resource `" $FilePath `" is missing!"
9649 }
97- $Result.Result |
98- Write-Output
9950 }
100- Catch {
101- Write-Error - Message " Unable to successfully invoke the NodeJS wrapper `` $Name `` : $_ " - Category ' InvalidData '
51+ If ([ String ]::IsNullOrEmpty( $ Env: RUNNER_TEMP )) {
52+ Throw ' environment variable `RUNNER_TEMP` is not defined! '
10253 }
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!"
59+ }
60+ }
61+ Catch {
62+ Write-Error - Message " This function depends and requires to invoke with the compatible NodeJS environment, but $_ " - Category ' ResourceUnavailable'
63+ Return
64+ }
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' )
69+ 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 |
74+ Where-Object - FilterScript {
75+ If ($_ -imatch ' ^::.+?::.*$' ) {
76+ Write-Host - Object $_
77+ Return $False
78+ }
79+ Return $True
80+ } |
81+ Join-String - Separator " `n "
82+ If ($LASTEXITCODE -ne 0 ) {
83+ Throw " [Exit Code $LASTEXITCODE ] $StdOut "
84+ }
85+ [PSCustomObject ]$Result = Get-Content - LiteralPath $ExchangeFilePath - Raw - Encoding ' UTF8NoBOM' |
86+ ConvertFrom-Json - Depth 100
87+ If (! $Result.IsSuccess ) {
88+ Throw $Result.Reason
89+ }
90+ $Result.Result |
91+ Write-Output
92+ }
93+ Catch {
94+ Write-Error - Message " Unable to successfully invoke the NodeJS wrapper `` $Name `` : $_ " - Category ' InvalidData'
10395 }
104- End {
96+ Finally {
10597 If (Test-Path - LiteralPath $ExchangeFilePath - PathType ' Leaf' ) {
10698 Remove-Item - LiteralPath $ExchangeFilePath - Force - Confirm:$False - ErrorAction ' Continue'
10799 }
0 commit comments