Created
March 17, 2018 15:41
-
-
Save jemjam/bcd071e30c7b9390748174208c600a62 to your computer and use it in GitHub Desktop.
Precommit script for bumping patch version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Precommit script: | |
* | |
* Script will bump the minor version in the package.json unless the version was | |
* changed manually (in which case the version number is left unchanged) | |
*/ | |
const fs = require("fs"); | |
const path = require("path"); | |
const { exec } = require("child_process"); | |
const { promisify } = require("util"); | |
const read = promisify(fs.readFile); | |
const view = promisify(exec); | |
const pathToPackage = path.resolve(__dirname, "./package.json"); | |
const runAsync = async () => { | |
const previousFile = await view("git show HEAD:package.json").then(val => { | |
return val.stdout; | |
}); | |
const currentFile = await read(pathToPackage).then(val => { | |
return val.toString(); | |
}); | |
if (versionIsSame(previousFile, currentFile)) { | |
const newVersion = incrementVersion(currentFile); | |
fs.writeFile(pathToPackage, newVersion, err => { | |
exec(`git add ${pathToPackage}`, () => { | |
console.log("Patch version bumped by one..."); | |
}); | |
}); | |
} | |
}; | |
runAsync(); | |
/** | |
* Take a string representing a package.json file | |
* and increment the version value in it. | |
*/ | |
const incrementVersion = inputPkgJson => { | |
versionRE = /"(\d+)\.(\d+)\.(\d+)"/; | |
const inputArray = inputPkgJson.split("\n"); | |
const versionLineIndex = inputArray.findIndex(e => { | |
return e.toLowerCase().includes('"version"'); | |
}); | |
const oldVersion = inputArray[versionLineIndex]; | |
const minorValue = oldVersion.match(versionRE)[3]; | |
const incrementedValue = parseInt(minorValue, 10) + 1; | |
const newVersionLine = oldVersion.replace( | |
versionRE, | |
`"$1.$2.${incrementedValue}"` | |
); | |
inputArray[versionLineIndex] = newVersionLine; | |
return inputArray.join("\n"); | |
}; | |
/** | |
* Predicate: returns true only if the version number is same for both contents | |
*/ | |
const versionIsSame = (prev, curr) => { | |
const previousVersion = prev.split("\n").find(e => { | |
return e.toLowerCase().includes('"version"'); | |
}); | |
const nextVersion = curr.split("\n").find(e => { | |
return e.toLowerCase().includes('"version"'); | |
}); | |
return previousVersion === nextVersion; | |
}; |
Maybe future enhancement: accept a cli arg to increment the minor or major version instead. (Could be left as a precommit hook because the only time it'll change your file is if the version was untouched since the last commit.)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
something I hacked together after looking for a package that would do it. I found packages yes, but many of them also automated pushing back to git or publishing on npm etc.
All I want is if I've forgotten to bump any version at all, that the patch increments once with each commit.