Skip to content

Commit

Permalink
Merge pull request #62 from ashleygwilliams/mgattozzi-scopes
Browse files Browse the repository at this point in the history
mgattozzi scopes rollup
  • Loading branch information
ashleygwilliams authored Mar 15, 2018
2 parents d96a066 + 9ea956f commit cef406f
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 10 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ this project is written in rust. [get rust] to work on this project.
- `help`: display available commands
- 🐣 `init`: create necessary files for js interop and npm publishing
- optionally pass a path to a dir that contains a `Cargo.toml`, e.g.:
`wasm-pack init examples/js-hello-world`
```
wasm-pack init examples/js-hello-world
```
- optionally pass a scope name to generate a `package.json` for a scoped pkg, e.g.:
```
wasm-pack init examples/scopes-hello-world --scope test
```
generates a `package.json` for an npm package called `@test/scopes-hello-world`
#### to be implemented
- 🍱 `pack`: create a tarball but don't push to the npm registry [NOT IMPLEMENTED]
- 🎆 `publish`: create a tarball and publish to the npm registry [NOT IMPLEMENTED]
Expand Down Expand Up @@ -72,7 +81,7 @@ this project is written in rust. [get rust] to work on this project.
```

5. install this tool: `cargo install wasm-pack`
6. run `wasm-pack init`, optionally, pass a path to a dir that contains your `Cargo.toml`
6. run `wasm-pack init`, optionally, pass a path to a dir or a scope (see above for details)
7. this tool generates files in a `pkg` dir. to publish to npm, `cd pkg` and then `npm publish`
(in the future you'll be able to use this tool to publish)

Expand Down
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ struct Cli {
enum Command {
#[structopt(name = "init")]
/// 🐣 initialize a package.json based on your compiled wasm
Init { path: Option<String> },
Init {
path: Option<String>,
#[structopt(long = "scope", short = "s")]
scope: Option<String>,
},
#[structopt(name = "pack")]
/// 🍱 create a tar of your npm package but don't publish! [NOT IMPLEMENTED]
Pack {},
Expand All @@ -34,7 +38,7 @@ enum Command {
}

main!(|args: Cli, log_level: verbosity| match args.cmd {
Command::Init { path } => {
Command::Init { path, scope } => {
let started = Instant::now();

let crate_path = match path {
Expand All @@ -45,7 +49,7 @@ main!(|args: Cli, log_level: verbosity| match args.cmd {
build::rustup_add_wasm_target();
build::cargo_build_wasm(&crate_path);
wasm_pack::create_pkg_dir(&crate_path)?;
manifest::write_package_json(&crate_path)?;
manifest::write_package_json(&crate_path, scope)?;
readme::copy_from_crate(&crate_path)?;
bindgen::cargo_install_wasm_bindgen();
let name = manifest::get_crate_name(&crate_path)?;
Expand Down
9 changes: 6 additions & 3 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ fn read_cargo_toml(path: &str) -> Result<CargoManifest, Error> {
}

impl CargoManifest {
fn into_npm(self) -> NpmPackage {
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);
}
NpmPackage {
name: self.package.name,
description: self.package.description,
Expand All @@ -68,7 +71,7 @@ impl CargoManifest {
}

/// Generate a package.json file inside in `./pkg`.
pub fn write_package_json(path: &str) -> Result<(), Error> {
pub fn write_package_json(path: &str, scope: Option<String>) -> Result<(), Error> {
let step = format!(
"{} {}Writing a package.json...",
style("[4/7]").bold().dim(),
Expand All @@ -78,7 +81,7 @@ pub fn write_package_json(path: &str) -> Result<(), Error> {
let pkg_file_path = format!("{}/pkg/package.json", path);
let mut pkg_file = File::create(pkg_file_path)?;
let crate_data = read_cargo_toml(path)?;
let npm_data = crate_data.into_npm();
let npm_data = crate_data.into_npm(scope);
let npm_json = serde_json::to_string(&npm_data)?;
pkg_file.write_all(npm_json.as_bytes())?;
pb.finish();
Expand Down
140 changes: 140 additions & 0 deletions tests/fixtures/scopes/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions tests/fixtures/scopes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "scopes-hello-world"
description = "an example rust->wasm crate"
version = "0.1.0"
authors = ["Michael Gattozzi <[email protected]>"]
license = "WTFPL"
repository = "https://github.com/ashleygwilliams/wasm-pack"

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.1.0"
2 changes: 2 additions & 0 deletions tests/fixtures/scopes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# scopes-hello-world
> an example rust -> wasm project
15 changes: 15 additions & 0 deletions tests/fixtures/scopes/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![feature(proc_macro)]

extern crate wasm_bindgen;

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern {
fn alert(s: &str);
}

#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}
20 changes: 18 additions & 2 deletions tests/manifest/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn it_gets_the_crate_name_provided_path() {
fn it_creates_a_package_json_default_path() {
let path = ".".to_string();
wasm_pack::create_pkg_dir(&path).unwrap();
assert!(manifest::write_package_json(&path).is_ok());
assert!(manifest::write_package_json(&path, None).is_ok());
let package_json_path = format!("{}/pkg/package.json", &path);
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::read_package_json(&path).is_ok());
Expand All @@ -47,11 +47,27 @@ fn it_creates_a_package_json_default_path() {
fn it_creates_a_package_json_provided_path() {
let path = "tests/fixtures/js-hello-world".to_string();
wasm_pack::create_pkg_dir(&path).unwrap();
assert!(manifest::write_package_json(&path).is_ok());
assert!(manifest::write_package_json(&path, None).is_ok());
let package_json_path = format!("{}/pkg/package.json", &path);
assert!(fs::metadata(package_json_path).is_ok());
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]
fn it_creates_a_package_json_provided_path_with_scope() {
let path = "tests/fixtures/scopes".to_string();
wasm_pack::create_pkg_dir(&path).unwrap();
assert!(manifest::write_package_json(&path, Some("test".to_string())).is_ok());
let package_json_path = format!("{}/pkg/package.json", &path);
assert!(fs::metadata(package_json_path).is_ok());
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"]
);
}

0 comments on commit cef406f

Please sign in to comment.