Skip to content

Commit

Permalink
feat(publish): add --access flag to publish command
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleygwilliams committed Sep 11, 2018
1 parent 2ef929d commit fb32af4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod utils;
use self::build::{Build, BuildOptions};
use self::login::login;
use self::pack::pack;
use self::publish::publish;
use self::publish::{publish, Access};
use error::Error;
use slog::Logger;
use std::path::PathBuf;
Expand All @@ -35,6 +35,10 @@ pub enum Command {
/// 🎆 pack up your npm package and publish!
Publish {
/// The path to the Rust crate.
#[structopt(long = "access", short = "a")]
access: Option<Access>,

/// The access level for the package to be published
#[structopt(parse(from_os_str))]
path: Option<PathBuf>,
},
Expand Down Expand Up @@ -86,10 +90,10 @@ pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error
info!(&log, "Path: {:?}", &path);
pack(path, &log)
}
Command::Publish { path } => {
Command::Publish { path, access } => {
info!(&log, "Running publish command...");
info!(&log, "Path: {:?}", &path);
publish(path, &log)
publish(path, access, &log)
}
Command::Login {
registry,
Expand Down
36 changes: 34 additions & 2 deletions src/command/publish.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
use self::Access::{Public, Restricted};
use command::utils::{find_pkg_directory, set_crate_path};
use error::Error;
use npm;
use slog::Logger;
use std::path::PathBuf;
use std::result;
use std::str::FromStr;
use PBAR;

#[derive(Debug)]
pub enum Access {
Public,
Restricted,
}

impl FromStr for Access {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Error> {
match s {
"public" => Ok(Access::Public),
"restricted" => Ok(Access::Restricted),
"private" => Ok(Access::Restricted),
_ => Err(Error::Unsupported { message: format!("{} is not a supported access level. See https://docs.npmjs.com/cli/access for more information on npm package access levels.", s)}),
}
}
}

/// Creates a tarball from a 'pkg' directory
/// and publishes it to the NPM registry
pub fn publish(path: Option<PathBuf>, log: &Logger) -> result::Result<(), Error> {
pub fn publish(
path: Option<PathBuf>,
access: Option<Access>,
log: &Logger,
) -> result::Result<(), Error> {
let crate_path = set_crate_path(path);
let access = match access {
Some(access) => match access {
Public => "public",
Restricted => "restricted",
},
None => "",
};

info!(&log, "Publishing the npm package...");
info!(&log, "npm info located in the npm debug log");
Expand All @@ -20,7 +52,7 @@ pub fn publish(path: Option<PathBuf>, log: &Logger) -> result::Result<(), Error>
),
})?;

npm::npm_publish(&pkg_directory.to_string_lossy())?;
npm::npm_publish(&pkg_directory.to_string_lossy(), access)?;
info!(&log, "Published your package!");

PBAR.message("💥 published your package!");
Expand Down
2 changes: 1 addition & 1 deletion src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn log_file_path(cmd: &Command) -> PathBuf {
let path = match cmd {
Command::Build(build_opts) => &build_opts.path,
Command::Pack { path } => path,
Command::Publish { path } => path,
Command::Publish { path, access: _ } => path,
Command::Login { .. } => &None,
};

Expand Down
3 changes: 2 additions & 1 deletion src/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ pub fn npm_pack(path: &str) -> Result<(), Error> {
}

/// Run the `npm publish` command.
pub fn npm_publish(path: &str) -> Result<(), Error> {
pub fn npm_publish(path: &str, access: &str) -> Result<(), Error> {
let output = Command::new("npm")
.current_dir(path)
.arg("publish")
.arg(&format!("--{}", access))
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.output()?;
Expand Down

0 comments on commit fb32af4

Please sign in to comment.