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 14, 2018
1 parent 0bd0b5a commit 0da4fa1
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 14 deletions.
13 changes: 9 additions & 4 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
pub mod build;
mod login;
mod pack;
mod publish;
pub mod test;
/// Data structures and functions for publishing a package.
pub mod publish;
pub mod utils;

use self::build::{Build, BuildOptions};
use self::login::login;
use self::pack::pack;
use self::publish::publish;
use self::test::{Test, TestOptions};
use self::publish::{access::Access, publish};
use error::Error;
use slog::Logger;
use std::path::PathBuf;
Expand All @@ -37,6 +38,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 @@ -92,10 +97,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
35 changes: 35 additions & 0 deletions src/command/publish/access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use error::Error;
use std::fmt;
use std::str::FromStr;

/// Represents access level for the to-be publish package. Passed to `wasm-pack publish` as a flag, e.g. `--access=public`.
#[derive(Debug)]
pub enum Access {
/// Access is granted to all. All unscoped packages *must* be public.
Public,
/// Access is restricted, granted via npm permissions. Must be a scoped package.
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)}),
}
}
}

impl fmt::Display for Access {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let printable = match *self {
Access::Public => "--access=public",
Access::Restricted => "--access=restricted",
};
write!(f, "{}", printable)
}
}
12 changes: 10 additions & 2 deletions src/command/publish.rs → src/command/publish/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/// Data structure to represent published package access level.
pub mod access;

use self::access::Access;
use command::utils::{find_pkg_directory, set_crate_path};
use error::Error;
use npm;
Expand All @@ -8,7 +12,11 @@ use PBAR;

/// 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)?;

info!(&log, "Publishing the npm package...");
Expand All @@ -20,7 +28,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::Test(test_opts) => &test_opts.path,
Command::Login { .. } => &None,
};
Expand Down
29 changes: 22 additions & 7 deletions src/npm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Functionality related to publishing to npm.

use command::publish::access::Access;
use error::Error;
use std::process::{Command, Stdio};

Expand All @@ -18,13 +19,27 @@ pub fn npm_pack(path: &str) -> Result<(), Error> {
}

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

if !output.status.success() {
let s = String::from_utf8_lossy(&output.stderr);
Error::cli("Publishing to npm failed", s)
Expand Down

0 comments on commit 0da4fa1

Please sign in to comment.