node_modues/.bin文件夹下,对于一个npm包,有两个可执行文件,没有后缀名的是是对应unix系的shell脚本,.cmd文件对应的是windows bat脚本,内容都是用node执行一个js文件
@IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\..\webpack\bin\webpack.js" %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0\..\webpack\bin\webpack.js" %*
)
这里是windows的cmd中的语法 ~dp0指执行脚本的当前目录 这句话的意思是如果当前目录下有node.exe,就用node.exe执行... ...webpack.js文件 %*是指执行bat时命令中输入的后续参数 否则 @SETLOCAL设置本次批处理命令中的环境变量 PATHEXT是windows下的文件扩展名环境变量 后面的语法是从PATHEXT中删除.JS 然后执行 node ... ... webpack.js 命令, 去除掉扩展名的作用是为了防止执行到node.js文件 比如当前文件夹下有一个node.js文件, 如果直接执行node命令可能会默认用vscode打开这个.js文件
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/setlocal
https://stackoverflow.com/questions/112055/what-does-d0-mean-in-a-windows-batch-file
另一个不带.cmd结尾的是为unix系统准备的:
#!/bin/sh
# $() https://www.cnblogs.com/chengd/p/7803664.html 做命令替换的
# sed https://www.runoob.com/linux/linux-comm-sed.html https://stackoverflow.com/questions/38934138/what-result-do-sed-s-g-and-egrep-example-variablename-give
# $0 https://unix.stackexchange.com/questions/280454/what-is-the-meaning-of-0-in-the-bash-shell
# 获取当前执行bash的dirname( strip last component from file name ),命名为basedir这个变量
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
# 如果uname 以 *开头 中间有CYGWIN后面有*结束 basedir是 cygpath -w "$basedir"
# cygpath -w 是以windows风格路径分割符处理$basedir
# case...in...esac语法 http://c.biancheng.net/view/2767.html
case `uname` in
*CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac
# if[]...then...else...fi语法 https://blog.csdn.net/puqutogether/article/details/45815003
# $@ 代表所有参数 https://superuser.com/questions/694501/what-does-mean-as-a-bash-script-function-parameter
# $?代表最近执行的返回值 https://unix.stackexchange.com/questions/7704/what-is-the-meaning-of-in-a-shell-script
# -x 如果后面文件存在是true http://man7.org/linux/man-pages/man1/dash.1.html (-x file)
if [ -x "$basedir/node" ]; then
"$basedir/node" "$basedir/../webpack/bin/webpack.js" "$@"
ret=$?
else
node "$basedir/../webpack/bin/webpack.js" "$@"
ret=$?
fi
exit $ret