Last active
August 29, 2015 14:13
-
-
Save michaelrhodes/42d480539c5e06914e45 to your computer and use it in GitHub Desktop.
Using JSONPatch to programatically update a package.json
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
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) | |
}) |
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
{ | |
"name": "my-package", | |
"version": "0.0.6", | |
"dependencies": { | |
"fast-json-patch": "^0.5.0" | |
} | |
} |
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
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