add --auth option - #19

This commit is contained in:
silverwind 2019-02-07 19:21:17 +01:00
parent 64ea707954
commit d80e48d364
Signed by: silverwind
GPG Key ID: 2E62B41C93869443
3 changed files with 23 additions and 1 deletions

@ -29,6 +29,7 @@ usage: updates [options]
-m, --minor [<pkg,...>] Consider only up to semver-minor
-E, --error-on-outdated Exit with error code 2 on outdated packages
-r, --registry <url> Use given registry URL
-a, --auth Authorize against the registry
-f, --file <path> Use given package.json file or module directory
-j, --json Output a JSON object
-c, --color Force-enable color output

@ -33,6 +33,7 @@
"hosted-git-info": "2.7.1",
"make-fetch-happen": "4.0.1",
"minimist": "1.2.0",
"registry-auth-token": "3.3.2",
"semver": "5.6.0",
"string-width": "3.0.0",
"text-table": "0.2.0"

@ -5,6 +5,7 @@ process.env.NODE_ENV = "production";
const args = require("minimist")(process.argv.slice(2), {
boolean: [
"a", "auth",
"c", "color",
"E", "error-on-outdated",
"h", "help",
@ -26,6 +27,7 @@ const args = require("minimist")(process.argv.slice(2), {
"registry": "https://registry.npmjs.org/",
},
alias: {
a: "auth",
c: "color",
E: "error-on-outdated",
e: "exclude",
@ -60,6 +62,7 @@ if (args.help) {
-m, --minor [<pkg,...>] Consider only up to semver-minor
-E, --error-on-outdated Exit with error code 2 on outdated packages
-r, --registry <url> Use given registry URL
-a, --auth Authorize against the registry
-f, --file <path> Use given package.json file or module directory
-j, --json Output a JSON object
-c, --color Force-enable color output
@ -179,13 +182,30 @@ const fetch = require("make-fetch-happen");
const chalk = require("chalk");
const hostedGitInfo = require("hosted-git-info");
let auth;
if (args.auth) {
auth = require("registry-auth-token")(registry);
if (!auth) {
finish(new Error(`Unable to find auth token for ${registry}`));
}
}
const get = async name => {
// on scoped packages replace "/" with "%2f"
if (/@[a-z0-9][\w-.]+\/[a-z0-9][\w-.]*/gi.test(name)) {
name = name.replace(/\//g, "%2f");
}
return fetch(registry + name).then(r => r.json());
let opts;
if (auth && auth.token) {
opts = {
headers: {
Authorization: `Bearer ${auth.token}`,
},
};
}
return fetch(registry + name, opts).then(r => r.json());
};
const getInfoUrl = ({repository, homepage}) => {