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
