Merge pull request #263 from xmclark/cli-pkg-dir

Specify output directory with CLI
master
Nick Fitzgerald 7 years ago committed by GitHub
commit 89be40df2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      .travis.yml
  2. 5
      src/bindgen.rs
  3. 31
      src/command/build.rs
  4. 5
      src/command/utils.rs
  5. 3
      src/manifest.rs
  6. 8
      src/readme.rs
  7. 86
      tests/all/manifest.rs
  8. 14
      tests/all/readme.rs
  9. 4
      tests/all/utils/manifest.rs

@ -1,6 +1,5 @@
language: rust language: rust
sudo: false sudo: false
cache: cargo
DEPLOY_TO_GITHUB: &DEPLOY_TO_GITHUB DEPLOY_TO_GITHUB: &DEPLOY_TO_GITHUB
before_deploy: before_deploy:

@ -186,6 +186,7 @@ pub fn cargo_install_wasm_bindgen(root_path: &Path, version: &str) -> Result<(),
/// `.wasm`. /// `.wasm`.
pub fn wasm_bindgen_build( pub fn wasm_bindgen_build(
path: &Path, path: &Path,
out_dir: &Path,
name: &str, name: &str,
disable_dts: bool, disable_dts: bool,
target: &str, target: &str,
@ -198,6 +199,8 @@ pub fn wasm_bindgen_build(
let binary_name = name.replace("-", "_"); let binary_name = name.replace("-", "_");
let release_or_debug = if debug { "debug" } else { "release" }; let release_or_debug = if debug { "debug" } else { "release" };
let out_dir = out_dir.to_str().unwrap();
if let Some(wasm_bindgen_path) = wasm_bindgen_path(path) { if let Some(wasm_bindgen_path) = wasm_bindgen_path(path) {
let wasm_path = format!( let wasm_path = format!(
"target/wasm32-unknown-unknown/{}/{}.wasm", "target/wasm32-unknown-unknown/{}/{}.wasm",
@ -217,7 +220,7 @@ pub fn wasm_bindgen_build(
.current_dir(path) .current_dir(path)
.arg(&wasm_path) .arg(&wasm_path)
.arg("--out-dir") .arg("--out-dir")
.arg("./pkg") .arg(out_dir)
.arg(dts_arg) .arg(dts_arg)
.arg(target_arg) .arg(target_arg)
.output()?; .output()?;

@ -22,6 +22,7 @@ pub(crate) struct Build {
pub mode: BuildMode, pub mode: BuildMode,
// build_config: Option<BuildConfig>, // build_config: Option<BuildConfig>,
pub crate_name: String, pub crate_name: String,
pub out_dir: PathBuf,
} }
/// The `BuildMode` determines which mode of initialization we are running, and /// The `BuildMode` determines which mode of initialization we are running, and
@ -63,6 +64,9 @@ pub struct BuildOptions {
debug: bool, debug: bool,
// build config from manifest // build config from manifest
// build_config: Option<BuildConfig>, // build_config: Option<BuildConfig>,
#[structopt(long = "out-dir", short = "d", default_value = "pkg")]
/// Sets the output directory with a relative path.
pub out_dir: String,
} }
type BuildStep = fn(&mut Build, &Step, &Logger) -> Result<(), Error>; type BuildStep = fn(&mut Build, &Step, &Logger) -> Result<(), Error>;
@ -72,6 +76,7 @@ impl Build {
pub fn try_from_opts(build_opts: BuildOptions) -> Result<Self, Error> { pub fn try_from_opts(build_opts: BuildOptions) -> Result<Self, Error> {
let crate_path = set_crate_path(build_opts.path); let crate_path = set_crate_path(build_opts.path);
let crate_name = manifest::get_crate_name(&crate_path)?; let crate_name = manifest::get_crate_name(&crate_path)?;
let out_dir = crate_path.join(PathBuf::from(build_opts.out_dir));
let mode = match build_opts.mode.as_str() { let mode = match build_opts.mode.as_str() {
"no-install" => BuildMode::Noinstall, "no-install" => BuildMode::Noinstall,
_ => BuildMode::Normal, _ => BuildMode::Normal,
@ -86,6 +91,7 @@ impl Build {
mode, mode,
// build_config, // build_config,
crate_name, crate_name,
out_dir,
}) })
} }
@ -106,8 +112,7 @@ impl Build {
info!(&log, "Done in {}.", &duration); info!(&log, "Done in {}.", &duration);
info!( info!(
&log, &log,
"Your wasm pkg is ready to publish at {:#?}.", "Your wasm pkg is ready to publish at {:#?}.", &self.out_dir
&self.crate_path.join("pkg")
); );
PBAR.message(&format!("{} Done in {}", emoji::SPARKLE, &duration)); PBAR.message(&format!("{} Done in {}", emoji::SPARKLE, &duration));
@ -115,7 +120,7 @@ impl Build {
PBAR.message(&format!( PBAR.message(&format!(
"{} Your wasm pkg is ready to publish at {:#?}.", "{} Your wasm pkg is ready to publish at {:#?}.",
emoji::PACKAGE, emoji::PACKAGE,
&self.crate_path.join("pkg") self.out_dir.canonicalize().unwrap_or(self.out_dir.clone())
)); ));
Ok(()) Ok(())
} }
@ -185,7 +190,7 @@ impl Build {
fn step_create_dir(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { fn step_create_dir(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Creating a pkg directory..."); info!(&log, "Creating a pkg directory...");
create_pkg_dir(&self.crate_path, step)?; create_pkg_dir(&self.out_dir, step)?;
info!(&log, "Created a pkg directory at {:#?}.", &self.crate_path); info!(&log, "Created a pkg directory at {:#?}.", &self.crate_path);
Ok(()) Ok(())
} }
@ -194,6 +199,7 @@ impl Build {
info!(&log, "Writing a package.json..."); info!(&log, "Writing a package.json...");
manifest::write_package_json( manifest::write_package_json(
&self.crate_path, &self.crate_path,
&self.out_dir,
&self.scope, &self.scope,
self.disable_dts, self.disable_dts,
&self.target, &self.target,
@ -202,19 +208,15 @@ impl Build {
info!( info!(
&log, &log,
"Wrote a package.json at {:#?}.", "Wrote a package.json at {:#?}.",
&self.crate_path.join("pkg").join("package.json") &self.out_dir.join("package.json")
); );
Ok(()) Ok(())
} }
fn step_copy_readme(&mut self, step: &Step, log: &Logger) -> Result<(), Error> { fn step_copy_readme(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Copying readme from crate..."); info!(&log, "Copying readme from crate...");
readme::copy_from_crate(&self.crate_path, step)?; readme::copy_from_crate(&self.crate_path, &self.out_dir, step)?;
info!( info!(&log, "Copied readme from crate to {:#?}.", &self.out_dir);
&log,
"Copied readme from crate to {:#?}.",
&self.crate_path.join("pkg")
);
Ok(()) Ok(())
} }
@ -250,17 +252,14 @@ impl Build {
info!(&log, "Building the wasm bindings..."); info!(&log, "Building the wasm bindings...");
bindgen::wasm_bindgen_build( bindgen::wasm_bindgen_build(
&self.crate_path, &self.crate_path,
&self.out_dir,
&self.crate_name, &self.crate_name,
self.disable_dts, self.disable_dts,
&self.target, &self.target,
self.debug, self.debug,
step, step,
)?; )?;
info!( info!(&log, "wasm bindings were built at {:#?}.", &self.out_dir);
&log,
"wasm bindings were built at {:#?}.",
&self.crate_path.join("pkg")
);
Ok(()) Ok(())
} }
} }

@ -19,11 +19,10 @@ pub fn set_crate_path(path: Option<PathBuf>) -> PathBuf {
} }
/// Construct our `pkg` directory in the crate. /// Construct our `pkg` directory in the crate.
pub fn create_pkg_dir(path: &Path, step: &Step) -> Result<(), Error> { pub fn create_pkg_dir(out_dir: &Path, step: &Step) -> Result<(), Error> {
let msg = format!("{}Creating a pkg directory...", emoji::FOLDER); let msg = format!("{}Creating a pkg directory...", emoji::FOLDER);
PBAR.step(step, &msg); PBAR.step(step, &msg);
let pkg_dir_path = path.join("pkg"); fs::create_dir_all(&out_dir)?;
fs::create_dir_all(pkg_dir_path)?;
Ok(()) Ok(())
} }

@ -146,6 +146,7 @@ impl CargoManifest {
/// Generate a package.json file inside in `./pkg`. /// Generate a package.json file inside in `./pkg`.
pub fn write_package_json( pub fn write_package_json(
path: &Path, path: &Path,
out_dir: &Path,
scope: &Option<String>, scope: &Option<String>,
disable_dts: bool, disable_dts: bool,
target: &str, target: &str,
@ -161,7 +162,7 @@ pub fn write_package_json(
}; };
PBAR.step(step, &msg); PBAR.step(step, &msg);
let pkg_file_path = path.join("pkg").join("package.json"); let pkg_file_path = out_dir.join("package.json");
let mut pkg_file = File::create(pkg_file_path)?; let mut pkg_file = File::create(pkg_file_path)?;
let crate_data = read_cargo_toml(path)?; let crate_data = read_cargo_toml(path)?;
let npm_data = crate_data.into_npm(scope, disable_dts, target); let npm_data = crate_data.into_npm(scope, disable_dts, target);

@ -9,22 +9,20 @@ use progressbar::Step;
use PBAR; use PBAR;
/// Copy the crate's README into the `pkg` directory. /// Copy the crate's README into the `pkg` directory.
pub fn copy_from_crate(path: &Path, step: &Step) -> Result<(), Error> { pub fn copy_from_crate(path: &Path, out_dir: &Path, step: &Step) -> Result<(), Error> {
assert!( assert!(
fs::metadata(path).ok().map_or(false, |m| m.is_dir()), fs::metadata(path).ok().map_or(false, |m| m.is_dir()),
"crate directory should exist" "crate directory should exist"
); );
assert!( assert!(
fs::metadata(path.join("pkg")) fs::metadata(&out_dir).ok().map_or(false, |m| m.is_dir()),
.ok()
.map_or(false, |m| m.is_dir()),
"crate's pkg directory should exist" "crate's pkg directory should exist"
); );
let msg = format!("{}Copying over your README...", emoji::DANCERS); let msg = format!("{}Copying over your README...", emoji::DANCERS);
PBAR.step(step, &msg); PBAR.step(step, &msg);
let crate_readme_path = path.join("README.md"); let crate_readme_path = path.join("README.md");
let new_readme_path = path.join("pkg").join("README.md"); let new_readme_path = out_dir.join("README.md");
if let Err(_) = fs::copy(&crate_readme_path, &new_readme_path) { if let Err(_) = fs::copy(&crate_readme_path, &new_readme_path) {
PBAR.warn("origin crate has no README"); PBAR.warn("origin crate has no README");
}; };

@ -54,13 +54,14 @@ fn it_recognizes_a_map_during_depcheck() {
#[test] #[test]
fn it_creates_a_package_json_default_path() { fn it_creates_a_package_json_default_path() {
let fixture = fixture::fixture("."); let fixture = fixture::fixture(".");
let out_dir = fixture.path.join("pkg");
let step = wasm_pack::progressbar::Step::new(1); let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap(); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &None, false, "", &step).is_ok()); assert!(manifest::write_package_json(&fixture.path, &out_dir, &None, false, "", &step).is_ok());
let package_json_path = &fixture.path.join("pkg").join("package.json"); let package_json_path = &fixture.path.join("pkg").join("package.json");
assert!(fs::metadata(package_json_path).is_ok()); assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path).is_ok()); assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path).unwrap(); let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "wasm-pack"); assert_eq!(pkg.name, "wasm-pack");
assert_eq!(pkg.repository.ty, "git"); assert_eq!(pkg.repository.ty, "git");
assert_eq!( assert_eq!(
@ -82,13 +83,14 @@ fn it_creates_a_package_json_default_path() {
#[test] #[test]
fn it_creates_a_package_json_provided_path() { fn it_creates_a_package_json_provided_path() {
let fixture = fixture::fixture("tests/fixtures/js-hello-world"); let fixture = fixture::fixture("tests/fixtures/js-hello-world");
let out_dir = fixture.path.join("pkg");
let step = wasm_pack::progressbar::Step::new(1); let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap(); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &None, false, "", &step).is_ok()); assert!(manifest::write_package_json(&fixture.path, &out_dir, &None, false, "", &step).is_ok());
let package_json_path = &fixture.path.join("pkg").join("package.json"); let package_json_path = &fixture.path.join("pkg").join("package.json");
assert!(fs::metadata(package_json_path).is_ok()); assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path).is_ok()); assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path).unwrap(); let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "js-hello-world"); assert_eq!(pkg.name, "js-hello-world");
assert_eq!(pkg.main, "js_hello_world.js"); assert_eq!(pkg.main, "js_hello_world.js");
@ -103,16 +105,23 @@ fn it_creates_a_package_json_provided_path() {
#[test] #[test]
fn it_creates_a_package_json_provided_path_with_scope() { fn it_creates_a_package_json_provided_path_with_scope() {
let fixture = fixture::fixture("tests/fixtures/scopes"); let fixture = fixture::fixture("tests/fixtures/scopes");
let out_dir = fixture.path.join("pkg");
let step = wasm_pack::progressbar::Step::new(1); let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap(); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
assert!( assert!(
manifest::write_package_json(&fixture.path, &Some("test".to_string()), false, "", &step) manifest::write_package_json(
.is_ok() &fixture.path,
&out_dir,
&Some("test".to_string()),
false,
"",
&step
).is_ok()
); );
let package_json_path = &fixture.path.join("pkg").join("package.json"); let package_json_path = &fixture.path.join("pkg").join("package.json");
assert!(fs::metadata(package_json_path).is_ok()); assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path).is_ok()); assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path).unwrap(); let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "@test/scopes-hello-world"); assert_eq!(pkg.name, "@test/scopes-hello-world");
assert_eq!(pkg.main, "scopes_hello_world.js"); assert_eq!(pkg.main, "scopes_hello_world.js");
@ -127,13 +136,17 @@ fn it_creates_a_package_json_provided_path_with_scope() {
#[test] #[test]
fn it_creates_a_pkg_json_with_correct_files_on_node() { fn it_creates_a_pkg_json_with_correct_files_on_node() {
let fixture = fixture::fixture("."); let fixture = fixture::fixture(".");
let out_dir = fixture.path.join("pkg");
let step = wasm_pack::progressbar::Step::new(1); let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap(); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &None, false, "nodejs", &step).is_ok()); assert!(
let package_json_path = &fixture.path.join("pkg").join("package.json"); manifest::write_package_json(&fixture.path, &out_dir, &None, false, "nodejs", &step)
.is_ok()
);
let package_json_path = &out_dir.join("package.json");
assert!(fs::metadata(package_json_path).is_ok()); assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path).is_ok()); assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path).unwrap(); let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "wasm-pack"); assert_eq!(pkg.name, "wasm-pack");
assert_eq!(pkg.repository.ty, "git"); assert_eq!(pkg.repository.ty, "git");
assert_eq!( assert_eq!(
@ -153,16 +166,43 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() {
assert_eq!(actual_files, expected_files); assert_eq!(actual_files, expected_files);
} }
#[test]
fn it_creates_a_pkg_json_in_out_dir() {
let fixture = fixture::fixture("tests/fixtures/js-hello-world");
let out_dir = fixture.path.join("./custom/out");
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &out_dir, &None, false, "", &step).is_ok());
let package_json_path = &fixture.path.join(&out_dir).join("package.json");
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "js-hello-world");
assert_eq!(pkg.main, "js_hello_world.js");
let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = ["js_hello_world_bg.wasm", "js_hello_world.d.ts"]
.iter()
.map(|&s| String::from(s))
.collect();
assert_eq!(actual_files, expected_files);
}
#[test] #[test]
fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() { fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() {
let fixture = fixture::fixture("."); let fixture = fixture::fixture(".");
let out_dir = fixture.path.join("pkg");
let step = wasm_pack::progressbar::Step::new(1); let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap(); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &None, true, "", &step).is_ok()); assert!(manifest::write_package_json(&fixture.path, &out_dir, &None, true, "", &step).is_ok());
let package_json_path = &fixture.path.join("pkg").join("package.json"); let package_json_path = &out_dir.join("package.json");
assert!(fs::metadata(package_json_path).is_ok()); assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path).is_ok()); assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path).unwrap(); let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "wasm-pack"); assert_eq!(pkg.name, "wasm-pack");
assert_eq!(pkg.repository.ty, "git"); assert_eq!(pkg.repository.ty, "git");
assert_eq!( assert_eq!(

@ -9,13 +9,14 @@ use wasm_pack::readme;
#[test] #[test]
fn it_copies_a_readme_default_path() { fn it_copies_a_readme_default_path() {
let fixture = fixture::fixture("."); let fixture = fixture::fixture(".");
fs::create_dir(fixture.path.join("pkg")).expect("should create pkg directory OK"); let out_dir = fixture.path.join("pkg");
fs::create_dir(&out_dir).expect("should create pkg directory OK");
let step = wasm_pack::progressbar::Step::new(1); let step = wasm_pack::progressbar::Step::new(1);
assert!(readme::copy_from_crate(&fixture.path, &step).is_ok()); assert!(readme::copy_from_crate(&fixture.path, &out_dir, &step).is_ok());
let crate_readme_path = fixture.path.join("README.md"); let crate_readme_path = fixture.path.join("README.md");
let pkg_readme_path = fixture.path.join("pkg").join("README.md"); let pkg_readme_path = out_dir.join("README.md");
println!( println!(
"wasm-pack: should have copied README.md from '{}' to '{}'", "wasm-pack: should have copied README.md from '{}' to '{}'",
crate_readme_path.display(), crate_readme_path.display(),
@ -33,12 +34,13 @@ fn it_copies_a_readme_default_path() {
#[test] #[test]
fn it_creates_a_package_json_provided_path() { fn it_creates_a_package_json_provided_path() {
let fixture = fixture::fixture("tests/fixtures/js-hello-world"); let fixture = fixture::fixture("tests/fixtures/js-hello-world");
fs::create_dir(fixture.path.join("pkg")).expect("should create pkg directory OK"); let out_dir = fixture.path.join("pkg");
fs::create_dir(&out_dir).expect("should create pkg directory OK");
let step = wasm_pack::progressbar::Step::new(1); let step = wasm_pack::progressbar::Step::new(1);
assert!(readme::copy_from_crate(&fixture.path, &step).is_ok()); assert!(readme::copy_from_crate(&fixture.path, &out_dir, &step).is_ok());
let crate_readme_path = fixture.path.join("README.md"); let crate_readme_path = fixture.path.join("README.md");
let pkg_readme_path = fixture.path.join("pkg").join("README.md"); let pkg_readme_path = out_dir.join("README.md");
println!( println!(
"wasm-pack: should have copied README.md from '{}' to '{}'", "wasm-pack: should have copied README.md from '{}' to '{}'",
crate_readme_path.display(), crate_readme_path.display(),

@ -24,8 +24,8 @@ pub struct Repository {
pub url: String, pub url: String,
} }
pub fn read_package_json(path: &Path) -> Result<NpmPackage, Error> { pub fn read_package_json(path: &Path, out_dir: &Path) -> Result<NpmPackage, Error> {
let manifest_path = path.join("pkg").join("package.json"); let manifest_path = path.join(out_dir).join("package.json");
let mut pkg_file = File::open(manifest_path)?; let mut pkg_file = File::open(manifest_path)?;
let mut pkg_contents = String::new(); let mut pkg_contents = String::new();
pkg_file.read_to_string(&mut pkg_contents)?; pkg_file.read_to_string(&mut pkg_contents)?;

Loading…
Cancel
Save