如何允许用户在package.json脚本的shell脚本中输入值

问题描述 投票:0回答:1

我正在使用 Percy 和 Cypress 在我的 Nextjs 应用程序上运行可视化测试。

要运行单个规范,我将其添加到 package.json 的脚本中 例如

"scripts": {
  "test-visual:single": "start-server-and-test 'npm run dev' http://localhost:3000 'percy exec -- cypress run --spec /path/to/test.cy.ts'",
},

我希望用户输入测试规范的路径(/path/to/test.cy.ts)。正确的做法是什么?

我尝试过使用 $PATH

$PATH=/path/to/test.cy.ts npm run test-visual:single

"test-visual:single": "start-server-and-test 'npm run dev' http://localhost:3000 'percy exec -- cypress run --spec $PATH'",

我也尝试过:

npm run test-visual:single /path/to/test.cy.ts

"scripts": {
  "test-visual:single": "start-server-and-test 'npm run dev' http://localhost:3000 
 'percy exec -- cypress run --spec $1'",
},

没有运气

shell package.json
1个回答
0
投票

您可以维护 package.json 的模板版本,例如

package.json.template
:

...
"scripts": {
  "test-visual:single": "start-server-and-test 'npm run dev' http://localhost:3000 'percy exec -- cypress run --spec $tpath'",
},
...

现在假设在使用此 json 的脚本中,用户(调用者)将要使用的路径作为脚本的第一个参数。然后你可以做一个

tpath=$1
export tpath
envsubst '$tpath' <package.json.template >package.json

如果您只需要这一处的路径,当然可以简化为

tpath=$1 envsubst '$tpath' <package.json.template >package.json

如果你确定你的模板文件不包含任何其他

$
,这可能会被错误地理解为变量引用,你可以写得更简单:

tpath=$1 envsubst <package.json.template >package.json
© www.soinside.com 2019 - 2024. All rights reserved.