Skip to content

Instantly share code, notes, and snippets.

@ChrisCinelli
Created September 2, 2016 18:10
Show Gist options
  • Save ChrisCinelli/be634ecc2f69026fb3ea18ac44341539 to your computer and use it in GitHub Desktop.
Save ChrisCinelli/be634ecc2f69026fb3ea18ac44341539 to your computer and use it in GitHub Desktop.
// Convert the package versions in package.json to the version of the installed packages
//
// cd your-package
// node fix-package-json.js > package.json
var fs = require("fs");
var contents = fs.readFileSync("package.json");
var json = JSON.parse(contents);
// Get current installed versions
var spawn = require( 'child_process' ).spawnSync,
versionText = spawn( 'npm', [ 'ls', ' --depth=0' ] );
// If you want to use the output of another person
/*
versionText = `
website@0.0.1 /Users/itsme/Sites/website
├── ava@0.15.2
├── babel-core@6.14.0
├── babel-eslint@6.1.2
├── babel-loader@6.2.5
├── babel-plugin-webpack-loaders@0.7.1
├── babel-polyfill@6.13.0
├── babel-preset-es2015@6.14.0
├── babel-preset-es2015-native-modules@6.9.4
├── babel-preset-react@6.11.1
├── babel-preset-react-optimize@1.0.1
├── babel-preset-stage-0@6.5.0
├── babel-register@6.14.0
├── body-parser@1.15.2
├── webpack-dev-middleware@1.6.1
├── webpack-dev-server@2.1.0-beta.2
├── webpack-externals-plugin@1.0.0
├── webpack-hot-middleware@2.12.2
└── webpack-manifest-plugin@1.0.1
`;
*/
// Create fixed map
var versions = {}
versionText.split("\n").map((line) => {
var ar = line.split('@');
var key = ar[0].split(' '); key = key[key.length - 1];
versions[key] = ar[1];
});
// Fix Dependencies
for (var key in json.dependencies){
if(versions[key]){
json.dependencies[key] = versions[key];
}
}
for (key in json.devDependencies){
if(versions[key]){
json.devDependencies[key] = versions[key];
}
}
console.log(JSON.stringify(json, null, ' '));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment