2

how I need to set up package.json to check if build success then add and commit changes to git?

Current build:

"build": "react-scripts build"
CoryCoolguy
  • 1,065
  • 8
  • 18
denismart
  • 492
  • 6
  • 15
  • Look up Husky, simple package that allows you to set up a pre commit command – Jon B May 27 '20 at 11:49
  • 1
    Consider doing something like [this answer](https://stackoverflow.com/questions/55470546/pass-git-commit-message-to-npm-script-and-append-to-predefined-string/55494338#55494338) whereby you run e.g. `npm run buildAndCommit -- "commit message for a new build"` - whereby you pass the git commit message as an argument to the `npm run ...` command. – RobC May 27 '20 at 11:57
  • @RobC yes, tnx so much – denismart May 27 '20 at 14:49

1 Answers1

1

Here the answer:

  "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] })\""
  }

Running like:

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

or:

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

Full answer here: Pass git commit message to npm script and append to predefined string

Tnx to RobC

denismart
  • 492
  • 6
  • 15
  • 1
    In [this solution](https://stackoverflow.com/questions/55470546/pass-git-commit-message-to-npm-script-and-append-to-predefined-string/55494338#55494338) that I previously provided, the reason for utilizing `node -e \" ... \"`, in the npm script was because that question wanted to prefix the given git message with `BUILD - `. Given the variant in your answer doesn't do that it's unnecessary to utilize `node -e \" ... \"` in your npm script. Instead you can just define your npm script as; `"build-and-commit": "npm run build && git add . && git commit -m"` - then run it with the same command – RobC May 28 '20 at 08:36
  • @RobC got error "error: switch `m' requires a value" – denismart May 28 '20 at 15:25
  • 1
    npm run build && git add . && git commit -m && git push -u origin staging – denismart May 28 '20 at 15:26
  • 1
    Sure, it will now because you've changed your compound command to `npm run build && git add . && git commit -m && git push -u origin staging`, However, I was referring to the example in your answer, which did not include the `&& git push -u origin staging` part. – RobC May 28 '20 at 15:30
  • @RobC its not possible to simplify it for command as i wrote? – denismart May 29 '20 at 08:36
  • 1
    Yes I know, it's not possible to simplify it for the compound commands that you wrote in the comments. The first comment I wrote regarding simplifying it was referring to the example commands provided in your answer. – RobC May 29 '20 at 08:40