Skip to content
This repository has been archived by the owner on Nov 8, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' into refactor-binary-installation
Browse files Browse the repository at this point in the history
  • Loading branch information
drager committed Dec 28, 2018
2 parents a2235e5 + 9c01c38 commit 28f67e7
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 22 deletions.
66 changes: 66 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ documentation = "https://rustwasm.github.io/wasm-pack/"
atty = "0.2.11"
cargo_metadata = "0.6.0"
console = "0.6.1"
dialoguer = "0.3.0"
failure = "0.1.2"
human-panic = "1.0.1"
glob = "0.2"
Expand All @@ -32,6 +33,7 @@ structopt = "0.2"
toml = "0.4"
which = "2.0.0"
wasm-pack-binary-install = { version = "0.1.0", path = "./binary-install" }
walkdir = "2"

[dev-dependencies]
tempfile = "3"
Expand Down
32 changes: 25 additions & 7 deletions src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,43 @@ pub struct BuildOptions {

#[structopt(long = "debug")]
/// Deprecated. Renamed to `--dev`.
debug: bool,
pub debug: bool,

#[structopt(long = "dev")]
/// Create a development build. Enable debug info, and disable
/// optimizations.
dev: bool,
pub dev: bool,

#[structopt(long = "release")]
/// Create a release build. Enable optimizations and disable debug info.
release: bool,
pub release: bool,

#[structopt(long = "profiling")]
/// Create a profiling build. Enable optimizations and debug info.
profiling: bool,
pub profiling: bool,

#[structopt(long = "out-dir", short = "d", default_value = "pkg")]
/// Sets the output directory with a relative path.
pub out_dir: String,
}

impl Default for BuildOptions {
fn default() -> Self {
Self {
path: None,
scope: None,
mode: BuildMode::Normal,
disable_dts: false,
target: String::new(),
debug: false,
dev: false,
release: false,
profiling: false,
out_dir: String::new(),
}
}
}

type BuildStep = fn(&mut Build, &Step, &Logger) -> Result<(), Error>;

impl Build {
Expand Down Expand Up @@ -177,15 +194,16 @@ impl Build {
info!(&log, "Done in {}.", &duration);
info!(
&log,
"Your wasm pkg is ready to publish at {:#?}.", &self.out_dir
"Your wasm pkg is ready to publish at {}.",
self.out_dir.display()
);

PBAR.message(&format!("{} Done in {}", emoji::SPARKLE, &duration));

PBAR.message(&format!(
"{} Your wasm pkg is ready to publish at {:#?}.",
"{} Your wasm pkg is ready to publish at {}.",
emoji::PACKAGE,
self.out_dir.canonicalize().unwrap_or(self.out_dir.clone())
self.out_dir.display()
));
Ok(())
}
Expand Down
12 changes: 10 additions & 2 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ pub enum Command {
#[structopt(name = "publish")]
/// 🎆 pack up your npm package and publish!
Publish {
#[structopt(long = "target", short = "t", default_value = "browser")]
/// Sets the target environment. [possible values: browser, nodejs, no-modules]
target: String,

/// The access level for the package to be published
#[structopt(long = "access", short = "a")]
access: Option<Access>,
Expand Down Expand Up @@ -96,10 +100,14 @@ pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error
info!(&log, "Path: {:?}", &path);
pack(path, &log)
}
Command::Publish { path, access } => {
Command::Publish {
target,
path,
access,
} => {
info!(&log, "Running publish command...");
info!(&log, "Path: {:?}", &path);
publish(path, access, &log)
publish(target, path, access, &log)
}
Command::Login {
registry,
Expand Down
58 changes: 51 additions & 7 deletions src/command/publish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
pub mod access;

use self::access::Access;
use command::build::{Build, BuildOptions};
use command::utils::{find_pkg_directory, set_crate_path};
use dialoguer::{Confirmation, Input, Select};
use failure::Error;
use npm;
use slog::Logger;
Expand All @@ -13,6 +15,7 @@ use PBAR;
/// Creates a tarball from a 'pkg' directory
/// and publishes it to the NPM registry
pub fn publish(
_target: String,
path: Option<PathBuf>,
access: Option<Access>,
log: &Logger,
Expand All @@ -21,14 +24,55 @@ pub fn publish(

info!(&log, "Publishing the npm package...");
info!(&log, "npm info located in the npm debug log");
let pkg_directory = find_pkg_directory(&crate_path).ok_or_else(|| {
format_err!(
"Unable to find the pkg directory at path '{:#?}', or in a child directory of '{:#?}'",
&crate_path,
&crate_path
)
})?;

let pkg_directory = match find_pkg_directory(&crate_path) {
Some(path) => Ok(path),
None => {
// while `wasm-pack publish`, if the pkg directory cannot be found,
// then try to `wasm-pack build`
if Confirmation::new()
.with_text("Your package hasn't been built, build it?")
.interact()?
{
let out_dir = Input::new()
.with_prompt("out_dir[default: pkg]")
.default(".".to_string())
.show_default(false)
.interact()?;
let out_dir = format!("{}/pkg", out_dir);
let target = Select::new()
.with_prompt("target[default: browser]")
.items(&["browser", "nodejs", "no-modules"])
.default(0)
.interact()?
.to_string();
let build_opts = BuildOptions {
path: Some(crate_path.clone()),
target,
out_dir: out_dir.clone(),
..Default::default()
};
Build::try_from_opts(build_opts)
.and_then(|mut build| build.run(&log))
.map(|()| crate_path.join(out_dir))
.map_err(|_| {
format_err!(
"Unable to find the pkg directory at path '{:#?}',\
or in a child directory of '{:#?}'",
&crate_path,
&crate_path
)
})
} else {
bail!(
"Unable to find the pkg directory at path '{:#?}',\
or in a child directory of '{:#?}'",
&crate_path,
&crate_path
)
}
}
}?;
npm::npm_publish(log, &pkg_directory.to_string_lossy(), access)?;
info!(&log, "Published your package!");

Expand Down
10 changes: 5 additions & 5 deletions src/command/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use failure;
use progressbar::Step;
use std::fs;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;
use PBAR;

/// If an explicit path is given, then use it, otherwise assume the current
Expand All @@ -29,11 +30,10 @@ pub fn find_pkg_directory(path: &Path) -> Option<PathBuf> {
return Some(path.to_owned());
}

path.read_dir().ok().and_then(|entries| {
entries
.filter_map(|x| x.ok().map(|v| v.path()))
.find(|x| is_pkg_directory(&x))
})
WalkDir::new(path)
.into_iter()
.filter_map(|x| x.ok().map(|e| e.into_path()))
.find(|x| is_pkg_directory(&x))
}

fn is_pkg_directory(path: &Path) -> bool {
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ extern crate serde_json;
extern crate structopt;
#[macro_use]
extern crate slog;
extern crate dialoguer;
extern crate slog_async;
extern crate slog_term;
extern crate toml;
extern crate walkdir;
extern crate wasm_pack_binary_install;

pub mod bindgen;
Expand Down
Loading

0 comments on commit 28f67e7

Please sign in to comment.