Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modules modules modules #312

Merged
merged 13 commits into from
Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 67 additions & 49 deletions src/manifest.rs → src/manifest/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
//! Reading and writing Cargo.toml and package.json manifests.

mod npm;

use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;

use self::npm::{repository::Repository, CommonJSPackage, ESModulesPackage, NpmPackage};
use console::style;
use emoji;
use error::Error;
Expand Down Expand Up @@ -75,32 +78,6 @@ struct CargoLib {
crate_type: Option<Vec<String>>,
}

#[derive(Serialize)]
struct NpmPackage {
name: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
collaborators: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
version: String,
#[serde(skip_serializing_if = "Option::is_none")]
license: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
repository: Option<Repository>,
#[serde(skip_serializing_if = "Vec::is_empty")]
files: Vec<String>,
main: String,
#[serde(skip_serializing_if = "Option::is_none")]
types: Option<String>,
}

#[derive(Serialize)]
struct Repository {
#[serde(rename = "type")]
ty: String,
url: String,
}

fn read_cargo_toml(path: &Path) -> Result<CargoManifest, Error> {
let manifest_path = path.join("Cargo.toml");
if !manifest_path.is_file() {
Expand All @@ -120,7 +97,7 @@ fn read_cargo_toml(path: &Path) -> Result<CargoManifest, Error> {
}

impl CargoManifest {
fn into_npm(mut self, scope: &Option<String>, disable_dts: bool, target: &str) -> NpmPackage {
fn into_commonjs(mut self, scope: &Option<String>, disable_dts: bool) -> NpmPackage {
let filename = self.package.name.replace("-", "_");
let wasm_file = format!("{}_bg.wasm", filename);
let js_file = format!("{}.js", filename);
Expand All @@ -131,11 +108,7 @@ impl CargoManifest {
Some(format!("{}.d.ts", filename))
};

let js_bg_file = if target == "nodejs" {
Some(format!("{}_bg.js", filename))
} else {
None
};
let js_bg_file = Some(format!("{}_bg.js", filename));

if let Some(s) = scope {
self.package.name = format!("@{}/{}", s, self.package.name);
Expand All @@ -156,7 +129,7 @@ impl CargoManifest {
None => {}
}

NpmPackage {
NpmPackage::CommonJSPackage(CommonJSPackage {
name: self.package.name,
collaborators: self.package.authors,
description: self.package.description,
Expand All @@ -169,7 +142,46 @@ impl CargoManifest {
files: files,
main: js_file,
types: dts_file,
})
}

fn into_esmodules(mut self, scope: &Option<String>, disable_dts: bool) -> NpmPackage {
let filename = self.package.name.replace("-", "_");
let wasm_file = format!("{}_bg.wasm", filename);
let js_file = format!("{}.js", filename);

let dts_file = if disable_dts == true {
None
} else {
Some(format!("{}.d.ts", filename))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dts logic looks duplicated with the above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is all from master but yeah you are right i think, file an issue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess i can try to fix it in this PR, might as well

};

if let Some(s) = scope {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This scope handling logic looks duplicated between this and the above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is all from master but yeah you are right i think, file an issue?

self.package.name = format!("@{}/{}", s, self.package.name);
}
let mut files = vec![wasm_file, js_file.clone()];
Copy link
Member Author

@ashleygwilliams ashleygwilliams Sep 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this clone. i hate it but i also love not having a ton of lifetime parameters everywhere. i think it's fine but if we hate it let's talk about ways to make it as clean as possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this single allocation that isn't in a loop (or even within any code that we know to be hot) is a problem. 👍


match dts_file {
Some(ref dts_file) => {
files.push(dts_file.to_string());
}
None => {}
}

NpmPackage::ESModulesPackage(ESModulesPackage {
name: self.package.name,
collaborators: self.package.authors,
description: self.package.description,
version: self.package.version,
license: self.package.license,
repository: self.package.repository.map(|repo_url| Repository {
ty: "git".to_string(),
url: repo_url,
}),
files: files,
module: js_file,
types: dts_file,
})
}
}

Expand All @@ -184,28 +196,34 @@ pub fn write_package_json(
) -> Result<(), Error> {
let msg = format!("{}Writing a package.json...", emoji::MEMO);

let warn_fmt = |field| {
format!(
"Field '{}' is missing from Cargo.toml. It is not necessary, but recommended",
field
)
};
//TODO: below, these checks need to move
//let warn_fmt = |field| {
// format!(
// "Field '{}' is missing from Cargo.toml. It is not necessary, but recommended",
// field
// )
//};

PBAR.step(step, &msg);
let pkg_file_path = out_dir.join("package.json");
let mut pkg_file = File::create(pkg_file_path)?;
let crate_data = read_cargo_toml(path)?;
let npm_data = crate_data.into_npm(scope, disable_dts, target);
let npm_data = if target == "nodejs" {
crate_data.into_commonjs(scope, disable_dts)
} else {
crate_data.into_esmodules(scope, disable_dts)
};

if npm_data.description.is_none() {
PBAR.warn(&warn_fmt("description"));
}
if npm_data.repository.is_none() {
PBAR.warn(&warn_fmt("repository"));
}
if npm_data.license.is_none() {
PBAR.warn(&warn_fmt("license"));
}
//TODO: these checks won't work now, we should do this before we serialize
//if npm_data.description.is_none() {
// PBAR.warn(&warn_fmt("description"));
//}
//if npm_data.repository.is_none() {
// PBAR.warn(&warn_fmt("repository"));
//}
//if npm_data.license.is_none() {
// PBAR.warn(&warn_fmt("license"));
//}

let npm_json = serde_json::to_string_pretty(&npm_data)?;
pkg_file.write_all(npm_json.as_bytes())?;
Expand Down
20 changes: 20 additions & 0 deletions src/manifest/npm/commonjs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use manifest::npm::repository::Repository;

#[derive(Serialize)]
pub struct CommonJSPackage {
pub name: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub collaborators: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub version: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub license: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub repository: Option<Repository>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub files: Vec<String>,
pub main: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub types: Option<String>,
}
20 changes: 20 additions & 0 deletions src/manifest/npm/esmodules.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use manifest::npm::repository::Repository;

#[derive(Serialize)]
pub struct ESModulesPackage {
pub name: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub collaborators: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub version: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub license: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub repository: Option<Repository>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub files: Vec<String>,
pub module: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub types: Option<String>,
}
12 changes: 12 additions & 0 deletions src/manifest/npm/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
mod commonjs;
mod esmodules;
pub mod repository;

pub use self::commonjs::CommonJSPackage;
pub use self::esmodules::ESModulesPackage;

#[derive(Serialize)]
pub enum NpmPackage {
CommonJSPackage(CommonJSPackage),
ESModulesPackage(ESModulesPackage),
}
6 changes: 6 additions & 0 deletions src/manifest/npm/repository.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[derive(Serialize)]
pub struct Repository {
#[serde(rename = "type")]
pub ty: String,
pub url: String,
}