Skip to content

Commit

Permalink
- Further docs
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Oct 6, 2019
1 parent 90f06bb commit b84dd92
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 39 deletions.
8 changes: 7 additions & 1 deletion lib/npm-check-updates.js
Expand Up @@ -304,8 +304,14 @@ function initOptions(options) {
});
}

/**
* @typedef {Array} PkgInfo
* @property 0 pkgFile
* @property 1 pkgData
*/

/** Finds the package file and data.
@returns Promise [pkgFile, pkgData]
@returns Promise<PkgInfo>
Searches as follows:
--packageData flag
Expand Down
23 changes: 23 additions & 0 deletions lib/package-managers/bower.js
Expand Up @@ -4,6 +4,7 @@ const chalk = require('chalk');
const requireg = require('requireg');

/**
* @param args
* @param args.global
* @param args.registry
* @param args.loglevel
Expand All @@ -24,6 +25,12 @@ const bower = ({loglevel}) => {

module.exports = {

/**
* @param {Object} [info]
* @param {string} info.prefix
* @param {string} info.loglevel
* @returns {Promise<Object>}
*/
list({prefix, loglevel} = {}) {

return new Promise((resolve, reject) => {
Expand All @@ -37,6 +44,14 @@ module.exports = {
});
},

/**
* @param {string} packageName
* @param _
* @param {Object} [info]
* @param {string} info.prefix
* @param {string} info.loglevel
* @returns {Promise}
*/
latest(packageName, _, {prefix, loglevel} = {}) {

return new Promise((resolve, reject) => {
Expand All @@ -51,6 +66,14 @@ module.exports = {
});
},

/**
* @param {string} packageName
* @param _
* @param {Object} [info]
* @param {string} info.prefix
* @param {string} info.loglevel
* @returns {Promise}
*/
greatest(packageName, _, {prefix, loglevel} = {}) {

return new Promise((resolve, reject) => {
Expand Down
67 changes: 57 additions & 10 deletions lib/package-managers/npm.js
Expand Up @@ -19,9 +19,17 @@ require('libnpmconfig').read().forEach((value, key) => {
});
npmConfig.cache = false;

/** Parse JSON and throw an informative error on failure.
/**
* @typedef {Object} CommandAndPackageName
* @property {string} command
* @property {string} packageName
*/

/**
* Parse JSON and throw an informative error on failure.
* @param result Data to be parsed
* @param data { command, packageName }
* @param {CommandAndPackageName} data
* @returns {Object}
*/
function parseJson(result, data) {
let json;
Expand All @@ -35,9 +43,10 @@ function parseJson(result, data) {
}

/**
* @param packageName Name of the package
* @param field Field such as "versions" or "dist-tags.latest" are parsed from the pacote result (https://www.npmjs.com/package/pacote#packument)
* @returns Promised result
* @param {string} packageName Name of the package
* @param {string} field Field such as "versions" or "dist-tags.latest" are parsed from the pacote result (https://www.npmjs.com/package/pacote#packument)
* @param {string} currentVersion
* @returns {Promise} Promised result
*/
function view(packageName, field, currentVersion) {
if (currentVersion && (!semver.validRange(currentVersion) || versionUtil.isWildCard(currentVersion))) {
Expand All @@ -61,8 +70,8 @@ function view(packageName, field, currentVersion) {
}

/**
* @param versions Array of all available versions
* @returns An array of versions with the release versions filtered out
* @param {Array} versions Array of all available versions
* @returns {Array} An array of versions with the release versions filtered out
*/
function filterOutPrereleaseVersions(versions) {
return _.filter(versions, _.negate(isPre));
Expand All @@ -77,7 +86,13 @@ function isPre(version) {
}


/** Spawn npm requires a different command on Windows. */
/**
* Spawn npm requires a different command on Windows.
* @param args
* @param {Object} [npmOptions={}]
* @param {Object} [spawnOptions={}]
* @returns {Promise<string>}
*/
function spawnNpm(args, npmOptions={}, spawnOptions={}) {
const cmd = process.platform === 'win32'? 'npm.cmd' : 'npm';

Expand All @@ -92,8 +107,10 @@ function spawnNpm(args, npmOptions={}, spawnOptions={}) {
}

/** Get platform-specific default prefix to pass on to npm.
* @param options.global
* @param options.prefix
* @param {Object} options
* @param {boolean} [options.global]
* @param [options.prefix]
* @returns {Promise<string>}
*/
function defaultPrefix(options) {

Expand Down Expand Up @@ -139,6 +156,12 @@ module.exports = {
});
},

/**
* @param {string} packageName
* @param {string} currentVersion
* @param {boolean} pre
* @returns {Promise}
*/
latest(packageName, currentVersion, pre) {
return view(packageName, 'dist-tags.latest', currentVersion)
.then(version => {
Expand All @@ -156,6 +179,12 @@ module.exports = {
});
},

/**
* @param {string} packageName
* @param {string} currentVersion
* @param {boolean} pre
* @returns {Promise}
*/
newest(packageName, currentVersion, pre) {
return view(packageName, 'time', currentVersion)
.then(_.keys)
Expand All @@ -165,20 +194,38 @@ module.exports = {
});
},

/**
* @param {string} packageName
* @param {string} currentVersion
* @param {boolean} pre
* @returns {Promise}
*/
greatest(packageName, currentVersion, pre) {
return view(packageName, 'versions', currentVersion)
.then(versions => {
return _.last(pre ? versions : filterOutPrereleaseVersions(versions));
});
},

/**
* @param {string} packageName
* @param {string} currentVersion
* @param {boolean} pre
* @returns {Promise}
*/
greatestMajor(packageName, currentVersion, pre) {
return view(packageName, 'versions', currentVersion).then(versions => {
const resultVersions = pre ? versions : filterOutPrereleaseVersions(versions);
return versionUtil.findGreatestByLevel(resultVersions, currentVersion, 'major');
});
},

/**
* @param {string} packageName
* @param {string} currentVersion
* @param {boolean} pre
* @returns {Promise}
*/
greatestMinor(packageName, currentVersion, pre) {
return view(packageName, 'versions', currentVersion).then(versions => {
const resultVersions = pre ? versions : filterOutPrereleaseVersions(versions);
Expand Down
2 changes: 2 additions & 0 deletions lib/raw-promisify.js
Expand Up @@ -5,6 +5,8 @@ const _ = require('lodash');
* For some reason, Promise.promisifyAll does not work on npm.commands :(
* Promise.promisifyAll(npm.commands);
* So we have to do it manually.
* @param {Object} obj
* @returns {void}
*/
function rawPromisify(obj) {
_.each(obj, (method, name) => {
Expand Down
47 changes: 39 additions & 8 deletions lib/version-util.js
Expand Up @@ -22,7 +22,8 @@ const WILDCARD_PURE_REGEX = new RegExp(`^(${WILDCARDS_PURE.join('|')
const SEMANTIC_DIRECT = new RegExp('^\\d+\\.\\d+\\.\\d+([-|+].*)*$');

/**
* Returns the number of parts in the version
* @param {string} version
* @returns {number} The number of parts in the version
*/
function numParts(version) {

Expand All @@ -37,6 +38,9 @@ function numParts(version) {

/**
* Increases or decreases the given precision by the given amount, e.g. major+1 -> minor
* @param {string} precision
* @param {number} n
* @returns {string}
*/
function precisionAdd(precision, n) {

Expand All @@ -58,8 +62,13 @@ function precisionAdd(precision, n) {
return VERSION_PARTS[index];
}

/** Joins the major, minor, patch, release, and build parts (controlled by an optional precision arg) of a semver object
* into a dot-delimited string. */
/**
* Joins the major, minor, patch, release, and build parts (controlled by an
* optional precision arg) of a semver object into a dot-delimited string.
* @param semver
* @param {string} [precision]
* @returns {string}
*/
function stringify(semver, precision) {

// get a list of the parts up until (and including) the given precision
Expand All @@ -79,6 +88,8 @@ function stringify(semver, precision) {

/**
* Gets how precise this version number is (major, minor, patch, release, or build)
* @param {string} version
* @returns {string}
*/
function getPrecision(version) {
const semver = semverutils.parseRange(version)[0];
Expand All @@ -88,32 +99,52 @@ function getPrecision(version) {

/**
* Sets the precision of a (loose) semver to the specified level: major, minor, etc.
* @param {string} version
* @param {string} [precision]
* @returns {string}
*/
function setPrecision(version, precision) {
const semver = semverutils.parseRange(version)[0];
return stringify(semver, precision);
}

/** Adds a given wildcard (^,~,.*,.x) to a version number. Adds ^ and ~ to the beginning. Replaces everything after the
* major version number with .* or .x */
/**
* Adds a given wildcard (^,~,.*,.x) to a version number. Adds ^ and ~ to the
* beginning. Replaces everything after the major version number with .* or .x
* @param {string} version
* @param {string} wildcard
* @returns {string}
*/
function addWildCard(version, wildcard) {
return wildcard === '^' || wildcard === '~' ?
wildcard + version :
setPrecision(version, 'major') + wildcard;
}

/** Returns true if the given string is one of the wild cards. */
/**
* Returns true if the given string is one of the wild cards.
* @param {string} version
* @returns {boolean}
*/
function isWildCard(version) {
return WILDCARD_PURE_REGEX.test(version);
}

/** Returns true if the given digit is a wildcard for a part of a version. */
/**
* Returns true if the given digit is a wildcard for a part of a version.
* @param {"*"|"x"} versionPart
* @returns {boolean}
*/
function isWildPart(versionPart) {
return versionPart === '*' || versionPart === 'x';
}

/**
* Colorize the parts of a version string (to) that are different than another (from). Assumes that the two verson strings are in the same format.
* Colorize the parts of a version string (to) that are different than
* another (from). Assumes that the two verson strings are in the same format.
* @param from
* @param to
* @returns {string}
*/
function colorizeDiff(from, to) {
let leadingWildcard = '';
Expand Down

0 comments on commit b84dd92

Please sign in to comment.