Skip to content

Instantly share code, notes, and snippets.

@bspaulding
Last active November 14, 2018 17:42
Show Gist options
  • Save bspaulding/1bbb2b9c5400bd8e23e466fae3ccd2e5 to your computer and use it in GitHub Desktop.
Save bspaulding/1bbb2b9c5400bd8e23e466fae3ccd2e5 to your computer and use it in GitHub Desktop.
Convert dependencies in package.json to the exact versions currently installed.
const fs = require('fs');
const prettier = require('prettier');
const package = JSON.parse(fs.readFileSync('package.json', 'UTF-8'));
function updateDeps(deps) {
let newDeps = {};
for (let name in deps) {
const version = deps[name];
if(!version.match(/^([0-9]|file:|git)/)) {
const localPackage = JSON.parse(fs.readFileSync(`./node_modules/${name}/package.json`));
console.log(`${name} is set to non-exact version: ${version}, installed ${localPackage.version}`);
newDeps[name] = localPackage.version;
}
}
return newDeps;
}
package.devDependencies = {
...package.devDependencies,
...updateDeps(package.devDependencies)
};
package.dependencies = {
...package.dependencies,
...updateDeps(package.dependencies)
};
fs.writeFileSync('package.json', prettier.format(JSON.stringify(package), {
parser: "json",
useTabs: true
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment