Skip to content

Commit

Permalink
feat: support auth for add, deps, remove, and view commands
Browse files Browse the repository at this point in the history
  • Loading branch information
favoyang committed Aug 9, 2020
1 parent 0184fa4 commit b41725f
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 77 deletions.
3 changes: 2 additions & 1 deletion lib/cmd-add.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const {
const add = async function(pkgs, options) {
if (!Array.isArray(pkgs)) pkgs = [pkgs];
// parse env
if (!parseEnv(options, { checkPath: true })) return 1;
const envOk = await parseEnv(options, { checkPath: true });
if (!envOk) return 1;
// add
const results = [];
for (const pkg of pkgs)
Expand Down
3 changes: 2 additions & 1 deletion lib/cmd-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const { fetchPackageDependencies, parseEnv, parseName } = require("./core");

const deps = async function(pkg, options) {
// parse env
if (!parseEnv(options, { checkPath: false })) return 1;
const envOk = await parseEnv(options, { checkPath: false });
if (!envOk) return 1;
// parse name
let { name, version } = parseName(pkg);
// deps
Expand Down
4 changes: 2 additions & 2 deletions lib/cmd-login.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const path = require("path");

const _ = require("lodash");
const promptly = require("promptly");
const TOML = require("@iarna/toml");

const { getNpmClient } = require("./client");
const { log } = require("./logger");
Expand All @@ -16,7 +15,8 @@ const {

const login = async function(options) {
// parse env
if (!parseEnv(options, { checkPath: false })) return 1;
const envOk = await parseEnv(options, { checkPath: false });
if (!envOk) return 1;
// query parameters
if (!options.username) options.username = await promptly.prompt("Username: ");
if (!options.password)
Expand Down
3 changes: 2 additions & 1 deletion lib/cmd-remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const {
const remove = async function(pkgs, options) {
if (!Array.isArray(pkgs)) pkgs = [pkgs];
// parse env
if (!parseEnv(options, { checkPath: true })) return 1;
const envOk = await parseEnv(options, { checkPath: true });
if (!envOk) return 1;
// remove
const results = [];
for (const pkg of pkgs) results.push(await _remove(pkg));
Expand Down
3 changes: 2 additions & 1 deletion lib/cmd-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ const getTable = function() {

module.exports = async function(keyword, options) {
// parse env
if (!parseEnv(options, { checkPath: false })) return 1;
const envOk = await parseEnv(options, { checkPath: false });
if (!envOk) return 1;
let table = getTable();
// search endpoint
let results = await searchEndpoint(keyword);
Expand Down
3 changes: 2 additions & 1 deletion lib/cmd-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const {

const view = async function(pkg, options) {
// parse env
if (!parseEnv(options, { checkPath: false })) return 1;
const envOk = await parseEnv(options, { checkPath: false });
if (!envOk) return 1;
// parse name
let { name, version } = parseName(pkg);
if (version) {
Expand Down
41 changes: 39 additions & 2 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const { log } = require("./logger");
const env = {};

// Parse env
const parseEnv = function(options, { checkPath }) {
const parseEnv = async function(options, { checkPath }) {
// set defaults
env.registry = "https://package.openupm.com";
env.namespace = "com.openupm";
Expand All @@ -32,6 +32,10 @@ const parseEnv = function(options, { checkPath }) {
env.upstreamRegistry = "https://packages.unity.com";
env.systemUser = false;
env.wsl = false;
// the npmAuth field of .upmconfig.toml
env.npmAuth = {};
// the dict of auth param for npm registry API
env.auth = {};
// log level
log.level = options.parent.verbose ? "verbose" : "notice";
// color
Expand Down Expand Up @@ -62,6 +66,39 @@ const parseEnv = function(options, { checkPath }) {
// auth
if (options.parent.systemUser) env.systemUser = true;
if (options.parent.wsl) env.wsl = true;
const upmConfig = await loadUpmConfig();
if (upmConfig) {
env.npmAuth = upmConfig.npmAuth;
if (env.npmAuth) {
for (const reg in env.npmAuth) {
const regAuth = env.npmAuth[reg];
if (regAuth.token) {
env.auth[reg] = {
token: regAuth.token,
alwaysAuth: regAuth.alwaysAuth || false
};
} else if (regAuth._auth) {
const buf = Buffer.from(regAuth._auth, "base64");
const text = buf.toString("utf-8");
const [username, password] = text.split(":", 2);
env.auth[reg] = {
username,
password,
email: regAuth.email,
alwaysAuth: regAuth.alwaysAuth || false
};
} else {
log.warn(
"env.auth",
`failed to parse auth info for ${reg} in .upmconfig.toml: missing token or _auth fields`
);
log.warn("env.auth", regAuth);
}
}
}
}
// log.verbose("env.npmAuth", env.npmAuth);
// log.verbose("env.auth", env.auth);
// return if no need to check path
if (!checkPath) return true;
// cwd
Expand Down Expand Up @@ -111,7 +148,7 @@ const fetchPackageInfo = async function(name, registry) {
const pkgPath = `${registry}/${name}`;
const client = getNpmClient();
try {
return await client.get(pkgPath, {});
return await client.get(pkgPath, { auth: env.auth[registry] || undefined });
// eslint-disable-next-line no-empty
} catch (err) {}
};
Expand Down
Loading

0 comments on commit b41725f

Please sign in to comment.