Skip to content

Instantly share code, notes, and snippets.

@insin
Created January 10, 2017 12:22
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 insin/9a3d1f2975072e986f8d91c1604fccae to your computer and use it in GitHub Desktop.
Save insin/9a3d1f2975072e986f8d91c1604fccae to your computer and use it in GitHub Desktop.
import spawn from 'cross-spawn'
import ora from 'ora'
/**
* Get the latest version of a package from npm.
*/
export function getLatestVersion(pkg, cb) {
let spinner = ora(`Checking for latest version of ${pkg}`).start()
let npm = spawn('npm', ['dist-tag', 'ls', pkg, '--no-progress'], {stdio: ['ignore', 'pipe', 'inherit']})
let stdout = ''
npm.stdout.on('data', (data) => { stdout += data })
npm.on('exit', (code) => {
if (code !== 0) {
spinner.fail()
return cb(new Error('Error running npm dist-tag ls'))
}
let match = /^latest: ([^\n]+)/m.exec(stdout.toString('utf-8'))
if (!match) {
spinner.fail()
return cb(new Error('Could not find latest version in dist-tag results'))
}
spinner.succeed()
cb(match[1])
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment