Merge pull request #100 from djfarly/wrap-npm-adduser

Wrap npm adduser
master
ashley williams 7 years ago committed by GitHub
commit 90b605aa43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 50
      src/command.rs
  2. 41
      src/npm.rs

@ -23,12 +23,38 @@ pub enum Command {
#[structopt(long = "scope", short = "s")]
scope: Option<String>,
},
#[structopt(name = "pack")]
/// 🍱 create a tar of your npm package but don't publish! [NOT IMPLEMENTED]
/// 🍱 create a tar of your npm package but don't publish!
Pack { path: Option<String> },
#[structopt(name = "publish")]
/// 🎆 pack up your npm package and publish! [NOT IMPLEMENTED]
/// 🎆 pack up your npm package and publish!
Publish { path: Option<String> },
#[structopt(name = "login", alias = "adduser", alias = "add-user")]
/// 👤 Add a registry user account! (aliases: adduser, add-user)
Login {
#[structopt(long = "registry", short = "r")]
/// Default: 'https://registry.npmjs.org/'.
/// The base URL of the npm package registry. If scope is also specified, this registry will only be used for packages with that scope. scope defaults to the scope of the project directory you're currently in, if any.
registry: Option<String>,
#[structopt(long = "scope", short = "s")]
/// Default: none.
/// If specified, the user and login credentials given will be associated with the specified scope.
scope: Option<String>,
#[structopt(long = "always-auth", short = "a")]
/// If specified, save configuration indicating that all requests to the given registry should include authorization information. Useful for private registries. Can be used with --registry and / or --scope
always_auth: bool,
#[structopt(long = "auth-type", short = "t")]
/// Default: 'legacy'.
/// Type: 'legacy', 'sso', 'saml', 'oauth'.
/// What authentication strategy to use with adduser/login. Some npm registries (for example, npmE) might support alternative auth strategies besides classic username/password entry in legacy npm.
auth_type: Option<String>,
},
}
pub fn run_wasm_pack(command: Command) -> result::Result<(), Error> {
@ -38,6 +64,12 @@ pub fn run_wasm_pack(command: Command) -> result::Result<(), Error> {
Command::Init { path, scope } => init(path, scope),
Command::Pack { path } => pack(path),
Command::Publish { path } => publish(path),
Command::Login {
registry,
scope,
always_auth,
auth_type,
} => login(registry, scope, always_auth, auth_type),
};
match status {
@ -114,6 +146,20 @@ fn publish(path: Option<String>) -> result::Result<(), Error> {
Ok(())
}
fn login(
registry: Option<String>,
scope: Option<String>,
always_auth: bool,
auth_type: Option<String>,
) -> result::Result<(), Error> {
let registry = registry.unwrap_or(npm::DEFAULT_NPM_REGISTRY.to_string());
npm::npm_login(&registry, &scope, always_auth, &auth_type)?;
PBAR.message(&format!("👋 logged you in!"));
Ok(())
}
fn set_crate_path(path: Option<String>) -> String {
let crate_path = match path {
Some(p) => p,

@ -1,5 +1,7 @@
use error::Error;
use std::process::Command;
use std::process::{Command, Stdio};
pub const DEFAULT_NPM_REGISTRY: &'static str = "https://registry.npmjs.org/";
pub fn npm_pack(path: &str) -> Result<(), Error> {
let pkg_file_path = format!("{}/pkg", path);
@ -28,3 +30,40 @@ pub fn npm_publish(path: &str) -> Result<(), Error> {
Ok(())
}
}
pub fn npm_login(
registry: &String,
scope: &Option<String>,
always_auth: bool,
auth_type: &Option<String>,
) -> Result<(), Error> {
let mut args = String::new();
args.push_str(&format!("--registry={}", registry));
if let Some(scope) = scope {
args.push_str(&format!(" --scope={}", scope));
}
if always_auth == true {
args.push_str(" --always_auth");
}
if let Some(auth_type) = auth_type {
args.push_str(&format!(" --auth_type={}", auth_type));
}
let output = Command::new("npm")
.arg("login")
.arg(args)
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.output()?;
if !output.status.success() {
let s = String::from_utf8_lossy(&output.stderr);
Error::cli(&format!("Login to registry {} failed", registry), s)
} else {
Ok(())
}
}

Loading…
Cancel
Save