【问题标题】:npm run build and if success then commit [duplicate]npm run build,如果成功则提交 [重复]
【发布时间】:2020-09-14 10:49:21
【问题描述】:

我需要如何设置 package.json 来检查构建是否成功,然后将更改添加并提交到 git?

当前版本:

"build": "react-scripts build"

【问题讨论】:

  • 查找 Husky,简单的包,允许您设置预提交命令
  • 考虑做类似this answer 这样你运行的事情,例如npm run buildAndCommit -- "commit message for a new build" - 您将 git commit 消息作为参数传递给 npm run ... 命令。
  • @RobC 是的,tnx 这么多

标签: git npm yarnpkg package.json


【解决方案1】:

答案在这里:

  "scripts": {
    "build": "react-scripts build",
    "build-and-commit": "node -e \"const mssg = process.argv[1]; require('child_process').execSync('npm run build && git add . && git commit -m \\\"' + mssg + '\\\"', { stdio:[0, 1, 2] })\""
  }

运行方式:

$ npm run build-and-commit -- "commit message"

或:

$ yarn build-and-commit -- "commit message"

完整答案在这里:Pass git commit message to npm script and append to predefined string

转至RobC

【讨论】:

  • 在我之前提供的this solution 中,在npm 脚本中使用node -e \" ... \" 的原因是因为该问题想在给定的git 消息前面加上BUILD - 。鉴于您的答案中的变体并没有这样做,因此没有必要在您的 npm 脚本中使用 node -e \" ... \" 。相反,您可以将您的 npm 脚本定义为; "build-and-commit": "npm run build && git add . && git commit -m" - 然后使用相同的命令运行它
  • @RobC 得到错误“错误:开关 `m' 需要一个值”
  • npm run build && git add 。 && git commit -m && git push -u origin staging
  • 当然可以,因为您已将复合命令更改为npm run build && git add . && git commit -m && git push -u origin staging,但是,我指的是您回答中的示例,其中不包括&& git push -u origin staging 部分。跨度>
  • 是的,我知道,对于您在 cmets 中编写的复合命令,无法简化它。我写的关于简化它的第一条评论是指您的答案中提供的示例命令。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-03
  • 2020-04-05
  • 1970-01-01
  • 2022-11-16
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多