-
Notifications
You must be signed in to change notification settings - Fork 416
/
Copy pathnpm.rs
57 lines (47 loc) · 1.75 KB
/
npm.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! Functionality related to publishing to npm.
use crate::child;
use crate::command::publish::access::Access;
use anyhow::{bail, Context, Result};
use log::info;
/// The default npm registry used when we aren't working with a custom registry.
pub const DEFAULT_NPM_REGISTRY: &str = "https://registry.npmjs.org/";
/// Run the `npm pack` command.
pub fn npm_pack(path: &str) -> Result<()> {
let mut cmd = child::new_command("npm");
cmd.current_dir(path).arg("pack");
child::run(cmd, "npm pack").context("Packaging up your code failed")?;
Ok(())
}
/// Run the `npm publish` command.
pub fn npm_publish(path: &str, access: Option<Access>, tag: Option<String>) -> Result<()> {
let mut cmd = child::new_command("npm");
match access {
Some(a) => cmd.current_dir(path).arg("publish").arg(&a.to_string()),
None => cmd.current_dir(path).arg("publish"),
};
if let Some(tag) = tag {
cmd.arg("--tag").arg(tag);
};
child::run(cmd, "npm publish").context("Publishing to npm failed")?;
Ok(())
}
/// Run the `npm login` command.
pub fn npm_login(registry: &str, scope: &Option<String>, auth_type: &Option<String>) -> Result<()> {
let mut args = vec!["login".to_string(), format!("--registry={}", registry)];
if let Some(scope) = scope {
args.push(format!("--scope={}", scope));
}
if let Some(auth_type) = auth_type {
args.push(format!("--auth_type={}", auth_type));
}
// Interactively ask user for npm login info.
// (child::run does not support interactive input)
let mut cmd = child::new_command("npm");
cmd.args(args);
info!("Running {:?}", cmd);
if cmd.status()?.success() {
Ok(())
} else {
bail!("Login to registry {} failed", registry)
}
}