From 03534e51bcc57c6a3379e885d1541efa777c965d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20B=C3=B6nnemann?= Date: Sat, 22 Aug 2015 23:32:47 +0200 Subject: [PATCH] fix(registry): be better at using the correct registry (relevant for npme) Closes #53 --- src/index.js | 6 ++++-- src/lib/get-registry.js | 12 ++++++++++++ test/specs/get-registry.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/lib/get-registry.js create mode 100644 test/specs/get-registry.js diff --git a/src/index.js b/src/index.js index ec29ce7c..e41aadbd 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ const { readFileSync, writeFileSync } = require('fs') const path = require('path') +const url = require('url') const _ = require('lodash') const log = require('npmlog') @@ -42,11 +43,12 @@ npmconf.load({}, (err, conf) => { token: env.NPM_TOKEN }, loglevel: conf.get('loglevel'), - registry: conf.get('registry'), + registry: require('./lib/get-registry')(pkg, conf), tag: (pkg.publishConfig || {}).tag || conf.get('tag') || 'latest' } - if (npm.registry[npm.registry.length - 1] !== '/') npm.registry += '/' + // normalize trailing slash + npm.registry = url.format(url.parse(npm.registry)) log.level = npm.loglevel diff --git a/src/lib/get-registry.js b/src/lib/get-registry.js new file mode 100644 index 00000000..a7557c9c --- /dev/null +++ b/src/lib/get-registry.js @@ -0,0 +1,12 @@ +module.exports = function (pkg, conf) { + if (pkg.publishConfig && pkg.publishConfig.registry) return pkg.publishConfig.registry + + if (pkg.name[0] !== '@') return conf.get('registry') || 'https://registry.npmjs.org/' + + const scope = pkg.name.split('/')[0] + const scopedRegistry = conf.get(`${scope}/registry`) + + if (scopedRegistry) return scopedRegistry + + return conf.get('registry') || 'https://registry.npmjs.org/' +} diff --git a/test/specs/get-registry.js b/test/specs/get-registry.js new file mode 100644 index 00000000..2b7d3f3c --- /dev/null +++ b/test/specs/get-registry.js @@ -0,0 +1,30 @@ +const test = require('tap').test + +const getRegistry = require('../../dist/lib/get-registry') + +test('get correct registry', (t) => { + t.is(getRegistry({ + name: 'publish-config', + publishConfig: { + registry: 'a' + }}, + {}), 'a') + + t.is(getRegistry({name: 'normal'}, {get: () => 'b'}), 'b') + + t.is(getRegistry({name: 'normal'}, {get: () => null}), 'https://registry.npmjs.org/') + + t.is(getRegistry({name: '@scoped/foo'}, { + get: (input) => input === '@scoped/registry' ? 'c' : 'd' + }), 'c') + + t.is(getRegistry({name: '@scoped/bar'}, { + get: () => 'e' + }), 'e') + + t.is(getRegistry({name: '@scoped/baz'}, { + get: () => null + }), 'https://registry.npmjs.org/') + + t.end() +})