Skip to content

Instantly share code, notes, and snippets.

@mikechau
Created January 30, 2017 22:31
Show Gist options
  • Save mikechau/eca874100025d040aff77b7ffaec9a3f to your computer and use it in GitHub Desktop.
Save mikechau/eca874100025d040aff77b7ffaec9a3f to your computer and use it in GitHub Desktop.
// scripts/reconfig.js
//
// Description:
// Pass in a key as an argument with a value and it will merge the change
// into the package.json.
//
// Usage:
// ./reconfig.js --$KEY_1_NAME $KEY_1_VALUE --$KEY_2_NAME --$KEY_2_VALUE
// ./reconfig.js --homepage http://custom-homepage.com
//
var path = require('path');
var fs = require('fs');
var argv = require('minimist')(process.argv.slice(2));
var packageJson = require('../package');
var overrides = Object.assign({}, argv);
delete overrides._;
var updatedKeys = Object.keys(overrides);
if (!updatedKeys.length) {
console.log('No keys in package.json to update. Exiting...');
process.exit();
}
var newPackageJson = Object.assign({}, packageJson, overrides);
console.log('Updating package.json keys:', Object.keys(overrides));
console.log('');
fs.writeFileSync(
path.join(__dirname, '../package.json'),
JSON.stringify(newPackageJson, null, 2)
);
console.log('Updated package.json result:', newPackageJson);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment