Skip to main content

link包到全局

文件头部

image-20220323172222854

非常重要:无论什么系统,都需要在执行文件的头部添加 #!/usr/bin/env node

使用流程

# 首先切换到需要链接的npm包根目录(package.json的目录)
cd /d {project_dir}

npm link

cps-cli

image-20220323171953846

同时node的根目录会生成如下文件:

image-20220323172047522

快捷脚本

  • cps-cli
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")

case `uname` in
*CYGWIN*|*MINGW*|*MSYS*) basedir=`cygpath -w "$basedir"`;;
esac

if [ -x "$basedir/node" ]; then
exec "$basedir/node" "$basedir/node_modules/cps-cli/src/index.mjs" "$@"
else
exec node "$basedir/node_modules/cps-cli/src/index.mjs" "$@"
fi
  • .cmd
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent

$exe=""
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
$exe=".exe"
}
$ret=0
if (Test-Path "$basedir/node$exe") {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "$basedir/node$exe" "$basedir/node_modules/cps-cli/src/index.mjs" $args
} else {
& "$basedir/node$exe" "$basedir/node_modules/cps-cli/src/index.mjs" $args
}
$ret=$LASTEXITCODE
} else {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "node$exe" "$basedir/node_modules/cps-cli/src/index.mjs" $args
} else {
& "node$exe" "$basedir/node_modules/cps-cli/src/index.mjs" $args
}
$ret=$LASTEXITCODE
}
exit $ret

*.ps1

@ECHO off
GOTO start
:find_dp0
SET dp0=%~dp0
EXIT /b
:start
SETLOCAL
CALL :find_dp0

IF EXIST "%dp0%\node.exe" (
SET "_prog=%dp0%\node.exe"
) ELSE (
SET "_prog=node"
SET PATHEXT=%PATHEXT:;.JS;=;%
)

endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\node_modules\cps-cli\src\index.mjs" %*