Skip to content

Commit

Permalink
Add flag to mark package.json as private
Browse files Browse the repository at this point in the history
This PR adds a `--private` flag to the build command, which is used to
add a `private` field to the generated package.json.

This helps ensure that a package isn't accidentally published to npm.
  • Loading branch information
andrewhamon committed Feb 26, 2025
1 parent 32e52ca commit fb70e16
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct Build {
pub bindgen: Option<install::Status>,
pub cache: Cache,
pub extra_options: Vec<String>,
pub private: bool,
}

/// What sort of output we're going to be generating and flags we're invoking
Expand Down Expand Up @@ -184,6 +185,10 @@ pub struct BuildOptions {

/// List of extra options to pass to `cargo build`
pub extra_options: Vec<String>,

#[clap(long)]
/// Whether the generated package.json should be marked as private
pub private: bool,
}

impl Default for BuildOptions {
Expand All @@ -206,6 +211,7 @@ impl Default for BuildOptions {
out_dir: String::new(),
out_name: None,
extra_options: Vec::new(),
private: false,
}
}
}
Expand Down Expand Up @@ -260,6 +266,7 @@ impl Build {
bindgen: None,
cache: cache::get_wasm_pack_cache()?,
extra_options: build_opts.extra_options,
private: build_opts.private,
})
}

Expand Down Expand Up @@ -394,6 +401,7 @@ impl Build {
&self.scope,
self.disable_dts,
self.target,
self.private,
)?;
info!(
"Wrote a package.json at {:#?}.",
Expand Down
22 changes: 18 additions & 4 deletions src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ impl CrateData {
scope: &Option<String>,
disable_dts: bool,
target: Target,
private: bool,
) -> Result<()> {
let pkg_file_path = out_dir.join("package.json");
// Check if a `package.json` was already generated by wasm-bindgen, if so
Expand All @@ -635,11 +636,16 @@ impl CrateData {
} else {
None
};
let private = if private { Some(true) } else { None };
let npm_data = match target {
Target::Nodejs => self.to_commonjs(scope, disable_dts, existing_deps, out_dir),
Target::NoModules => self.to_nomodules(scope, disable_dts, existing_deps, out_dir),
Target::Bundler => self.to_esmodules(scope, disable_dts, existing_deps, out_dir),
Target::Web => self.to_web(scope, disable_dts, existing_deps, out_dir),
Target::Nodejs => self.to_commonjs(scope, disable_dts, existing_deps, out_dir, private),
Target::NoModules => {
self.to_nomodules(scope, disable_dts, existing_deps, out_dir, private)
}
Target::Bundler => {
self.to_esmodules(scope, disable_dts, existing_deps, out_dir, private)
}
Target::Web => self.to_web(scope, disable_dts, existing_deps, out_dir, private),
// Deno does not need package.json
Target::Deno => return Ok(()),
};
Expand Down Expand Up @@ -726,6 +732,7 @@ impl CrateData {
disable_dts: bool,
dependencies: Option<HashMap<String, String>>,
out_dir: &Path,
private: Option<bool>,
) -> NpmPackage {
let data = self.npm_data(scope, false, disable_dts, out_dir);
let pkg = &self.data.packages[self.current_idx];
Expand All @@ -734,6 +741,7 @@ impl CrateData {

NpmPackage::CommonJSPackage(CommonJSPackage {
name: data.name,
private,
collaborators: pkg.authors.clone(),
description: self.pkg().description.clone(),
version: pkg.version.to_string(),
Expand All @@ -757,6 +765,7 @@ impl CrateData {
disable_dts: bool,
dependencies: Option<HashMap<String, String>>,
out_dir: &Path,
private: Option<bool>,
) -> NpmPackage {
let data = self.npm_data(scope, true, disable_dts, out_dir);
let pkg = &self.data.packages[self.current_idx];
Expand All @@ -765,6 +774,7 @@ impl CrateData {

NpmPackage::ESModulesPackage(ESModulesPackage {
name: data.name,
private,
ty: "module".into(),
collaborators: pkg.authors.clone(),
description: self.pkg().description.clone(),
Expand All @@ -790,6 +800,7 @@ impl CrateData {
disable_dts: bool,
dependencies: Option<HashMap<String, String>>,
out_dir: &Path,
private: Option<bool>,
) -> NpmPackage {
let data = self.npm_data(scope, false, disable_dts, out_dir);
let pkg = &self.data.packages[self.current_idx];
Expand All @@ -798,6 +809,7 @@ impl CrateData {

NpmPackage::ESModulesPackage(ESModulesPackage {
name: data.name,
private,
ty: "module".into(),
collaborators: pkg.authors.clone(),
description: self.pkg().description.clone(),
Expand All @@ -823,6 +835,7 @@ impl CrateData {
disable_dts: bool,
dependencies: Option<HashMap<String, String>>,
out_dir: &Path,
private: Option<bool>,
) -> NpmPackage {
let data = self.npm_data(scope, false, disable_dts, out_dir);
let pkg = &self.data.packages[self.current_idx];
Expand All @@ -831,6 +844,7 @@ impl CrateData {

NpmPackage::NoModulesPackage(NoModulesPackage {
name: data.name,
private,
collaborators: pkg.authors.clone(),
description: self.pkg().description.clone(),
version: pkg.version.to_string(),
Expand Down
2 changes: 2 additions & 0 deletions src/manifest/npm/commonjs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::manifest::npm::repository::Repository;
#[derive(Serialize)]
pub struct CommonJSPackage {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub private: Option<bool>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub collaborators: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
2 changes: 2 additions & 0 deletions src/manifest/npm/esmodules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::manifest::npm::repository::Repository;
#[derive(Serialize)]
pub struct ESModulesPackage {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub private: Option<bool>,
#[serde(rename = "type")]
pub ty: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
Expand Down
2 changes: 2 additions & 0 deletions src/manifest/npm/nomodules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::manifest::npm::repository::Repository;
#[derive(Serialize)]
pub struct NoModulesPackage {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub private: Option<bool>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub collaborators: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down

0 comments on commit fb70e16

Please sign in to comment.