-
Notifications
You must be signed in to change notification settings - Fork 422
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
Changes from 5 commits
33eccd3
13d77ef
f06f3cc
7f38eb9
20239ec
195c4e1
c26a834
fc7c2d1
e607eda
354f038
069f7ed
8b9ad63
f5eaad2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
@@ -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() { | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
@@ -156,7 +129,7 @@ impl CargoManifest { | |
None => {} | ||
} | ||
|
||
NpmPackage { | ||
NpmPackage::CommonJSPackage(CommonJSPackage { | ||
name: self.package.name, | ||
collaborators: self.package.authors, | ||
description: self.package.description, | ||
|
@@ -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)) | ||
}; | ||
|
||
if let Some(s) = scope { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This scope handling logic looks duplicated between this and the above? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
}) | ||
} | ||
} | ||
|
||
|
@@ -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())?; | ||
|
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>, | ||
} |
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>, | ||
} |
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), | ||
} |
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, | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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