Skip to content

Commit 75db2a8

Browse files
committed
feat(command): expose --no-typescript option to init command
- relates to #107
1 parent 26c8b8d commit 75db2a8

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

src/bindgen.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn cargo_install_wasm_bindgen() -> Result<(), Error> {
2929
}
3030
}
3131

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

src/command.rs

Lines changed: 14 additions & 4 deletions
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
#[structopt(name = "pack")]
2729
/// 🍱 create a tar of your npm package but don't publish! [NOT IMPLEMENTED]
@@ -35,7 +37,11 @@ pub fn run_wasm_pack(command: Command) -> result::Result<(), Error> {
3537
// Run the correct command based off input and store the result of it so that we can clear
3638
// the progress bar then return it
3739
let status = match command {
38-
Command::Init { path, scope } => init(path, scope),
40+
Command::Init {
41+
path,
42+
scope,
43+
disable_dts,
44+
} => init(path, scope, disable_dts),
3945
Command::Pack { path } => pack(path),
4046
Command::Publish { path } => publish(path),
4147
};
@@ -65,19 +71,23 @@ pub fn create_pkg_dir(path: &str) -> result::Result<(), Error> {
6571
Ok(())
6672
}
6773

68-
fn init(path: Option<String>, scope: Option<String>) -> result::Result<(), Error> {
74+
fn init(
75+
path: Option<String>,
76+
scope: Option<String>,
77+
disable_dts: bool,
78+
) -> result::Result<(), Error> {
6979
let started = Instant::now();
7080

7181
let crate_path = set_crate_path(path);
7282

7383
build::rustup_add_wasm_target()?;
7484
build::cargo_build_wasm(&crate_path)?;
7585
create_pkg_dir(&crate_path)?;
76-
manifest::write_package_json(&crate_path, scope)?;
86+
manifest::write_package_json(&crate_path, scope, disable_dts)?;
7787
readme::copy_from_crate(&crate_path)?;
7888
bindgen::cargo_install_wasm_bindgen()?;
7989
let name = manifest::get_crate_name(&crate_path)?;
80-
bindgen::wasm_bindgen_build(&crate_path, &name)?;
90+
bindgen::wasm_bindgen_build(&crate_path, &name, disable_dts)?;
8191
PBAR.message(&format!(
8292
"{} Done in {}",
8393
emoji::SPARKLE,

src/manifest.rs

Lines changed: 15 additions & 3 deletions
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"));

0 commit comments

Comments
 (0)