diff --git a/Cargo.lock b/Cargo.lock index 68c8b34..6222abf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -543,8 +543,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "wasm-pack" version = "0.1.0" dependencies = [ + "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "quicli 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/Cargo.toml b/Cargo.toml index f5c6536..32cb53f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,9 @@ version = "0.1.0" authors = ["Ashley Williams "] [dependencies] +failure = "0.1.1" quicli = "0.2" serde = "1.0" +serde_derive = "1.0" serde_json = "1.0" toml = "0.4" diff --git a/package.json b/package.json deleted file mode 100644 index cb7ca0f..0000000 --- a/package.json +++ /dev/null @@ -1 +0,0 @@ -{"name":"wasm-pack","description":"pack up the wasm and publish it to npm!","version":"0.1.0"} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..3696813 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,56 @@ +#[macro_use] +extern crate serde_derive; +extern crate serde_json; +extern crate toml; +extern crate failure; + +use std::fs::File; +use std::io::prelude::*; + +use failure::Error; + +#[derive(Deserialize)] +struct CargoManifest { + package: CargoPackage, +} + +#[derive(Deserialize)] +struct CargoPackage { + name: String, + description: String, + version: String, +} + +#[derive(Serialize)] +struct NpmPackage { + name: String, + description: String, + version: String, +} + +fn read_cargo_toml() -> Result { + let mut cargo_file = File::open("Cargo.toml")?; + let mut cargo_contents = String::new(); + cargo_file.read_to_string(&mut cargo_contents)?; + + Ok(toml::from_str(&cargo_contents)?) +} + +impl CargoManifest { + fn into_npm(self) -> NpmPackage { + NpmPackage { + name: self.package.name, + description: self.package.description, + version: self.package.version, + } + } +} + +pub fn write_package_json() -> Result<(), Error> { + let mut pkg_file = File::create("package.json")?; + let crate_data = read_cargo_toml()?; + let npm_data = crate_data.into_npm(); + let npm_json = serde_json::to_string(&npm_data)?; + pkg_file.write_all(npm_json.as_bytes())?; + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index d3494af..31c392a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,9 @@ #[macro_use] extern crate quicli; -extern crate serde_json; -extern crate toml; +extern crate wasm_pack; use quicli::prelude::*; -use std::fs::File; -use std::io::prelude::*; - /// 📦 ✨ pack and publish your wasm! #[derive(Debug, StructOpt)] struct Cli { @@ -31,55 +27,9 @@ enum Command { Publish {}, } -#[derive(Deserialize)] -pub struct CargoManifest { - package: CargoPackage, -} - -#[derive(Deserialize)] -pub struct CargoPackage { - name: String, - description: String, - version: String, -} - -#[derive(Serialize)] -pub struct NpmPackage { - name: String, - description: String, - version: String, -} - -fn read_cargo_toml() -> Result { - let mut cargo_file = File::open("Cargo.toml")?; - let mut cargo_contents = String::new(); - cargo_file.read_to_string(&mut cargo_contents)?; - - Ok(toml::from_str(&cargo_contents)?) -} - -impl CargoManifest { - fn into_npm(self) -> NpmPackage { - NpmPackage { - name: self.package.name, - description: self.package.description, - version: self.package.version, - } - } -} - -fn write_package_json() -> Result<()> { - let mut pkg_file = File::create("package.json")?; - let crate_data = read_cargo_toml()?; - let npm_data = crate_data.into_npm(); - let npm_json = serde_json::to_string(&npm_data)?; - pkg_file.write_all(npm_json.as_bytes())?; - Ok(()) -} - main!(|args: Cli, log_level: verbosity| match args.cmd { Command::Init { .. } => { - write_package_json()?; + wasm_pack::write_package_json()?; println!("✍️ wrote a package.json!"); } Command::Pack { .. } => { diff --git a/tests/wasm-pack.rs b/tests/wasm-pack.rs new file mode 100644 index 0000000..8093d18 --- /dev/null +++ b/tests/wasm-pack.rs @@ -0,0 +1,9 @@ +extern crate wasm_pack; + +use std::fs; + +#[test] +fn it_creates_a_package_json() { + assert!(wasm_pack::write_package_json().is_ok()); + assert!(fs::metadata("./package.json").is_ok()); +}