Skip to content

Commit

Permalink
Merge pull request rustwasm#95 from ashleygwilliams/index-main
Browse files Browse the repository at this point in the history
feat(manifest): add main entry
  • Loading branch information
ashleygwilliams authored Apr 19, 2018
2 parents 5641d5c + 6a2a20d commit 40a5e66
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 16 deletions.
12 changes: 10 additions & 2 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use PBAR;
use console::style;
use emoji;
use failure::Error;
use std::fs;
use std::process::Command;
use PBAR;

pub fn cargo_install_wasm_bindgen() -> Result<(), Error> {
let step = format!(
Expand All @@ -13,11 +14,15 @@ pub fn cargo_install_wasm_bindgen() -> Result<(), Error> {
let pb = PBAR.message(&step);
let output = Command::new("cargo")
.arg("install")
.arg("wasm-bindgen")
.arg("wasm-bindgen-cli")
.output()?;
pb.finish();
if !output.status.success() {
let s = String::from_utf8_lossy(&output.stderr);
if s.contains("already exists") {
PBAR.one_off_message("wasm-bindgen already installed");
return Ok(());
}
PBAR.error("Installing wasm-bindgen failed");
bail!(format!("Details:\n{}", s));
} else {
Expand Down Expand Up @@ -46,6 +51,9 @@ pub fn wasm_bindgen_build(path: &str, name: &str) -> Result<(), Error> {
PBAR.error("wasm-bindgen failed to execute properly");
bail!(format!("Details:\n{}", s));
} else {
let js_file = format!("{}/pkg/{}.js", path, binary_name);
let index_file = format!("{}/pkg/index.js", path);
fs::rename(&js_file, &index_file)?;
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use PBAR;
use console::style;
use emoji;
use failure::Error;
use std::process::Command;
use PBAR;

pub fn rustup_add_wasm_target() -> Result<(), Error> {
let step = format!(
Expand Down
2 changes: 1 addition & 1 deletion src/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use PBAR;
use bindgen;
use build;
use console::style;
Expand All @@ -13,6 +12,7 @@ use readme;
use std::fs;
use std::result;
use std::time::Instant;
use PBAR;

#[derive(Debug, StructOpt)]
pub enum Command {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ extern crate indicatif;
extern crate quicli;

use quicli::prelude::*;
use wasm_pack::Cli;
use wasm_pack::command::run_wasm_pack;
use wasm_pack::Cli;

main!(|args: Cli, log_level: verbosity| {
run_wasm_pack(args.cmd)?;
Expand Down
7 changes: 4 additions & 3 deletions src/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::fs::File;
use std::io::prelude::*;

use PBAR;
use console::style;
use emoji;
use failure::Error;
use serde_json;
use toml;
use PBAR;

#[derive(Deserialize)]
struct CargoManifest {
Expand All @@ -32,6 +32,7 @@ struct NpmPackage {
license: Option<String>,
repository: Option<Repository>,
files: Vec<String>,
main: String,
}

#[derive(Serialize)]
Expand All @@ -53,7 +54,6 @@ fn read_cargo_toml(path: &str) -> Result<CargoManifest, Error> {
impl CargoManifest {
fn into_npm(mut self, scope: Option<String>) -> NpmPackage {
let filename = self.package.name.replace("-", "_");
let js_file = format!("{}.js", filename);
let wasm_file = format!("{}_bg.wasm", filename);
if let Some(s) = scope {
self.package.name = format!("@{}/{}", s, self.package.name);
Expand All @@ -68,7 +68,8 @@ impl CargoManifest {
ty: "git".to_string(),
url: repo_url,
}),
files: vec![js_file, wasm_file],
files: vec![wasm_file],
main: "index.js".to_string(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/npm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use PBAR;
use failure::Error;
use std::process::Command;
use PBAR;

pub fn npm_pack(path: &str) -> Result<(), Error> {
let pkg_file_path = format!("{}/pkg", path);
Expand Down
2 changes: 1 addition & 1 deletion src/readme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use console::style;
use failure::Error;
use std::fs;

use PBAR;
use emoji;
use PBAR;

pub fn copy_from_crate(path: &str) -> Result<(), Error> {
let step = format!(
Expand Down
8 changes: 2 additions & 6 deletions tests/manifest/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ fn it_creates_a_package_json_default_path() {
pkg.repository.url,
"https://github.com/ashleygwilliams/wasm-pack.git"
);
assert_eq!(pkg.files, ["wasm_pack.js", "wasm_pack_bg.wasm"]);
assert_eq!(pkg.files, ["wasm_pack_bg.wasm"]);
assert_eq!(pkg.main, "index.js");
}

#[test]
Expand All @@ -53,7 +54,6 @@ fn it_creates_a_package_json_provided_path() {
assert!(utils::read_package_json(&path).is_ok());
let pkg = utils::read_package_json(&path).unwrap();
assert_eq!(pkg.name, "js-hello-world");
assert_eq!(pkg.files, ["js_hello_world.js", "js_hello_world_bg.wasm"]);
}

#[test]
Expand All @@ -66,8 +66,4 @@ fn it_creates_a_package_json_provided_path_with_scope() {
assert!(utils::read_package_json(&path).is_ok());
let pkg = utils::read_package_json(&path).unwrap();
assert_eq!(pkg.name, "@test/scopes-hello-world");
assert_eq!(
pkg.files,
["scopes_hello_world.js", "scopes_hello_world_bg.wasm"]
);
}
1 change: 1 addition & 0 deletions tests/manifest/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct NpmPackage {
pub license: String,
pub repository: Repository,
pub files: Vec<String>,
pub main: String,
}

#[derive(Deserialize)]
Expand Down

0 comments on commit 40a5e66

Please sign in to comment.