我可以在 package.json 的脚本 属性 中为文件名添加一个变量吗

Can I put a variable for a filename in the scripts property of package.json

我想在不指定文件名的情况下做这样的事情:

"scripts": {
  "test <any_file.js>": "*actual command* <any_file.js>"
}

你真正想做的是阅读一个论点。您可以为此使用 process.argv。有关全局节点变量 process 的更多信息 here.

当您的脚本标签看起来像这样时:

"scripts": {
  "my-command": "my-script.js"
}

当您的脚本文件如下所示时:

console.log( process.argv );

然后剩下要做的就是 运行 您的命令 npmyarn

npm run my-command <your-argument e.g. file.js>

如果你想把它传递给另一个命令,你可以用 require( 'child_process' ).exec 调用那个命令。 here.

就是一个很好的例子