Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows fixes #489

Merged
merged 2 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use failure::Error;
use slog::Logger;
use std::{
io::{self, Read},
mem, process, string,
mem,
process::{Command, Stdio},
string,
sync::mpsc,
thread,
};
Expand All @@ -19,6 +21,22 @@ enum OutputFragment {
Stderr(Vec<u8>),
}

/// Return a new Command object
pub fn new_command(program: &str) -> Command {
// On Windows, initializes launching <program> as `cmd /c <program>`.
// Initializing only with `Command::new("npm")` will launch
// `npm` with quotes, `"npm"`, causing a run-time error on Windows.
// See rustc: #42436, #42791, #44542

if cfg!(windows) {
let mut cmd = Command::new("cmd");
cmd.arg("/c").arg(program);
cmd
} else {
Command::new(program)
}
}

/// Read data from the give reader and send it as an `OutputFragment` over the
/// given sender.
fn read_and_send<R, F>(
Expand Down Expand Up @@ -115,16 +133,12 @@ where
}

/// Run the given command and return its stdout.
pub fn run(
logger: &Logger,
mut command: process::Command,
command_name: &str,
) -> Result<String, Error> {
pub fn run(logger: &Logger, mut command: Command, command_name: &str) -> Result<String, Error> {
info!(logger, "Running {:?}", command);

let mut child = command
.stdout(process::Stdio::piped())
.stderr(process::Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;

let stdout = child.stdout.take().unwrap();
Expand Down
19 changes: 6 additions & 13 deletions src/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,27 @@ use child;
use command::publish::access::Access;
use failure::{self, ResultExt};
use slog::Logger;
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(log: &Logger, path: &str) -> Result<(), failure::Error> {
let mut cmd = Command::new("npm");
let mut cmd = child::new_command("npm");
cmd.current_dir(path).arg("pack");
child::run(log, cmd, "npm pack").context("Packaging up your code failed")?;
Ok(())
}

/// Run the `npm publish` command.
pub fn npm_publish(log: &Logger, path: &str, access: Option<Access>) -> Result<(), failure::Error> {
let mut cmd = Command::new("npm");
let mut cmd = child::new_command("npm");
match access {
Some(a) => cmd
.current_dir(path)
.arg("publish")
.arg(&format!("{}", a.to_string()))
.stdin(Stdio::inherit())
.stdout(Stdio::inherit()),
None => cmd
.current_dir(path)
.arg("publish")
.stdin(Stdio::inherit())
.stdout(Stdio::inherit()),
.arg(&format!("{}", a.to_string())),
None => cmd.current_dir(path).arg("publish"),
};

child::run(log, cmd, "npm publish").context("Publishing to npm failed")?;
Expand All @@ -52,7 +45,7 @@ pub fn npm_login(
args.push(format!("--scope={}", scope));
}

if always_auth == true {
if always_auth {
args.push(format!("--always_auth"));
}

Expand All @@ -62,7 +55,7 @@ pub fn npm_login(

// Interactively ask user for npm login info.
// (child::run does not support interactive input)
let mut cmd = Command::new("npm");
let mut cmd = child::new_command("npm");
cmd.args(args);

info!(log, "Running {:?}", cmd);
Expand Down