Last active
December 5, 2018 17:05
-
-
Save Dkendal/06af1eeda2a0216ad29ca656f3a9794c to your computer and use it in GitHub Desktop.
Node js script to set exact dependencies based on what is installed in node_modules.
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
'use strict'; | |
const fs = require('fs'); | |
const pwd = process.cwd(); | |
const pkg = require(`${pwd}/package.json`); | |
for (const key of [ | |
'devDependencies', | |
'dependencies', | |
'peerDependencies', | |
'optionalDependencies', | |
]) { | |
const entry = pkg[key]; | |
if (!entry) { | |
continue; | |
} | |
for (const [dep, _version] of Object.entries(entry)) { | |
const localpkg = fs.readFileSync( | |
`${pwd}/node_modules/${dep}/package.json`, | |
{ | |
encoding: 'utf8', | |
} | |
); | |
const version = JSON.parse(localpkg).version; | |
pkg[key][dep] = version; | |
} | |
} | |
console.log(JSON.stringify(pkg, null, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment