Skip to content

Commit

Permalink
Merge pull request #263 from xmclark/cli-pkg-dir
Browse files Browse the repository at this point in the history
Specify output directory with CLI
  • Loading branch information
fitzgen authored Sep 4, 2018
2 parents 995a137 + 40b0291 commit 89be40d
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 58 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: rust
sudo: false
cache: cargo

DEPLOY_TO_GITHUB: &DEPLOY_TO_GITHUB
before_deploy:
Expand Down
5 changes: 4 additions & 1 deletion src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ pub fn cargo_install_wasm_bindgen(root_path: &Path, version: &str) -> Result<(),
/// `.wasm`.
pub fn wasm_bindgen_build(
path: &Path,
out_dir: &Path,
name: &str,
disable_dts: bool,
target: &str,
Expand All @@ -198,6 +199,8 @@ pub fn wasm_bindgen_build(
let binary_name = name.replace("-", "_");
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) {
let wasm_path = format!(
"target/wasm32-unknown-unknown/{}/{}.wasm",
Expand All @@ -217,7 +220,7 @@ pub fn wasm_bindgen_build(
.current_dir(path)
.arg(&wasm_path)
.arg("--out-dir")
.arg("./pkg")
.arg(out_dir)
.arg(dts_arg)
.arg(target_arg)
.output()?;
Expand Down
31 changes: 15 additions & 16 deletions src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub(crate) struct Build {
pub mode: BuildMode,
// build_config: Option<BuildConfig>,
pub crate_name: String,
pub out_dir: PathBuf,
}

/// The `BuildMode` determines which mode of initialization we are running, and
Expand Down Expand Up @@ -63,6 +64,9 @@ pub struct BuildOptions {
debug: bool,
// build config from manifest
// 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>;
Expand All @@ -72,6 +76,7 @@ impl Build {
pub fn try_from_opts(build_opts: BuildOptions) -> Result<Self, Error> {
let crate_path = set_crate_path(build_opts.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() {
"no-install" => BuildMode::Noinstall,
_ => BuildMode::Normal,
Expand All @@ -86,6 +91,7 @@ impl Build {
mode,
// build_config,
crate_name,
out_dir,
})
}

Expand All @@ -106,16 +112,15 @@ impl Build {
info!(&log, "Done in {}.", &duration);
info!(
&log,
"Your wasm pkg is ready to publish at {:#?}.",
&self.crate_path.join("pkg")
"Your wasm pkg is ready to publish at {:#?}.", &self.out_dir
);

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

PBAR.message(&format!(
"{} Your wasm pkg is ready to publish at {:#?}.",
emoji::PACKAGE,
&self.crate_path.join("pkg")
self.out_dir.canonicalize().unwrap_or(self.out_dir.clone())
));
Ok(())
}
Expand Down Expand Up @@ -185,7 +190,7 @@ impl Build {

fn step_create_dir(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
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);
Ok(())
}
Expand All @@ -194,6 +199,7 @@ impl Build {
info!(&log, "Writing a package.json...");
manifest::write_package_json(
&self.crate_path,
&self.out_dir,
&self.scope,
self.disable_dts,
&self.target,
Expand All @@ -202,19 +208,15 @@ impl Build {
info!(
&log,
"Wrote a package.json at {:#?}.",
&self.crate_path.join("pkg").join("package.json")
&self.out_dir.join("package.json")
);
Ok(())
}

fn step_copy_readme(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Copying readme from crate...");
readme::copy_from_crate(&self.crate_path, step)?;
info!(
&log,
"Copied readme from crate to {:#?}.",
&self.crate_path.join("pkg")
);
readme::copy_from_crate(&self.crate_path, &self.out_dir, step)?;
info!(&log, "Copied readme from crate to {:#?}.", &self.out_dir);
Ok(())
}

Expand Down Expand Up @@ -250,17 +252,14 @@ impl Build {
info!(&log, "Building the wasm bindings...");
bindgen::wasm_bindgen_build(
&self.crate_path,
&self.out_dir,
&self.crate_name,
self.disable_dts,
&self.target,
self.debug,
step,
)?;
info!(
&log,
"wasm bindings were built at {:#?}.",
&self.crate_path.join("pkg")
);
info!(&log, "wasm bindings were built at {:#?}.", &self.out_dir);
Ok(())
}
}
5 changes: 2 additions & 3 deletions src/command/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ pub fn set_crate_path(path: Option<PathBuf>) -> PathBuf {
}

/// 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);
PBAR.step(step, &msg);
let pkg_dir_path = path.join("pkg");
fs::create_dir_all(pkg_dir_path)?;
fs::create_dir_all(&out_dir)?;
Ok(())
}

Expand Down
3 changes: 2 additions & 1 deletion src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl CargoManifest {
/// Generate a package.json file inside in `./pkg`.
pub fn write_package_json(
path: &Path,
out_dir: &Path,
scope: &Option<String>,
disable_dts: bool,
target: &str,
Expand All @@ -161,7 +162,7 @@ pub fn write_package_json(
};

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 crate_data = read_cargo_toml(path)?;
let npm_data = crate_data.into_npm(scope, disable_dts, target);
Expand Down
8 changes: 3 additions & 5 deletions src/readme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,20 @@ use progressbar::Step;
use PBAR;

/// 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!(
fs::metadata(path).ok().map_or(false, |m| m.is_dir()),
"crate directory should exist"
);
assert!(
fs::metadata(path.join("pkg"))
.ok()
.map_or(false, |m| m.is_dir()),
fs::metadata(&out_dir).ok().map_or(false, |m| m.is_dir()),
"crate's pkg directory should exist"
);

let msg = format!("{}Copying over your README...", emoji::DANCERS);
PBAR.step(step, &msg);
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) {
PBAR.warn("origin crate has no README");
};
Expand Down
86 changes: 63 additions & 23 deletions tests/all/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ fn it_recognizes_a_map_during_depcheck() {
#[test]
fn it_creates_a_package_json_default_path() {
let fixture = fixture::fixture(".");
let out_dir = fixture.path.join("pkg");
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &None, false, "", &step).is_ok());
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("pkg").join("package.json");
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path).unwrap();
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, "wasm-pack");
assert_eq!(pkg.repository.ty, "git");
assert_eq!(
Expand All @@ -82,13 +83,14 @@ fn it_creates_a_package_json_default_path() {
#[test]
fn it_creates_a_package_json_provided_path() {
let fixture = fixture::fixture("tests/fixtures/js-hello-world");
let out_dir = fixture.path.join("pkg");
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &None, false, "", &step).is_ok());
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("pkg").join("package.json");
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path).unwrap();
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");

Expand All @@ -103,16 +105,23 @@ fn it_creates_a_package_json_provided_path() {
#[test]
fn it_creates_a_package_json_provided_path_with_scope() {
let fixture = fixture::fixture("tests/fixtures/scopes");
let out_dir = fixture.path.join("pkg");
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, &Some("test".to_string()), false, "", &step)
.is_ok()
manifest::write_package_json(
&fixture.path,
&out_dir,
&Some("test".to_string()),
false,
"",
&step
).is_ok()
);
let package_json_path = &fixture.path.join("pkg").join("package.json");
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path).unwrap();
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, "@test/scopes-hello-world");
assert_eq!(pkg.main, "scopes_hello_world.js");

Expand All @@ -127,13 +136,17 @@ fn it_creates_a_package_json_provided_path_with_scope() {
#[test]
fn it_creates_a_pkg_json_with_correct_files_on_node() {
let fixture = fixture::fixture(".");
let out_dir = fixture.path.join("pkg");
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &None, false, "nodejs", &step).is_ok());
let package_json_path = &fixture.path.join("pkg").join("package.json");
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
assert!(
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!(utils::manifest::read_package_json(&fixture.path).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path).unwrap();
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, "wasm-pack");
assert_eq!(pkg.repository.ty, "git");
assert_eq!(
Expand All @@ -153,16 +166,43 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() {
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]
fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() {
let fixture = fixture::fixture(".");
let out_dir = fixture.path.join("pkg");
let step = wasm_pack::progressbar::Step::new(1);
wasm_pack::command::utils::create_pkg_dir(&fixture.path, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &None, true, "", &step).is_ok());
let package_json_path = &fixture.path.join("pkg").join("package.json");
wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &out_dir, &None, true, "", &step).is_ok());
let package_json_path = &out_dir.join("package.json");
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path).unwrap();
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, "wasm-pack");
assert_eq!(pkg.repository.ty, "git");
assert_eq!(
Expand Down
14 changes: 8 additions & 6 deletions tests/all/readme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ use wasm_pack::readme;
#[test]
fn it_copies_a_readme_default_path() {
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);
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 pkg_readme_path = fixture.path.join("pkg").join("README.md");
let pkg_readme_path = out_dir.join("README.md");
println!(
"wasm-pack: should have copied README.md from '{}' to '{}'",
crate_readme_path.display(),
Expand All @@ -33,12 +34,13 @@ fn it_copies_a_readme_default_path() {
#[test]
fn it_creates_a_package_json_provided_path() {
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);
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 pkg_readme_path = fixture.path.join("pkg").join("README.md");
let pkg_readme_path = out_dir.join("README.md");
println!(
"wasm-pack: should have copied README.md from '{}' to '{}'",
crate_readme_path.display(),
Expand Down
4 changes: 2 additions & 2 deletions tests/all/utils/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub struct Repository {
pub url: String,
}

pub fn read_package_json(path: &Path) -> Result<NpmPackage, Error> {
let manifest_path = path.join("pkg").join("package.json");
pub fn read_package_json(path: &Path, out_dir: &Path) -> Result<NpmPackage, Error> {
let manifest_path = path.join(out_dir).join("package.json");
let mut pkg_file = File::open(manifest_path)?;
let mut pkg_contents = String::new();
pkg_file.read_to_string(&mut pkg_contents)?;
Expand Down

0 comments on commit 89be40d

Please sign in to comment.