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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! Functionality related to publishing to npm.

use error::Error;
use std::process::{Command, Stdio};

/// The default npm registry used when we aren't working with a custom registry.
pub const DEFAULT_NPM_REGISTRY: &'static str = "https://registry.npmjs.org/";

/// Run the `npm pack` command.
pub fn npm_pack(path: &str) -> Result<(), Error> {
    let pkg_file_path = format!("{}/pkg", path);
    let output = Command::new("npm")
        .current_dir(pkg_file_path)
        .arg("pack")
        .output()?;
    if !output.status.success() {
        let s = String::from_utf8_lossy(&output.stderr);
        Error::cli("Packaging up your code failed", s)
    } else {
        Ok(())
    }
}

/// Run the `npm publish` command.
pub fn npm_publish(path: &str) -> Result<(), Error> {
    let pkg_file_path = format!("{}/pkg", path);
    let output = Command::new("npm")
        .current_dir(pkg_file_path)
        .arg("publish")
        .output()?;
    if !output.status.success() {
        let s = String::from_utf8_lossy(&output.stderr);
        Error::cli("Publishing to npm failed", s)
    } else {
        Ok(())
    }
}

/// Run the `npm login` command.
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(())
    }
}