Skip to content

Commit 90b605a

Browse files
Merge pull request #100 from djfarly/wrap-npm-adduser
Wrap npm adduser
2 parents 827ce54 + 58b75a4 commit 90b605a

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

src/command.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,38 @@ pub enum Command {
2323
#[structopt(long = "scope", short = "s")]
2424
scope: Option<String>,
2525
},
26+
2627
#[structopt(name = "pack")]
27-
/// 🍱 create a tar of your npm package but don't publish! [NOT IMPLEMENTED]
28+
/// 🍱 create a tar of your npm package but don't publish!
2829
Pack { path: Option<String> },
30+
2931
#[structopt(name = "publish")]
30-
/// 🎆 pack up your npm package and publish! [NOT IMPLEMENTED]
32+
/// 🎆 pack up your npm package and publish!
3133
Publish { path: Option<String> },
34+
35+
#[structopt(name = "login", alias = "adduser", alias = "add-user")]
36+
/// 👤 Add a registry user account! (aliases: adduser, add-user)
37+
Login {
38+
#[structopt(long = "registry", short = "r")]
39+
/// Default: 'https://registry.npmjs.org/'.
40+
/// 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.
41+
registry: Option<String>,
42+
43+
#[structopt(long = "scope", short = "s")]
44+
/// Default: none.
45+
/// If specified, the user and login credentials given will be associated with the specified scope.
46+
scope: Option<String>,
47+
48+
#[structopt(long = "always-auth", short = "a")]
49+
/// 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
50+
always_auth: bool,
51+
52+
#[structopt(long = "auth-type", short = "t")]
53+
/// Default: 'legacy'.
54+
/// Type: 'legacy', 'sso', 'saml', 'oauth'.
55+
/// 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.
56+
auth_type: Option<String>,
57+
},
3258
}
3359

3460
pub fn run_wasm_pack(command: Command) -> result::Result<(), Error> {
@@ -38,6 +64,12 @@ pub fn run_wasm_pack(command: Command) -> result::Result<(), Error> {
3864
Command::Init { path, scope } => init(path, scope),
3965
Command::Pack { path } => pack(path),
4066
Command::Publish { path } => publish(path),
67+
Command::Login {
68+
registry,
69+
scope,
70+
always_auth,
71+
auth_type,
72+
} => login(registry, scope, always_auth, auth_type),
4173
};
4274

4375
match status {
@@ -114,6 +146,20 @@ fn publish(path: Option<String>) -> result::Result<(), Error> {
114146
Ok(())
115147
}
116148

149+
fn login(
150+
registry: Option<String>,
151+
scope: Option<String>,
152+
always_auth: bool,
153+
auth_type: Option<String>,
154+
) -> result::Result<(), Error> {
155+
let registry = registry.unwrap_or(npm::DEFAULT_NPM_REGISTRY.to_string());
156+
157+
npm::npm_login(&registry, &scope, always_auth, &auth_type)?;
158+
159+
PBAR.message(&format!("👋 logged you in!"));
160+
Ok(())
161+
}
162+
117163
fn set_crate_path(path: Option<String>) -> String {
118164
let crate_path = match path {
119165
Some(p) => p,

src/npm.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use error::Error;
2-
use std::process::Command;
2+
use std::process::{Command, Stdio};
3+
4+
pub const DEFAULT_NPM_REGISTRY: &'static str = "https://registry.npmjs.org/";
35

46
pub fn npm_pack(path: &str) -> Result<(), Error> {
57
let pkg_file_path = format!("{}/pkg", path);
@@ -28,3 +30,40 @@ pub fn npm_publish(path: &str) -> Result<(), Error> {
2830
Ok(())
2931
}
3032
}
33+
34+
pub fn npm_login(
35+
registry: &String,
36+
scope: &Option<String>,
37+
always_auth: bool,
38+
auth_type: &Option<String>,
39+
) -> Result<(), Error> {
40+
let mut args = String::new();
41+
42+
args.push_str(&format!("--registry={}", registry));
43+
44+
if let Some(scope) = scope {
45+
args.push_str(&format!(" --scope={}", scope));
46+
}
47+
48+
if always_auth == true {
49+
args.push_str(" --always_auth");
50+
}
51+
52+
if let Some(auth_type) = auth_type {
53+
args.push_str(&format!(" --auth_type={}", auth_type));
54+
}
55+
56+
let output = Command::new("npm")
57+
.arg("login")
58+
.arg(args)
59+
.stdin(Stdio::inherit())
60+
.stdout(Stdio::inherit())
61+
.output()?;
62+
63+
if !output.status.success() {
64+
let s = String::from_utf8_lossy(&output.stderr);
65+
Error::cli(&format!("Login to registry {} failed", registry), s)
66+
} else {
67+
Ok(())
68+
}
69+
}

0 commit comments

Comments
 (0)