Skip to content

Instantly share code, notes, and snippets.

@jeswin
Forked from kentcdodds/package-exact.js
Last active December 9, 2020 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeswin/f072969bf7102bea5e44e1d64a4b0bef to your computer and use it in GitHub Desktop.
Save jeswin/f072969bf7102bea5e44e1d64a4b0bef to your computer and use it in GitHub Desktop.
make your package.json dependencies be the exact version that's in your node_modules currently
/* jshint node:true */
/* eslint-env node */
/*
* This will look at the current version of all of your dependencies and update your package.json
* with the specific version that you currently have in node_modules. This will save you from the
* sadness that is: DEPENDENCY MANAGEMENT
*
* Place this file in a folder that's a a sibling to your package.json and node_modules
* Then simply run: node scripts/package-strict
* (replace "scripts" with the name of the folder you placed this stuff in)
*
* When you're ready to update dependencies, I recommend https://github.com/bahmutov/next-update
*/
var fs = require("fs");
var path = require("path");
var packageJson = require(path.join(process.cwd(), "package.json"));
strictifyDeps("dependencies");
strictifyDeps("devDependencies");
console.log("done!");
function strictifyDeps(depsProperty) {
var deps = Object.keys(packageJson[depsProperty]);
deps.forEach(function (dep) {
var depPackageJson = require(path.join(
process.cwd(),
"node_modules",
dep,
"package.json"
));
packageJson[depsProperty][dep] = depPackageJson.version;
});
fs.writeFileSync(
path.resolve(path.join(process.cwd(), "package.json")),
JSON.stringify(packageJson, null, 2)
);
}
@jeswin
Copy link
Author

jeswin commented Dec 2, 2020

Forked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment