【问题标题】:Pass argument to npm script command by inserting it, not appending通过插入而不是附加将参数传递给 npm 脚本命令
【发布时间】:2019-01-29 14:26:54
【问题描述】:

我想在命令中的某处插入参数。以某种方式对其进行插值。例如:

{
    "scripts": {
        "foo": "git commit -am $message && git push"
    }
}

然后运行:

$ npm run foo -- --message "Baz"

或者是这样的:

"foo": "git commit -am {0} && git push"

然后运行:

$ npm run foo -- "Lorem"

请注意这里的 git 命令仅用于演示目的,我们有不同的多个命令。所以 Git 别名不是解决方案。

有什么方法可以通过 npm 实现这种行为吗?

【问题讨论】:

  • 我的回答 here 提供了一个解决方案,可以满足您将参数传递到 npm 脚本中间的要求。同样here
  • 另一个例子还有here

标签: npm npm-scripts


【解决方案1】:

感谢@RobC,事实证明我们可以为 npm 脚本指定 shell,所以在 windows 上我可以为它设置 git bash 并使用@RobC 解决方案 here

$ npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"

package.json

"foo": "func() { echo \"$1\"; }; func"

然后运行:

$ npm start foo "lorem ipsum"

【讨论】:

  • 谢谢!有趣。是的,这是一个替代解决方案,我相信它符合您的用例。然而,它不是很容易跨平台移植(不像 nodejs 解决方案),因为它依赖于指定用户配置和具有 git bash 的旧版本的 Windows。
猜你喜欢
  • 2019-02-08
  • 2022-10-04
  • 2020-05-09
  • 2012-06-19
  • 2017-04-20
  • 1970-01-01
  • 2023-03-05
相关资源
最近更新 更多