Skip to content

Commit ad2cf7b

Browse files
xpure-sklattbahmutov
authored andcommitted
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
1 parent 9205cc6 commit ad2cf7b

File tree

5 files changed

+28
-14
lines changed

5 files changed

+28
-14
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
/cover
44
/test/cover
55
/src/test/npm-debug.log
6+
.idea/
7+
*.eml
8+
*.iml
69
.DS_Store
710
node_modules/
811
npm-debug.log

bin/next-update.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ if (program.available) {
7171
allow: allow,
7272
type: program.type,
7373
tldr: program.tldr,
74-
without: program.without
74+
without: program.without,
75+
registry: program.registry,
76+
checkVersionTimeout: program['check-version-timeout'] || 10000
7577
}
7678

7779
var checkCurrent = nextUpdate.checkCurrentInstall.bind(null, opts)

src/cli-options.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ var program = optimist
9797
alias: 'L',
9898
description: 'print commit changes between working versions'
9999
})
100+
.options('registry', {
101+
string: true,
102+
default: false,
103+
description: 'use a custom registry url'
104+
})
105+
.options('check-version-timeout', {
106+
number: true,
107+
default: 10000,
108+
description: 'define a custom timeout value for checking next versions'
109+
})
100110
.usage(info)
101111
.argv
102112

src/registry.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function isNotFound (str) {
145145
// fetching versions inspired by
146146
// https://github.com/jprichardson/npm-latest
147147
// returns a promise
148-
function fetchVersions (nameVersion) {
148+
function fetchVersions (options, nameVersion) {
149149
// console.log(nameVersion);
150150
// TODO use check.schema
151151
check.verify.object(nameVersion, 'expected name, version object')
@@ -161,7 +161,7 @@ function fetchVersions (nameVersion) {
161161
}
162162

163163
// console.log('fetching versions for', name, 'current version', version);
164-
var MAX_WAIT_TIMEOUT = 25000
164+
var MAX_WAIT_TIMEOUT = options.checkVersionTimeout || 25000
165165
var deferred = q.defer()
166166

167167
function rejectOnTimeout () {
@@ -180,7 +180,7 @@ function fetchVersions (nameVersion) {
180180
la(check.webUrl(npmUrl), 'need npm registry url, got', npmUrl)
181181

182182
npmUrl = npmUrl.replace(/^https:/, 'http:').trim()
183-
var url = npmUrl + escapeName(name)
183+
var url = (options.registry || npmUrl) + escapeName(name)
184184

185185
// TODO how to detect if the registry is not responding?
186186

@@ -310,14 +310,13 @@ function nextVersions (options, nameVersionPairs, checkLatestOnly) {
310310
check.verify.array(nameVersionPairs, 'expected array')
311311
nameVersionPairs = cleanVersions(nameVersionPairs)
312312

313-
var MAX_CHECK_TIMEOUT = 10000
314-
315313
const verbose = verboseLog(options)
316314
verbose('checking NPM registry')
315+
var MAX_CHECK_TIMEOUT = options.checkVersionTimeout || 10000
317316

318-
var fetchPromises = nameVersionPairs.map(fetchVersions)
317+
var fetchPromises = nameVersionPairs.map(fetchVersions.bind(null, options))
319318
var fetchAllPromise = q.all(fetchPromises)
320-
.timeout(MAX_CHECK_TIMEOUT, 'timed out waiting for NPM')
319+
.timeout(MAX_CHECK_TIMEOUT, 'timed out waiting for NPM after ' + MAX_CHECK_TIMEOUT + 'ms')
321320

322321
return fetchAllPromise.then(
323322
_.partial(filterFetchedVersions, checkLatestOnly),

src/test/registry.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ function onError (error) {
1212

1313
gt.test('basic of fetch', function () {
1414
gt.func(fetchVersions)
15-
gt.arity(fetchVersions, 1)
15+
gt.arity(fetchVersions, 2)
1616
})
1717

1818
gt.async('fetch non existent module', 2, function () {
19-
var promise = fetchVersions({
19+
var promise = fetchVersions({}, {
2020
name: 'this-module-should-not-exist-at-all',
2121
version: '0.2.0'
2222
})
@@ -31,8 +31,8 @@ gt.async('fetch non existent module', 2, function () {
3131

3232
gt.async('fetch gt later versions', function () {
3333
gt.func(fetchVersions)
34-
gt.arity(fetchVersions, 1)
35-
var promise = fetchVersions({ name: 'gt', version: '0.5.0' })
34+
gt.arity(fetchVersions, 2)
35+
var promise = fetchVersions({}, { name: 'gt', version: '0.5.0' })
3636
gt.func(promise.then, 'return object has then method')
3737
promise.then(function (results) {
3838
gt.object(results, 'returns an object')
@@ -44,8 +44,8 @@ gt.async('fetch gt later versions', function () {
4444

4545
gt.async('fetch module later versions', function () {
4646
gt.func(fetchVersions)
47-
gt.arity(fetchVersions, 1)
48-
var promise = fetchVersions({ name: 'lodash', version: '0.7.0' })
47+
gt.arity(fetchVersions, 2)
48+
var promise = fetchVersions({}, { name: 'lodash', version: '0.7.0' })
4949
gt.func(promise.then, 'return object has then method')
5050
promise.then(function (results) {
5151
gt.object(results, 'returns an object')

0 commit comments

Comments
 (0)