![](/img/trans.png)
[英]How to run 2 and more npm tasks in parallell by ".sh" file at Windows?
[英]How to run npm sh script on Windows and Unix
我想運行一個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"
}
在Win7的Node v4.4.5和npm 2.15.5上進行了測試
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.