codekingpro/portable-devtools
114k
1#!/usr/bin/env pwsh
2
3Set-StrictMode -Version 'Latest'
4
5$NODE_EXE="$PSScriptRoot/node.exe"
6if (-not (Test-Path $NODE_EXE)) {
7 $NODE_EXE="$PSScriptRoot/node"
8}
9if (-not (Test-Path $NODE_EXE)) {
10 $NODE_EXE="node"
11}
12
13$NPM_PREFIX_JS="$PSScriptRoot/node_modules/npm/bin/npm-prefix.js"
14$NPX_CLI_JS="$PSScriptRoot/node_modules/npm/bin/npx-cli.js"
15$NPM_PREFIX=(& $NODE_EXE $NPM_PREFIX_JS)
16
17if ($LASTEXITCODE -ne 0) {
18 Write-Host "Could not determine Node.js install directory"
19 exit 1
20}
21
22$NPM_PREFIX_NPX_CLI_JS="$NPM_PREFIX/node_modules/npm/bin/npx-cli.js"
23if (Test-Path $NPM_PREFIX_NPX_CLI_JS) {
24 $NPX_CLI_JS=$NPM_PREFIX_NPX_CLI_JS
25}
26
27if ($MyInvocation.ExpectingInput) { # takes pipeline input
28 $input | & $NODE_EXE $NPX_CLI_JS $args
29} elseif (-not $MyInvocation.Line) { # used "-File" argument
30 & $NODE_EXE $NPX_CLI_JS $args
31} else { # used "-Command" argument
32 if (($MyInvocation | Get-Member -Name 'Statement') -and $MyInvocation.Statement) {
33 $NPX_ORIGINAL_COMMAND = $MyInvocation.Statement
34 } else {
35 $NPX_ORIGINAL_COMMAND = (
36 [Management.Automation.InvocationInfo].GetProperty('ScriptPosition', [Reflection.BindingFlags] 'Instance, NonPublic')
37 ).GetValue($MyInvocation).Text
38 }
39
40 $NODE_EXE = $NODE_EXE.Replace("``", "````")
41 $NPX_CLI_JS = $NPX_CLI_JS.Replace("``", "````")
42
43 $NPX_COMMAND_ARRAY = [Management.Automation.Language.Parser]::ParseInput($NPX_ORIGINAL_COMMAND, [ref] $null, [ref] $null).
44 EndBlock.Statements.PipelineElements.CommandElements.Extent.Text
45 $NPX_ARGS = ($NPX_COMMAND_ARRAY | Select-Object -Skip 1) -join ' '
46
47 Invoke-Expression "& `"$NODE_EXE`" `"$NPX_CLI_JS`" $NPX_ARGS"
48}
49
50exit $LASTEXITCODE
51 