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

feat(publish): add --access flag to publish command #299

Merged
merged 1 commit into from
Sep 14, 2018
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
13 changes: 9 additions & 4 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
pub mod build;
mod login;
mod pack;
mod publish;
/// Data structures and functions for publishing a package.
pub mod publish;
pub mod test;
pub mod utils;

use self::build::{Build, BuildOptions};
use self::login::login;
use self::pack::pack;
use self::publish::publish;
use self::publish::{access::Access, publish};
use self::test::{Test, TestOptions};
use error::Error;
use slog::Logger;
Expand All @@ -36,6 +37,10 @@ pub enum Command {
#[structopt(name = "publish")]
/// 🎆 pack up your npm package and publish!
Publish {
/// The access level for the package to be published
#[structopt(long = "access", short = "a")]
access: Option<Access>,

/// The path to the Rust crate.
#[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
25 changes: 18 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,23 @@ 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