Skip to content

Instantly share code, notes, and snippets.

@lagden
Last active January 20, 2021 14:01
Show Gist options
  • Save lagden/6f2195a2c71a0ca1edd341d458eeb800 to your computer and use it in GitHub Desktop.
Save lagden/6f2195a2c71a0ca1edd341d458eeb800 to your computer and use it in GitHub Desktop.
Update all dependencies and devDependencies to latest version in package.json
#!/usr/bin/env node
'use strict'
const {resolve} = require('path')
const {createWriteStream} = require('fs')
const {promisify} = require('util')
const child_process = require('child_process')
const exec = promisify(child_process.exec)
const packageFile = resolve(process.cwd(), 'package.json')
const packageJson = require(packageFile)
const {
dependencies,
devDependencies
} = packageJson
let cc = 0
function getLatestVersionPackage(data, prop) {
if (!data) {
return Promise.resolve('no data to show')
}
const keys = Object.keys(data)
return Promise.allSettled(keys.map(async name => {
const cmd = `npm show ${name} version`
try {
let {stdout: version} = await exec(cmd)
version = String(version).replace('\n', '')
if (version && data[name] !== String(version)) {
cc++
process.stdout.write(`${name} --> ${version}\n`)
packageJson[prop][name] = version
return {name, version}
}
} catch {}
return Promise.reject()
}))
}
Promise.all([
getLatestVersionPackage(dependencies, 'dependencies'),
getLatestVersionPackage(devDependencies, 'devDependencies')
])
.then(() => {
createWriteStream(packageFile)
.on('finish', () => {
process.stdout.write(cc > 0 ? 'All writes are now complete.' : 'No updates')
})
.on('close', () => {
process.exit(0)
})
.end(JSON.stringify(packageJson, undefined, ' '))
})
.catch(error => {
process.stderr.write(error.message)
process.exit(1)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment