Repository: ashleygwilliams/wasm-pack (rust)

Issue:
Declare a "main file" or index.js in the generated module
In the wasm-add example, the main JS file in the generated module is `wasm-add/wasm_add.js`. This means that you have to specify the JS filename when importing that module e.g. through webpack.

If the generated JS file were called `index.js` instead, or if its name was declared as the main file using [the `main` property in the module's package.json](https://docs.npmjs.com/files/package.json#main), one would be able to use just `import("wasm-add")` to import the module, which would be a bit nicer.

This would also allow the "test in your browser" button on the npm package site to work more smoothly. For example, if you click that button on [https://www.npmjs.com/package/@mgattozzi/wasm-add](https://www.npmjs.com/package/@mgattozzi/wasm-add), you get to [https://npm.runkit.com/@mgattozzi/wasm-add](https://npm.runkit.com/@mgattozzi/wasm-add), which says:

> `@mgattozzi/wasm-add` lists no main file and has no index.js, so it can't be directly required. If this is a mistake, please let us know. It may however contain internal files that you can require manually


Pull request:
feat(manifest): add main entry
fixes #94 

this PR does a few things:

- adds a step to the `wasm_bindgen` run that renames the generated js file to `index.js`
- adds a `main` entry to the `package.json` that points to `index.js`
- removes the js file from the files entry since it's preservation in the package is cover by being in the `main` entry
- removes redundant/unnecessary checks on `package.json` contents in some tests

Patch:
diff --git a/src/bindgen.rs b/src/bindgen.rs
index 3d8599a..12ad10f 100644
--- a/src/bindgen.rs
+++ b/src/bindgen.rs
@@ -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!(
@@ -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 {
@@ -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(())
     }
 }
diff --git a/src/build.rs b/src/build.rs
index 1422f2f..dfcb299 100644
--- a/src/build.rs
+++ b/src/build.rs
@@ -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!(
diff --git a/src/command.rs b/src/command.rs
index 1d0179e..488c127 100644
--- a/src/command.rs
+++ b/src/command.rs
@@ -1,4 +1,3 @@
-use PBAR;
 use bindgen;
 use build;
 use console::style;
@@ -13,6 +12,7 @@ use readme;
 use std::fs;
 use std::result;
 use std::time::Instant;
+use PBAR;
 
 #[derive(Debug, StructOpt)]
 pub enum Command {
diff --git a/src/main.rs b/src/main.rs
index b431d38..26a5d9d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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)?;
diff --git a/src/manifest.rs b/src/manifest.rs
index c60583a..9f60435 100644
--- a/src/manifest.rs
+++ b/src/manifest.rs
@@ -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 {
@@ -32,6 +32,7 @@ struct NpmPackage {
     license: Option<String>,
     repository: Option<Repository>,
     files: Vec<String>,
+    main: String,
 }
 
 #[derive(Serialize)]
@@ -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);
@@ -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(),
         }
     }
 }
diff --git a/src/npm.rs b/src/npm.rs
index 6f545a7..5c26e35 100644
--- a/src/npm.rs
+++ b/src/npm.rs
@@ -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);
diff --git a/src/readme.rs b/src/readme.rs
index b050306..fa9d4d3 100644
--- a/src/readme.rs
+++ b/src/readme.rs
@@ -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!(


Test patch:
diff --git a/tests/manifest/main.rs b/tests/manifest/main.rs
index 2ef3a6a..6b4ca9e 100644
--- a/tests/manifest/main.rs
+++ b/tests/manifest/main.rs
@@ -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]
@@ -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]
@@ -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"]
-    );
 }
diff --git a/tests/manifest/utils.rs b/tests/manifest/utils.rs
index 5b83909..aa97d98 100644
--- a/tests/manifest/utils.rs
+++ b/tests/manifest/utils.rs
@@ -12,6 +12,7 @@ pub struct NpmPackage {
     pub license: String,
     pub repository: Repository,
     pub files: Vec<String>,
+    pub main: String,
 }
 
 #[derive(Deserialize)]