From ad2cf7b801290baf2c6a6e3cde15727753b63aca Mon Sep 17 00:00:00 2001 From: xpure-sklatt <33316354+xpure-sklatt@users.noreply.github.com> Date: Mon, 6 Nov 2017 15:37:05 +0100 Subject: [PATCH] feat(registry): support custom registry and timeout (#117) * feat(registry): provide new option '--registry' to set a custom registry url #116 * feat(registry): fix passing options correctly into fetchVersions #116 * feat(check-version-timeout): define custom timeout for version check #116 * feat(registry/check-version-timeout): clean up code #116 * feat(registry/check-version-timeout): reset version to 0.0.0 #116 --- .gitignore | 3 +++ bin/next-update.js | 4 +++- src/cli-options.js | 10 ++++++++++ src/registry.js | 13 ++++++------- src/test/registry.js | 12 ++++++------ 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 1cb277e..08b3c0c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ /cover /test/cover /src/test/npm-debug.log +.idea/ +*.eml +*.iml .DS_Store node_modules/ npm-debug.log diff --git a/bin/next-update.js b/bin/next-update.js index 5b55666..a24c591 100755 --- a/bin/next-update.js +++ b/bin/next-update.js @@ -71,7 +71,9 @@ if (program.available) { allow: allow, type: program.type, tldr: program.tldr, - without: program.without + without: program.without, + registry: program.registry, + checkVersionTimeout: program['check-version-timeout'] || 10000 } var checkCurrent = nextUpdate.checkCurrentInstall.bind(null, opts) diff --git a/src/cli-options.js b/src/cli-options.js index e65e7d2..a256c15 100644 --- a/src/cli-options.js +++ b/src/cli-options.js @@ -97,6 +97,16 @@ var program = optimist alias: 'L', description: 'print commit changes between working versions' }) +.options('registry', { + string: true, + default: false, + description: 'use a custom registry url' +}) +.options('check-version-timeout', { + number: true, + default: 10000, + description: 'define a custom timeout value for checking next versions' +}) .usage(info) .argv diff --git a/src/registry.js b/src/registry.js index 8ba95f1..98fae75 100644 --- a/src/registry.js +++ b/src/registry.js @@ -145,7 +145,7 @@ function isNotFound (str) { // fetching versions inspired by // https://github.com/jprichardson/npm-latest // returns a promise -function fetchVersions (nameVersion) { +function fetchVersions (options, nameVersion) { // console.log(nameVersion); // TODO use check.schema check.verify.object(nameVersion, 'expected name, version object') @@ -161,7 +161,7 @@ function fetchVersions (nameVersion) { } // console.log('fetching versions for', name, 'current version', version); - var MAX_WAIT_TIMEOUT = 25000 + var MAX_WAIT_TIMEOUT = options.checkVersionTimeout || 25000 var deferred = q.defer() function rejectOnTimeout () { @@ -180,7 +180,7 @@ function fetchVersions (nameVersion) { la(check.webUrl(npmUrl), 'need npm registry url, got', npmUrl) npmUrl = npmUrl.replace(/^https:/, 'http:').trim() - var url = npmUrl + escapeName(name) + var url = (options.registry || npmUrl) + escapeName(name) // TODO how to detect if the registry is not responding? @@ -310,14 +310,13 @@ function nextVersions (options, nameVersionPairs, checkLatestOnly) { check.verify.array(nameVersionPairs, 'expected array') nameVersionPairs = cleanVersions(nameVersionPairs) - var MAX_CHECK_TIMEOUT = 10000 - const verbose = verboseLog(options) verbose('checking NPM registry') + var MAX_CHECK_TIMEOUT = options.checkVersionTimeout || 10000 - var fetchPromises = nameVersionPairs.map(fetchVersions) + var fetchPromises = nameVersionPairs.map(fetchVersions.bind(null, options)) var fetchAllPromise = q.all(fetchPromises) - .timeout(MAX_CHECK_TIMEOUT, 'timed out waiting for NPM') + .timeout(MAX_CHECK_TIMEOUT, 'timed out waiting for NPM after ' + MAX_CHECK_TIMEOUT + 'ms') return fetchAllPromise.then( _.partial(filterFetchedVersions, checkLatestOnly), diff --git a/src/test/registry.js b/src/test/registry.js index 50244f9..a9122b6 100644 --- a/src/test/registry.js +++ b/src/test/registry.js @@ -12,11 +12,11 @@ function onError (error) { gt.test('basic of fetch', function () { gt.func(fetchVersions) - gt.arity(fetchVersions, 1) + gt.arity(fetchVersions, 2) }) gt.async('fetch non existent module', 2, function () { - var promise = fetchVersions({ + var promise = fetchVersions({}, { name: 'this-module-should-not-exist-at-all', version: '0.2.0' }) @@ -31,8 +31,8 @@ gt.async('fetch non existent module', 2, function () { gt.async('fetch gt later versions', function () { gt.func(fetchVersions) - gt.arity(fetchVersions, 1) - var promise = fetchVersions({ name: 'gt', version: '0.5.0' }) + gt.arity(fetchVersions, 2) + var promise = fetchVersions({}, { name: 'gt', version: '0.5.0' }) gt.func(promise.then, 'return object has then method') promise.then(function (results) { gt.object(results, 'returns an object') @@ -44,8 +44,8 @@ gt.async('fetch gt later versions', function () { gt.async('fetch module later versions', function () { gt.func(fetchVersions) - gt.arity(fetchVersions, 1) - var promise = fetchVersions({ name: 'lodash', version: '0.7.0' }) + gt.arity(fetchVersions, 2) + var promise = fetchVersions({}, { name: 'lodash', version: '0.7.0' }) gt.func(promise.then, 'return object has then method') promise.then(function (results) { gt.object(results, 'returns an object')