Created
January 10, 2017 12:22
-
-
Save insin/9a3d1f2975072e986f8d91c1604fccae to your computer and use it in GitHub Desktop.
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
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