Last active
November 14, 2018 17:42
-
-
Save bspaulding/1bbb2b9c5400bd8e23e466fae3ccd2e5 to your computer and use it in GitHub Desktop.
Convert dependencies in package.json to the exact versions currently installed.
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
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