From d5a7a7beb1473237fb79a48f5fcd26dd2b985bf8 Mon Sep 17 00:00:00 2001 From: csmoe Date: Sun, 11 Nov 2018 12:08:07 +0800 Subject: [PATCH] pre-build before wasm-pack publish --- src/command/build.rs | 19 +++++++++++++++++-- src/command/publish/mod.rs | 30 +++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/command/build.rs b/src/command/build.rs index c495cdc..8f947da 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -102,12 +102,12 @@ 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. @@ -122,6 +122,21 @@ pub struct BuildOptions { pub out_dir: String, } +impl Default for BuildOptions { + fn default() -> Self { + Self { + path: None, + scope: None, + mode: BuildMode::Normal, + disable_dts: false, + target: String::from("browser"), + debug: false, + dev: false, + out_dir: String::from("pkg"), + } + } +} + type BuildStep = fn(&mut Build, &Step, &Logger) -> Result<(), Error>; impl Build { diff --git a/src/command/publish/mod.rs b/src/command/publish/mod.rs index 8b2c564..6e7ee1e 100644 --- a/src/command/publish/mod.rs +++ b/src/command/publish/mod.rs @@ -2,6 +2,7 @@ pub mod access; use self::access::Access; +use command::build::{Build, BuildOptions}; use command::utils::{find_pkg_directory, set_crate_path}; use failure::Error; use npm; @@ -21,14 +22,29 @@ 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` + let build_opts = BuildOptions { + path: Some(crate_path.clone()), + ..Default::default() + }; + Build::try_from_opts(build_opts) + .and_then(|mut build| build.run(&log)) + .map(|()| crate_path.join("pkg")) + .map_err(|_| { + format_err!( + "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!");