|
|
|
@ -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; |
|
|
|
@ -57,6 +60,27 @@ struct CargoPackage { |
|
|
|
|
repository: Option<String>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl CargoPackage { |
|
|
|
|
fn check_optional_fields(&self) { |
|
|
|
|
let warn_fmt = |field| { |
|
|
|
|
format!( |
|
|
|
|
"Field '{}' is missing from Cargo.toml. It is not necessary, but recommended", |
|
|
|
|
field |
|
|
|
|
) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if self.description.is_none() { |
|
|
|
|
PBAR.warn(&warn_fmt("description")); |
|
|
|
|
} |
|
|
|
|
if self.repository.is_none() { |
|
|
|
|
PBAR.warn(&warn_fmt("repository")); |
|
|
|
|
} |
|
|
|
|
if self.license.is_none() { |
|
|
|
|
PBAR.warn(&warn_fmt("license")); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)] |
|
|
|
|
#[serde(untagged)] |
|
|
|
|
enum CargoDependency { |
|
|
|
@ -75,32 +99,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,19 +118,55 @@ 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); |
|
|
|
|
let mut files = vec![wasm_file]; |
|
|
|
|
|
|
|
|
|
let dts_file = if disable_dts == true { |
|
|
|
|
None |
|
|
|
|
let js_bg_file = format!("{}_bg.js", filename); |
|
|
|
|
files.push(js_bg_file.to_string()); |
|
|
|
|
|
|
|
|
|
if let Some(s) = scope { |
|
|
|
|
self.package.name = format!("@{}/{}", s, self.package.name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let dts_file = if disable_dts == false { |
|
|
|
|
let file = format!("{}.d.ts", filename); |
|
|
|
|
files.push(file.to_string()); |
|
|
|
|
Some(file) |
|
|
|
|
} else { |
|
|
|
|
Some(format!("{}.d.ts", filename)) |
|
|
|
|
None |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
let js_bg_file = if target == "nodejs" { |
|
|
|
|
Some(format!("{}_bg.js", filename)) |
|
|
|
|
&self.package.check_optional_fields(); |
|
|
|
|
|
|
|
|
|
NpmPackage::CommonJSPackage(CommonJSPackage { |
|
|
|
|
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, |
|
|
|
|
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 mut files = vec![wasm_file, js_file.clone()]; |
|
|
|
|
|
|
|
|
|
let dts_file = if disable_dts == false { |
|
|
|
|
let file = format!("{}.d.ts", filename); |
|
|
|
|
files.push(file.to_string()); |
|
|
|
|
Some(file) |
|
|
|
|
} else { |
|
|
|
|
None |
|
|
|
|
}; |
|
|
|
@ -140,23 +174,10 @@ impl CargoManifest { |
|
|
|
|
if let Some(s) = scope { |
|
|
|
|
self.package.name = format!("@{}/{}", s, self.package.name); |
|
|
|
|
} |
|
|
|
|
let mut files = vec![wasm_file]; |
|
|
|
|
|
|
|
|
|
match dts_file { |
|
|
|
|
Some(ref dts_file) => { |
|
|
|
|
files.push(dts_file.to_string()); |
|
|
|
|
} |
|
|
|
|
None => {} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
match js_bg_file { |
|
|
|
|
Some(ref js_bg_file) => { |
|
|
|
|
files.push(js_bg_file.to_string()); |
|
|
|
|
} |
|
|
|
|
None => {} |
|
|
|
|
} |
|
|
|
|
&self.package.check_optional_fields(); |
|
|
|
|
|
|
|
|
|
NpmPackage { |
|
|
|
|
NpmPackage::ESModulesPackage(ESModulesPackage { |
|
|
|
|
name: self.package.name, |
|
|
|
|
collaborators: self.package.authors, |
|
|
|
|
description: self.package.description, |
|
|
|
@ -167,9 +188,10 @@ impl CargoManifest { |
|
|
|
|
url: repo_url, |
|
|
|
|
}), |
|
|
|
|
files: files, |
|
|
|
|
main: js_file, |
|
|
|
|
module: js_file, |
|
|
|
|
types: dts_file, |
|
|
|
|
} |
|
|
|
|
side_effects: "false".to_string(), |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -184,28 +206,15 @@ 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 |
|
|
|
|
) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
|
|
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_data = if target == "nodejs" { |
|
|
|
|
crate_data.into_commonjs(scope, disable_dts) |
|
|
|
|
} else { |
|
|
|
|
crate_data.into_esmodules(scope, disable_dts) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
let npm_json = serde_json::to_string_pretty(&npm_data)?; |
|
|
|
|
pkg_file.write_all(npm_json.as_bytes())?; |