Merge pull request #299 from rustwasm/access-flag

feat(publish): add --access flag to publish command
master
Michael Gattozzi 7 years ago committed by GitHub
commit 6ad8b0fb57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      src/command/mod.rs
  2. 35
      src/command/publish/access.rs
  3. 12
      src/command/publish/mod.rs
  4. 2
      src/logger.rs
  5. 25
      src/npm.rs

@ -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;
@ -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>,
@ -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,

@ -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)
}
}

@ -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;
@ -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...");
@ -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!");

@ -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,
};

@ -1,5 +1,6 @@
//! Functionality related to publishing to npm.
use command::publish::access::Access;
use error::Error;
use std::process::{Command, Stdio};
@ -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)

Loading…
Cancel
Save