[英]How to pass r-value of an environment variable as a CLI argument for an NPM script
目標:有沒有一種方法或技巧可以讓我將環境變量的右值添加為 NPM 腳本的 CLI 參數,而無需調用單獨的腳本?
示例:
"test:run": cypress run --spec 'tests/*' --env host=
npm run test:run localhost
cypress run --spec 'tests/*' --env host=localhost
我目前使用的是:
"test:run": cypress run --spec 'tests/*' --env
npm run test:run host=localhost
cypress run --spec 'tests/*' --env host=localhost
這行得通,但我想知道是否有一種方法甚至可以省略在我的命令中添加host=
,這樣我就可以只寫 r-value, localhost
為簡潔起見。
我不認為你想要實現的目標目前是你想要實現的,因為 npm 如何突破命令行 arguments。傳入的任何內容都作為單獨的“選項”附加,而不是僅僅添加到現有字符串,這是您當前看到的行為( npm run test:run localhost
=> cypress run --spec 'tests/*' --env host= localhost
)。
有一些替代方案,它們似乎是您不想做的,但您可以為該行為創建一個單獨的腳本。
"test:run": "cypress run --spec 'tests/*' --env"
"test:run:local": "npm run test:run host=localhost"
或者,您可以在測試命令之前聲明 Cypress 環境變量。
CYPRESS_HOST=localhost npm run test:run
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.