Skip to content

Instantly share code, notes, and snippets.

@AndrewJHart
Forked from zmts/update-build.md
Created April 21, 2022 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewJHart/e98b55db0508f56f16654ed411f76f60 to your computer and use it in GitHub Desktop.
Save AndrewJHart/e98b55db0508f56f16654ed411f76f60 to your computer and use it in GitHub Desktop.
Auto update application build version. Husky pre-commit hook.

Auto update application build version. Husky pre-commit hook.

package.json

"husky": {
    "hooks": {
      "pre-commit": "npm run build && node ./update-build.js"
    }
  }

update-build.js

(async () => {
  const fs = require('fs')
  const { exec } = require('child_process')
  const buildJson = require('./build.json')

  await checkGitConnection()
  buildJson.build++
  await writeBuild()
  await addBuildFileToGit()

  console.log('application updated to', buildJson.build, 'build ')

  function checkGitConnection() {
    return new Promise((resolve, reject) => {
      exec('git remote show origin', (e, stdout, stderr) => {
        if (e) return reject(e)
        if (stderr) return reject(stderr)
        return resolve(stdout)
      })
    })
  }

  function writeBuild() {
    return new Promise((resolve, reject) => {
      fs.writeFile('./build.json', JSON.stringify(buildJson), e => {
        if (e) return reject(e)
        return resolve()
      })
    })
  }

  function addBuildFileToGit() {
    return new Promise((resolve, reject) => {
      exec('git add ./build.json', (e, stdout, stderr) => {
        if (e) return reject(e)
        if (stderr) return reject(stderr)
        return resolve(stdout)
      })
    })
  }
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment