Skip to content

Commit

Permalink
- refactor: Use more ES6 features (array destructuring, rest params, …
Browse files Browse the repository at this point in the history
…`includes`, array extras, Object values/entries); `concat` args

- Add recommended "json" extension to `eslintrc`
  • Loading branch information
brettz9 committed Oct 6, 2019
1 parent b7b60cb commit b394a94
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 41 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion bin/ncu
Expand Up @@ -66,7 +66,7 @@ const rcArguments = rcFile && rcFile.config ?
value === true ? [`--${name}`] : [`--${name}`, value]
)) : [];

const combinedArguments = process.argv.slice(0,2).concat(rcArguments).concat(process.argv.slice(2));
const combinedArguments = process.argv.slice(0,2).concat(rcArguments, process.argv.slice(2));

program.parse(combinedArguments);

Expand Down
4 changes: 2 additions & 2 deletions lib/npm-check-updates.js
Expand Up @@ -289,7 +289,7 @@ function printUpgrades(options, {current, upgraded, numUpgraded, total}) {
/** Initializes and consolidates options from the cli. */
function initOptions(options) {

return _.assign({}, options, {
return Object.assign({}, options, {
filter: options.args.join(' ') || options.filter,
// convert silent option to loglevel silent
loglevel: options.silent ? 'silent' : options.loglevel,
Expand Down Expand Up @@ -433,6 +433,6 @@ async function run(options={}) {
return await Promise.race([timeoutPromise, getAnalysis()]);
}

module.exports = _.assign({
module.exports = Object.assign({
run
}, vm);
6 changes: 3 additions & 3 deletions lib/package-managers/npm.js
Expand Up @@ -48,9 +48,9 @@ function view(packageName, field, currentVersion) {

return pacote.packument(packageName, npmConfig).then(result => {
if (field.startsWith('dist-tags.')) {
const split = field.split('.');
if (result[split[0]]) {
return result[split[0]][split[1]];
const [tagName, version] = field.split('.');
if (result[tagName]) {
return result[tagName][version];
}
} else if (field === 'versions') {
return Object.keys(result[field]);
Expand Down
9 changes: 3 additions & 6 deletions lib/raw-promisify.js
@@ -1,16 +1,13 @@
'use strict';
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.
*/
function rawPromisify(obj) {
_.each(obj, (method, name) => {
obj[`${name}Async`] = () => {
const args = [].slice.call(arguments);
const that = this;
Object.entries(obj).forEach(([name, method]) => {
obj[`${name}Async`] = (...args) => {
return new Promise((resolve, reject) => {
args.push((err, results) => {
if (err) {
Expand All @@ -19,7 +16,7 @@ function rawPromisify(obj) {
resolve(results);
}
});
return method.apply(that, args);
return method.apply(this, args);
});
};
});
Expand Down
16 changes: 8 additions & 8 deletions lib/version-util.js
Expand Up @@ -26,7 +26,7 @@ const SEMANTIC_DIRECT = new RegExp('^\\d+\\.\\d+\\.\\d+([-|+].*)*$');
*/
function numParts(version) {

const semver = semverutils.parseRange(version)[0];
const [semver] = semverutils.parseRange(version);

if (!semver) {
throw new Error(util.format('semverutils.parseRange returned null when trying to parse "%s". This is probably a problem with the "semver-utils" dependency. Please report an issue at https://github.com/tjunnone/npm-check-updates/issues.', version));
Expand All @@ -45,8 +45,8 @@ function precisionAdd(precision, n) {
}

const index = n === 0 ? precision :
_.includes(VERSION_BASE_PARTS, precision) ? VERSION_BASE_PARTS.indexOf(precision) + n :
_.includes(VERSION_ADDED_PARTS, precision) ? VERSION_BASE_PARTS.length + n :
VERSION_BASE_PARTS.includes(precision) ? VERSION_BASE_PARTS.indexOf(precision) + n :
VERSION_ADDED_PARTS.includes(precision) ? VERSION_BASE_PARTS.length + n :
null;

if (index === null) {
Expand All @@ -69,7 +69,7 @@ function stringify(semver, precision) {
// pair each part with its delimiter and join together
return parts
.filter(part => {
return _.includes(VERSION_BASE_PARTS, precision) || semver[part];
return VERSION_BASE_PARTS.includes(precision) || semver[part];
})
.map(part => {
return VERSION_PART_DELIM[part] + (semver[part] || '0');
Expand All @@ -81,16 +81,16 @@ function stringify(semver, precision) {
* Gets how precise this version number is (major, minor, patch, release, or build)
*/
function getPrecision(version) {
const semver = semverutils.parseRange(version)[0];
const [semver] = semverutils.parseRange(version);
// expects VERSION_PARTS to be in correct order
return _.find(VERSION_PARTS.slice().reverse(), _.propertyOf(semver));
return VERSION_PARTS.slice().reverse().find(_.propertyOf(semver));
}

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

Expand Down Expand Up @@ -129,7 +129,7 @@ function colorizeDiff(from, to) {
const partsToColor = to.split('.');
const partsToCompare = from.split('.');

let i = _.findIndex(partsToColor, (part, i) => part !== partsToCompare[i]);
let i = partsToColor.findIndex((part, i) => part !== partsToCompare[i]);
i = i >= 0 ? i : partsToColor.length;

// major = red (or any change before 1.0.0)
Expand Down
41 changes: 20 additions & 21 deletions lib/versionmanager.js
Expand Up @@ -18,8 +18,8 @@ function v(str) {

/** Returns a new function that AND's the two functions over the provided arguments. */
function and(f, g) {
return function () {
return f.apply(this, arguments) && g.apply(this, arguments);
return function (...args) {
return f.apply(this, args) && g.apply(this, args);
};
}

Expand All @@ -35,7 +35,7 @@ function upgradeDependencyDeclaration(declaration, latestVersion, options = {})

// parse the latestVersion
// return original declaration if latestSemver is invalid
const latestSemver = semverutils.parseRange(latestVersion)[0];
const [latestSemver] = semverutils.parseRange(latestVersion);
if (!latestSemver) {
return declaration;
}
Expand All @@ -55,7 +55,7 @@ function upgradeDependencyDeclaration(declaration, latestVersion, options = {})
.reject({operator: '-'})
.sortBy(_.ary(_.flow(versionUtil.stringify, versionUtil.numParts), 1))
.value();
const declaredSemver = parsedRange[0];
const [declaredSemver] = parsedRange;

/**
* Chooses version parts between the declared version and the latest.
Expand All @@ -65,8 +65,8 @@ function upgradeDependencyDeclaration(declaration, latestVersion, options = {})
*/
function chooseVersion(part) {
return versionUtil.isWildPart(declaredSemver[part]) ? declaredSemver[part] :
_.includes(versionUtil.VERSION_BASE_PARTS, part) && declaredSemver[part] ? latestSemver[part] :
_.includes(versionUtil.VERSION_ADDED_PARTS, part) ? latestSemver[part] :
versionUtil.VERSION_BASE_PARTS.includes(part) && declaredSemver[part] ? latestSemver[part] :
versionUtil.VERSION_ADDED_PARTS.includes(part) ? latestSemver[part] :
undefined;
}

Expand Down Expand Up @@ -150,7 +150,7 @@ function isUpgradeable(current, latest) {
}

// remove the constraint (e.g. ^1.0.1 -> 1.0.1) to allow upgrades that satisfy the range, but are out of date
const range = semverutils.parseRange(current)[0];
const [range] = semverutils.parseRange(current);
if (!range) {
throw new Error(`"${current}" could not be parsed by semver-utils. This is probably a bug. Please file an issue at https://github.com/tjunnone/npm-check-updates.`);
}
Expand Down Expand Up @@ -182,11 +182,11 @@ function packageNameFilter(filter) {
} else {
// string filter
const packages = filter.split(/[\s,]+/);
filterPackages = _.includes.bind(_, packages);
filterPackages = packages.includes.bind(packages);
}
} else if (Array.isArray(filter)) {
// array filter
filterPackages = _.includes.bind(_, filter);
filterPackages = filter.includes.bind(filter);
} else if (filter instanceof RegExp) {
// raw RegExp
filterPackages = filter.test.bind(filter);
Expand Down Expand Up @@ -286,11 +286,11 @@ function getCurrentDependencies(pkgData = {}, options = {}) {

if (options.dep) {
const deps = (options.dep || '').split(',');
options.prod = _.includes(deps, 'prod');
options.dev = _.includes(deps, 'dev');
options.peer = _.includes(deps, 'peer');
options.optional = _.includes(deps, 'optional');
options.bundle = _.includes(deps, 'bundle');
options.prod = deps.includes('prod');
options.dev = deps.includes('dev');
options.peer = deps.includes('peer');
options.optional = deps.includes('optional');
options.bundle = deps.includes('bundle');
} else {
options.prod = options.dev = options.peer = options.optional = options.bundle = true;
}
Expand Down Expand Up @@ -417,9 +417,9 @@ function getPreferredWildcard(dependencies) {
}

// group the dependencies by wildcard
const groups = _.groupBy(_.values(dependencies), dep =>
_.find(versionUtil.WILDCARDS, wildcard =>
dep && dep.indexOf(wildcard) > -1
const groups = _.groupBy(Object.values(dependencies), dep =>
versionUtil.WILDCARDS.find(wildcard =>
dep && dep.includes(wildcard)
)
);

Expand All @@ -438,10 +438,9 @@ function getPreferredWildcard(dependencies) {
}

function getVersionTarget(options) {
return options.semverLevel ? options.semverLevel :
options.newest ? 'newest' :
options.greatest ? 'greatest' :
'latest';
return options.semverLevel || (options.newest ? 'newest' :
options.greatest ? 'greatest' :
'latest');
}

/**
Expand Down

0 comments on commit b394a94

Please sign in to comment.