如何在Windows和Unix上运行npm sh脚本
我想运行一个sh
脚本,通过npm scripts
来检测我的npm
版本。 我有以下脚本,它很酷:
if [ $(npm -v | cut -c 1 | awk '{print $1\"<3\"}' | bc -l) != 0 ]; then echo '[ERROR] You need npm version @>=3\n' && false; fi
但是,当我在Windows上运行脚本时,这个脚本只能在* NIX上工作我得到一个错误:
$(npm was unexpected at this time.
我想检测,如果我在Windows上运行的脚本,然后不执行脚本,我试过,但只能在* NIX:
if [ 'uname -s' != CYGWIN* ]; then if [ $(npm -v | cut -c 1 | awk '{print $1\"<3\"}' | bc -l) != 0 ]; then echo '[ERROR] You need npm version @>=3\n' && false; fi; fi
然后我得到:
'uname was unexpected at this time.
我正在检查相当于Windows上的uname
是systeminfo
但是,如果我使用systeminfo
那么我得到了* NIX的未定义。
任何想法如何使这个脚本工作在Windows && * NIX?
如果你想在不是Windows的平台上执行你的bash脚本,你可以使用os.platform()
来检查平台本身(假设节点已经安装os.platform()
scripts: { checkNpmVersion: "node -e \"process.exit(require('os').platform() === 'win32')\" && ./check-npm.sh" }
或者甚至更好,你可以在节点上写下你的小脚本,这样它就可以在任何平台上工作(再次假设节点已经安装)
// check-npm.js const exec = require('child_process').exec exec('npm -v', function (err, stdout, stderr) { if (err) throw err if (stdout.match(/^3/)) throw "npm>3 required" })
并在脚本领域
scripts: { checkNpmVersion: "node check-npm.js" }
使用Node v4.4.5和npm 2.15.5testingWin7