Skip to content

Instantly share code, notes, and snippets.

@michaelrhodes
Last active August 29, 2015 14:13
Show Gist options
  • Save michaelrhodes/42d480539c5e06914e45 to your computer and use it in GitHub Desktop.
Save michaelrhodes/42d480539c5e06914e45 to your computer and use it in GitHub Desktop.
Using JSONPatch to programatically update a package.json
var fs = require('fs')
var jsonpatch = require('fast-json-patch')
var pkg = require('./package.json')
var patch = require('./patch')(pkg)
var patches = jsonpatch.compare(pkg, patch)
// We don’t want to remove keys missing from our patch.
.filter(function (item) {
return item.op !== 'remove'
})
// The value of `pkg` is mutated.
jsonpatch.apply(pkg, patches)
// Use proper formatting.
var patched = JSON.stringify(
pkg, null, 2
)
fs.writeFile('./package.json', patched, function (err) {
console.log(err ? err.message : patched)
})
{
"name": "my-package",
"version": "0.0.6",
"dependencies": {
"fast-json-patch": "^0.5.0"
}
}
module.exports = function (og) {
return {
// Bump the patch version.
version: (function bump () {
var version = og.version.split('.').map(Number)
version[version.length - 1]++
return version.join('.')
})()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment