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

pre-build before wasm-pack publish #444

Merged
merged 4 commits into from
Dec 28, 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
581 changes: 390 additions & 191 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ atty = "0.2.11"
cargo_metadata = "0.6.0"
console = "0.6.1"
curl = "0.4.13"
dialoguer = "0.3.0"
dirs = "1.0.4"
failure = "0.1.2"
flate2 = "1.0.2"
Expand All @@ -37,6 +38,7 @@ structopt = "0.2"
tar = "0.4.16"
toml = "0.4"
which = "2.0.0"
walkdir = "2"
zip = "0.5.0"

[dev-dependencies]
Expand Down
25 changes: 21 additions & 4 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
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 @@ -28,11 +29,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 @@ -5,6 +5,7 @@
extern crate cargo_metadata;
extern crate console;
extern crate curl;
extern crate dialoguer;
extern crate dirs;
extern crate strsim;
#[macro_use]
Expand All @@ -30,6 +31,7 @@ extern crate slog_async;
extern crate slog_term;
extern crate tar;
extern crate toml;
extern crate walkdir;
extern crate which;
extern crate zip;

Expand Down
6 changes: 5 additions & 1 deletion src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ 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, access: _ } => path,
Command::Publish {
target: _,
path,
access: _,
} => path,
Command::Test(test_opts) => &test_opts.path,
Command::Login { .. } => &None,
};
Expand Down