Skip to content

Commit 40a5e66

Browse files
Merge pull request #95 from ashleygwilliams/index-main
feat(manifest): add main entry
2 parents 5641d5c + 6a2a20d commit 40a5e66

File tree

9 files changed

+22
-16
lines changed

9 files changed

+22
-16
lines changed

src/bindgen.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use PBAR;
21
use console::style;
32
use emoji;
43
use failure::Error;
4+
use std::fs;
55
use std::process::Command;
6+
use PBAR;
67

78
pub fn cargo_install_wasm_bindgen() -> Result<(), Error> {
89
let step = format!(
@@ -13,11 +14,15 @@ pub fn cargo_install_wasm_bindgen() -> Result<(), Error> {
1314
let pb = PBAR.message(&step);
1415
let output = Command::new("cargo")
1516
.arg("install")
16-
.arg("wasm-bindgen")
17+
.arg("wasm-bindgen-cli")
1718
.output()?;
1819
pb.finish();
1920
if !output.status.success() {
2021
let s = String::from_utf8_lossy(&output.stderr);
22+
if s.contains("already exists") {
23+
PBAR.one_off_message("wasm-bindgen already installed");
24+
return Ok(());
25+
}
2126
PBAR.error("Installing wasm-bindgen failed");
2227
bail!(format!("Details:\n{}", s));
2328
} else {
@@ -46,6 +51,9 @@ pub fn wasm_bindgen_build(path: &str, name: &str) -> Result<(), Error> {
4651
PBAR.error("wasm-bindgen failed to execute properly");
4752
bail!(format!("Details:\n{}", s));
4853
} else {
54+
let js_file = format!("{}/pkg/{}.js", path, binary_name);
55+
let index_file = format!("{}/pkg/index.js", path);
56+
fs::rename(&js_file, &index_file)?;
4957
Ok(())
5058
}
5159
}

src/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use PBAR;
21
use console::style;
32
use emoji;
43
use failure::Error;
54
use std::process::Command;
5+
use PBAR;
66

77
pub fn rustup_add_wasm_target() -> Result<(), Error> {
88
let step = format!(

src/command.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use PBAR;
21
use bindgen;
32
use build;
43
use console::style;
@@ -13,6 +12,7 @@ use readme;
1312
use std::fs;
1413
use std::result;
1514
use std::time::Instant;
15+
use PBAR;
1616

1717
#[derive(Debug, StructOpt)]
1818
pub enum Command {

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ extern crate indicatif;
55
extern crate quicli;
66

77
use quicli::prelude::*;
8-
use wasm_pack::Cli;
98
use wasm_pack::command::run_wasm_pack;
9+
use wasm_pack::Cli;
1010

1111
main!(|args: Cli, log_level: verbosity| {
1212
run_wasm_pack(args.cmd)?;

src/manifest.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use std::fs::File;
22
use std::io::prelude::*;
33

4-
use PBAR;
54
use console::style;
65
use emoji;
76
use failure::Error;
87
use serde_json;
98
use toml;
9+
use PBAR;
1010

1111
#[derive(Deserialize)]
1212
struct CargoManifest {
@@ -32,6 +32,7 @@ struct NpmPackage {
3232
license: Option<String>,
3333
repository: Option<Repository>,
3434
files: Vec<String>,
35+
main: String,
3536
}
3637

3738
#[derive(Serialize)]
@@ -53,7 +54,6 @@ fn read_cargo_toml(path: &str) -> Result<CargoManifest, Error> {
5354
impl CargoManifest {
5455
fn into_npm(mut self, scope: Option<String>) -> NpmPackage {
5556
let filename = self.package.name.replace("-", "_");
56-
let js_file = format!("{}.js", filename);
5757
let wasm_file = format!("{}_bg.wasm", filename);
5858
if let Some(s) = scope {
5959
self.package.name = format!("@{}/{}", s, self.package.name);
@@ -68,7 +68,8 @@ impl CargoManifest {
6868
ty: "git".to_string(),
6969
url: repo_url,
7070
}),
71-
files: vec![js_file, wasm_file],
71+
files: vec![wasm_file],
72+
main: "index.js".to_string(),
7273
}
7374
}
7475
}

src/npm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use PBAR;
21
use failure::Error;
32
use std::process::Command;
3+
use PBAR;
44

55
pub fn npm_pack(path: &str) -> Result<(), Error> {
66
let pkg_file_path = format!("{}/pkg", path);

src/readme.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use console::style;
22
use failure::Error;
33
use std::fs;
44

5-
use PBAR;
65
use emoji;
6+
use PBAR;
77

88
pub fn copy_from_crate(path: &str) -> Result<(), Error> {
99
let step = format!(

tests/manifest/main.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ fn it_creates_a_package_json_default_path() {
4040
pkg.repository.url,
4141
"https://github.com/ashleygwilliams/wasm-pack.git"
4242
);
43-
assert_eq!(pkg.files, ["wasm_pack.js", "wasm_pack_bg.wasm"]);
43+
assert_eq!(pkg.files, ["wasm_pack_bg.wasm"]);
44+
assert_eq!(pkg.main, "index.js");
4445
}
4546

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

5959
#[test]
@@ -66,8 +66,4 @@ fn it_creates_a_package_json_provided_path_with_scope() {
6666
assert!(utils::read_package_json(&path).is_ok());
6767
let pkg = utils::read_package_json(&path).unwrap();
6868
assert_eq!(pkg.name, "@test/scopes-hello-world");
69-
assert_eq!(
70-
pkg.files,
71-
["scopes_hello_world.js", "scopes_hello_world_bg.wasm"]
72-
);
7369
}

tests/manifest/utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub struct NpmPackage {
1212
pub license: String,
1313
pub repository: Repository,
1414
pub files: Vec<String>,
15+
pub main: String,
1516
}
1617

1718
#[derive(Deserialize)]

0 commit comments

Comments
 (0)