Skip to content

Commit 8102538

Browse files
Merge pull request #109 from kwonoj/feat-typescript-bindgen
feat(command): expose --no-typescript option to init command
2 parents 97d5a04 + e6b5ca5 commit 8102538

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed

src/bindgen.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn cargo_install_wasm_bindgen() -> Result<(), Error> {
2828
}
2929
}
3030

31-
pub fn wasm_bindgen_build(path: &str, name: &str) -> Result<(), Error> {
31+
pub fn wasm_bindgen_build(path: &str, name: &str, disable_dts: bool) -> Result<(), Error> {
3232
let step = format!(
3333
"{} {}Running WASM-bindgen...",
3434
style("[7/7]").bold().dim(),
@@ -37,11 +37,18 @@ pub fn wasm_bindgen_build(path: &str, name: &str) -> Result<(), Error> {
3737
let pb = PBAR.message(&step);
3838
let binary_name = name.replace("-", "_");
3939
let wasm_path = format!("target/wasm32-unknown-unknown/release/{}.wasm", binary_name);
40+
let dts_arg = if disable_dts == false {
41+
"--typescript"
42+
} else {
43+
"--no-typescript"
44+
};
45+
4046
let output = Command::new("wasm-bindgen")
4147
.current_dir(path)
4248
.arg(&wasm_path)
4349
.arg("--out-dir")
4450
.arg("./pkg")
51+
.arg(dts_arg)
4552
.output()?;
4653
pb.finish();
4754
if !output.status.success() {

src/command.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ pub enum Command {
2222
path: Option<String>,
2323
#[structopt(long = "scope", short = "s")]
2424
scope: Option<String>,
25+
#[structopt(long = "no-typescript")]
26+
disable_dts: bool,
2527
},
2628

2729
#[structopt(name = "pack")]
@@ -61,7 +63,11 @@ pub fn run_wasm_pack(command: Command) -> result::Result<(), Error> {
6163
// Run the correct command based off input and store the result of it so that we can clear
6264
// the progress bar then return it
6365
let status = match command {
64-
Command::Init { path, scope } => init(path, scope),
66+
Command::Init {
67+
path,
68+
scope,
69+
disable_dts,
70+
} => init(path, scope, disable_dts),
6571
Command::Pack { path } => pack(path),
6672
Command::Publish { path } => publish(path),
6773
Command::Login {
@@ -104,19 +110,23 @@ pub fn create_pkg_dir(path: &str) -> result::Result<(), Error> {
104110
Ok(())
105111
}
106112

107-
fn init(path: Option<String>, scope: Option<String>) -> result::Result<(), Error> {
113+
fn init(
114+
path: Option<String>,
115+
scope: Option<String>,
116+
disable_dts: bool,
117+
) -> result::Result<(), Error> {
108118
let started = Instant::now();
109119

110120
let crate_path = set_crate_path(path);
111121

112122
build::rustup_add_wasm_target()?;
113123
build::cargo_build_wasm(&crate_path)?;
114124
create_pkg_dir(&crate_path)?;
115-
manifest::write_package_json(&crate_path, scope)?;
125+
manifest::write_package_json(&crate_path, scope, disable_dts)?;
116126
readme::copy_from_crate(&crate_path)?;
117127
bindgen::cargo_install_wasm_bindgen()?;
118128
let name = manifest::get_crate_name(&crate_path)?;
119-
bindgen::wasm_bindgen_build(&crate_path, &name)?;
129+
bindgen::wasm_bindgen_build(&crate_path, &name, disable_dts)?;
120130
PBAR.message(&format!(
121131
"{} Done in {}",
122132
emoji::SPARKLE,

src/manifest.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct NpmPackage {
3333
repository: Option<Repository>,
3434
files: Vec<String>,
3535
main: String,
36+
types: Option<String>,
3637
}
3738

3839
#[derive(Serialize)]
@@ -52,10 +53,16 @@ fn read_cargo_toml(path: &str) -> Result<CargoManifest, Error> {
5253
}
5354

5455
impl CargoManifest {
55-
fn into_npm(mut self, scope: Option<String>) -> NpmPackage {
56+
fn into_npm(mut self, scope: Option<String>, disable_dts: bool) -> NpmPackage {
5657
let filename = self.package.name.replace("-", "_");
5758
let wasm_file = format!("{}_bg.wasm", filename);
5859
let js_file = format!("{}.js", filename);
60+
let dts_file = if disable_dts == true {
61+
None
62+
} else {
63+
Some(format!("{}.d.ts", filename))
64+
};
65+
5966
if let Some(s) = scope {
6067
self.package.name = format!("@{}/{}", s, self.package.name);
6168
}
@@ -71,12 +78,17 @@ impl CargoManifest {
7178
}),
7279
files: vec![wasm_file],
7380
main: js_file,
81+
types: dts_file,
7482
}
7583
}
7684
}
7785

7886
/// Generate a package.json file inside in `./pkg`.
79-
pub fn write_package_json(path: &str, scope: Option<String>) -> Result<(), Error> {
87+
pub fn write_package_json(
88+
path: &str,
89+
scope: Option<String>,
90+
disable_dts: bool,
91+
) -> Result<(), Error> {
8092
let step = format!(
8193
"{} {}Writing a package.json...",
8294
style("[4/7]").bold().dim(),
@@ -94,7 +106,7 @@ pub fn write_package_json(path: &str, scope: Option<String>) -> Result<(), Error
94106
let pkg_file_path = format!("{}/pkg/package.json", path);
95107
let mut pkg_file = File::create(pkg_file_path)?;
96108
let crate_data = read_cargo_toml(path)?;
97-
let npm_data = crate_data.into_npm(scope);
109+
let npm_data = crate_data.into_npm(scope, disable_dts);
98110

99111
if npm_data.description.is_none() {
100112
PBAR.warn(&warn_fmt("description"));

tests/manifest/main.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn it_gets_the_crate_name_provided_path() {
2929
fn it_creates_a_package_json_default_path() {
3030
let path = ".".to_string();
3131
wasm_pack::command::create_pkg_dir(&path).unwrap();
32-
assert!(manifest::write_package_json(&path, None).is_ok());
32+
assert!(manifest::write_package_json(&path, None, false).is_ok());
3333
let package_json_path = format!("{}/pkg/package.json", &path);
3434
assert!(fs::metadata(package_json_path).is_ok());
3535
assert!(utils::read_package_json(&path).is_ok());
@@ -42,13 +42,15 @@ fn it_creates_a_package_json_default_path() {
4242
);
4343
assert_eq!(pkg.files, ["wasm_pack_bg.wasm"]);
4444
assert_eq!(pkg.main, "wasm_pack.js");
45+
let types = pkg.types.unwrap_or_default();
46+
assert_eq!(types, "wasm_pack.d.ts");
4547
}
4648

4749
#[test]
4850
fn it_creates_a_package_json_provided_path() {
4951
let path = "tests/fixtures/js-hello-world".to_string();
5052
wasm_pack::command::create_pkg_dir(&path).unwrap();
51-
assert!(manifest::write_package_json(&path, None).is_ok());
53+
assert!(manifest::write_package_json(&path, None, false).is_ok());
5254
let package_json_path = format!("{}/pkg/package.json", &path);
5355
assert!(fs::metadata(package_json_path).is_ok());
5456
assert!(utils::read_package_json(&path).is_ok());
@@ -60,7 +62,7 @@ fn it_creates_a_package_json_provided_path() {
6062
fn it_creates_a_package_json_provided_path_with_scope() {
6163
let path = "tests/fixtures/scopes".to_string();
6264
wasm_pack::command::create_pkg_dir(&path).unwrap();
63-
assert!(manifest::write_package_json(&path, Some("test".to_string())).is_ok());
65+
assert!(manifest::write_package_json(&path, Some("test".to_string()), false).is_ok());
6466
let package_json_path = format!("{}/pkg/package.json", &path);
6567
assert!(fs::metadata(package_json_path).is_ok());
6668
assert!(utils::read_package_json(&path).is_ok());

tests/manifest/utils.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct NpmPackage {
1313
pub repository: Repository,
1414
pub files: Vec<String>,
1515
pub main: String,
16+
pub types: Option<String>,
1617
}
1718

1819
#[derive(Deserialize)]

0 commit comments

Comments
 (0)