Skip to content

Stdin pass to exe in Powershell (Issue with npm link)

mapoart edited this page Jan 23, 2020 · 2 revisions

During implementing of Powershell to Nexss Programmer I have noticed that STDIN is not passed to the nexss programmer despite code is written ok.

echo '{"x":1}' | nexss anyprogram.js # didn't work !
nexss myprogram.js | nexss myprogram.cpp # also didn't work !

The reason was quite not obvious, but it was in the file nexss.ps1 which was created by the npm-link command. I have rewritten the file so it seems to work now:

#!/usr/bin/env pwsh
$basedir = Split-Path $MyInvocation.MyCommand.Definition -Parent

$node = "node"
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
  # Fix case when both the Windows and Linux builds of Node
  # are installed in the same directory
  $node = "node.exe"
}
$ret = 0

if (Test-Path "$basedir/$node") {
  $node = "$basedir/$node"
}

if ($myinvocation.expectingInput) {
  $input | & $node  "$basedir/node_modules/@nexssp/cli/nexss.js" $args
  $ret = $LASTEXITCODE
}
else {
  & $node  "$basedir/node_modules/@nexssp/cli/nexss.js" $args
  $ret = $LASTEXITCODE
}
  
exit $ret