发送参数到 package.json 脚本

huangapple go评论87阅读模式
英文:

Send parameter to package.json script

问题

I have this script on package.json file:

"test:debug": "yarn build && playwright test --project=chromium-debug -c build && ts-node ./src/logs/generateLog.ts"

When I run this command: yarn test:debug -g 'myTestName'
I see in the terminal that it runs this command:

yarn build && playwright test --project=chromium-debug -c build && ts-node ./src/logs/generateLog.ts -g 'myTestName'

but I want it to run this command:

yarn build && playwright test --project=chromium-debug -c build -g 'myTestName' && ts-node ./src/logs/generateLog.ts

What should I change in my script in order to achieve that?

英文:

I have this script on package.json file:

"test:debug": "yarn build && playwright test --project=chromium-debug -c build && ts-node ./src/logs/generateLog.ts"

When I run this command: yarn test:debug -g 'myTestName'
I see in the terminal that it runs this command:

yarn build && playwright test --project=chromium-debug -c build && ts-node ./src/logs/generateLog.ts -g 'myTestName'

but I want it to run this command:

yarn build && playwright test --project=chromium-debug -c build -g 'myTestName' && ts-node ./src/logs/generateLog.ts

What should I change in my script in order to achieve that?

答案1

得分: 0

"test:debug" 在 package.json 中可以这样定义:

"test:debug": "yarn build && playwright test --project=chromium-debug -c build -g 'myTestName' && ts-node ./src/logs/generateLog.ts"

然后只需运行 yarn test:debug


编辑

一个解决方法是使用 Makefile 来实现你期望的结果,但首先确保你的设备上安装了 make

在项目的根目录中,创建一个新的 Makefile,然后将以下简单命令添加到其中(不要忘记保存 Makefile):

test:
	yarn build && playwright test --project=chromium-debug -c build -g '$(g)' && ts-node ./src/logs/generateLog.ts

(注意第二行的缩进,在你的IDE中,你应该将第二行的缩进替换为使用 <kbd>TAB</kbd> 而不是4个空格,否则 make 会报错)

现在在终端中调用该命令的方式如下:

make test g="myTestName"

这样你可以在调用命令时调整 "g" 变量,并实现所期望的结果。

英文:

You can define the command in package.json like that:

&quot;test:debug&quot;: &quot;yarn build &amp;&amp; playwright test --project=chromium-debug -c build -g &#39;myTestName&#39; &amp;&amp; ts-node ./src/logs/generateLog.ts&quot;

And then simply just run yarn test:debug


EDIT

A workaround would be to use a Makefile to achieve your desired result instead, but first make sure that make is installed on your device.

In your project root folder. Create a new Makefile and put this simple command in it (don’t forget to save the Makefile):

test:
    yarn build &amp;&amp; playwright test --project=chromium-debug -c build -g &#39;$(g)&#39; &amp;&amp; ts-node ./src/logs/generateLog.ts

(Notice the indentation in the second line, in your IDE you should replace the indentation of the second line to use a <kbd>TAB</kbd> instead of 4 spaces like I did here otherwise make will fire an error)

Now calling the command in a terminal would be like that:

make test g=&quot;myTestName&quot;

This will let you to tweak the ”g” variable when calling the command and it will achieve the desired result.

huangapple
  • 本文由 发表于 2023年6月8日 03:02:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定