-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modularise code, add batch to npm version info requests
- Loading branch information
Showing
5 changed files
with
213 additions
and
146 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
function Batch () { | ||
if (!(this instanceof Batch)) return new Batch() | ||
this._batch = {} | ||
} | ||
|
||
/** | ||
* Create or add a callback function to the end of a batch for a given key. | ||
* @param key | ||
* @param cb | ||
*/ | ||
Batch.prototype.push = function (key, cb) { | ||
this._batch[key] = this._batch[key] || [] | ||
this._batch[key].push(cb) | ||
} | ||
|
||
/** | ||
* Determine if there is a batch for the given key. | ||
* @param {String} key | ||
* @returns {boolean} | ||
*/ | ||
Batch.prototype.exists = function (key) { | ||
return this._batch[key] != null | ||
} | ||
|
||
/** | ||
* Call all the batched callback functions for a key and remove them. | ||
* @param {String} key ID of the batch operation | ||
* @param {Function} fn Function to call for each | ||
*/ | ||
Batch.prototype.call = function (key, cb) { | ||
var cbs = this._batch[key] | ||
this._batch[key] = null | ||
cbs.forEach(cb) | ||
} | ||
|
||
module.exports = Batch |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
var npm = require('npm') | ||
var Batch = require('./batch') | ||
|
||
var batch = new Batch() | ||
|
||
/** | ||
* Get info about the versions for this dependency. Returns an object with | ||
* `current` and `versions` properties. Where `current` is the version you'd | ||
* get when you `npm install [package]` and `versions` is a sorted array of | ||
* available versions for the dependency. | ||
* @param {String} name Dependency name | ||
* @param {Object} opts Options | ||
* @param {Object} [opts.npm] npm configuration options | ||
*/ | ||
function getVersionsInfo (name, opts, cb) { | ||
if (batch.exists(name)) { | ||
return batch.push(name, cb) | ||
} | ||
|
||
batch.push(name, cb) | ||
|
||
npm.load(opts.npm || {}, function (err) { | ||
if (err) return batch.call(name, function (cb) { cb(err) }) | ||
|
||
npm.commands.view([name, 'versions', 'time'], true, function (err, data) { | ||
if (err) return batch.call(name, function (cb) { cb(err) }) | ||
|
||
var currentVersion = Object.keys(data)[0] | ||
var versions = null | ||
|
||
// `npm view 0 versions` returns {} | ||
if (!currentVersion) { | ||
return batch.call(name, function (cb) { | ||
cb(new Error('Failed to get versions for ' + name)) | ||
}) | ||
} | ||
|
||
// Some packages simply don't have a time object | ||
if (data[currentVersion].time) { | ||
versions = data[currentVersion].versions.sort(function (a, b) { | ||
a = data[currentVersion].time[a] | ||
b = data[currentVersion].time[b] | ||
return (a < b ? -1 : (a > b ? 1 : 0)) | ||
}) | ||
} else { | ||
versions = data[currentVersion].versions | ||
} | ||
|
||
batch.call(name, function (cb) { | ||
cb(null, { current: currentVersion, versions: versions }) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
exports.getVersionsInfo = getVersionsInfo |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
var semver = require('semver') | ||
|
||
var unstablePattern = /[a-z+-]/i | ||
|
||
/** | ||
* Determine if a version is a stable version or not. | ||
* @param {String} version | ||
* @return {Boolean} | ||
*/ | ||
function isStable (version) { | ||
return !unstablePattern.test(version || '') | ||
} | ||
|
||
exports.isStable = isStable | ||
|
||
/** | ||
* Determine if a version is a SCM URL or not. | ||
* @param {String} version | ||
* @return {Boolean} | ||
*/ | ||
function isScm (version) { | ||
var scmPrefixes = ['git:', 'git+ssh:', 'https:', 'git+https:'] | ||
var blacklisted = scmPrefixes.filter(function (prefix) { | ||
return version.indexOf(prefix) === 0 | ||
}) | ||
return !!blacklisted.length | ||
} | ||
|
||
exports.isScm = isScm | ||
|
||
/** | ||
* Get the latest stable version from a list of versions in ascending order. | ||
* @param {Array} versions | ||
* @return {String} | ||
*/ | ||
function getLatestStable (versions) { | ||
versions = versions.slice() | ||
while (versions.length) { | ||
var version = versions.pop() | ||
if (isStable(version)) { | ||
return version | ||
} | ||
} | ||
return null | ||
} | ||
|
||
exports.getLatestStable = getLatestStable | ||
|
||
/** | ||
* Get the latest version and latest stable version | ||
* @param {String} current The version you get when you `npm install [package]` | ||
* @param {Array} versions All versions available | ||
* @param {Object} opts Options | ||
* @param {Boolean} [opts.loose] Use loose option when querying semver | ||
*/ | ||
function getLatest (current, versions, opts) { | ||
var stable = current | ||
var latest = versions[versions.length - 1] | ||
|
||
if (!isStable(stable)) { | ||
stable = getLatestStable(versions) | ||
} | ||
|
||
// getLatestStable might not have found a stable version | ||
if (stable) { | ||
// Latest is the most recent version with higher version than stable | ||
for (var i = versions.length - 1; i >= 0; i--) { | ||
// If !opts.loose then this may throw | ||
if (semver.gt(versions[i], stable, opts.loose)) { | ||
latest = versions[i] | ||
break | ||
} | ||
} | ||
} | ||
|
||
return { latest: latest, stable: stable } | ||
} | ||
|
||
exports.getLatest = getLatest | ||
|
||
function getVersionsInRange (range, versions, loose) { | ||
return versions.filter(function (v) { | ||
return semver.satisfies(v, range, loose) | ||
}) | ||
} | ||
|
||
exports.getVersionsInRange = getVersionsInRange |
Oops, something went wrong.