From 0ceae7a490d933144f63be5bc9d5ba0b27bf5c6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 12 Dec 2023 19:18:02 +0100 Subject: [PATCH] refactor: suggest entrypoint on missing 'exports' (#21549) --- cli/tools/registry/mod.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/cli/tools/registry/mod.rs b/cli/tools/registry/mod.rs index fa9fbe33f9388f..cfc335fd95891f 100644 --- a/cli/tools/registry/mod.rs +++ b/cli/tools/registry/mod.rs @@ -71,6 +71,9 @@ pub struct PublishingTask { pub error: Option, } +static SUGGESTED_ENTRYPOINTS: [&str; 4] = + ["mod.ts", "mod.js", "index.ts", "index.js"]; + async fn prepare_publish( initial_cwd: &Path, directory: PathBuf, @@ -92,6 +95,34 @@ async fn prepare_publish( let Some(name) = deno_json.json.name.clone() else { bail!("{} is missing 'name' field", deno_json_path.display()); }; + if deno_json.json.exports.is_none() { + let mut suggested_entrypoint = None; + + for entrypoint in SUGGESTED_ENTRYPOINTS { + if directory_path.join(entrypoint).exists() { + suggested_entrypoint = Some(entrypoint); + break; + } + } + + let exports_content = format!( + r#"{{ + "name": "{}", + "version": "{}", + "exports": "{}" +}}"#, + name, + version, + suggested_entrypoint.unwrap_or("") + ); + + bail!( + "You did not specify an entrypoint to \"{}\" package in {}. Add `exports` mapping in the configuration file, eg:\n{}", + name, + deno_json_path.display(), + exports_content + ); + } let Some(name) = name.strip_prefix('@') else { bail!("Invalid package name, use '@/ format"); };