diff --git a/cli/args/flags.rs b/cli/args/flags.rs index f40d5aed41..b31bfcecf7 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -428,6 +428,7 @@ pub struct PublishFlags { pub allow_slow_types: bool, pub allow_dirty: bool, pub no_provenance: bool, + pub set_version: Option, } #[derive(Clone, Debug, Eq, PartialEq)] @@ -1391,7 +1392,7 @@ pub fn flags_from_vec(args: Vec) -> clap::error::Result { "uninstall" => uninstall_parse(&mut flags, &mut m), "upgrade" => upgrade_parse(&mut flags, &mut m), "vendor" => vendor_parse(&mut flags, &mut m), - "publish" => publish_parse(&mut flags, &mut m), + "publish" => publish_parse(&mut flags, &mut m)?, _ => unreachable!(), } } else { @@ -3225,12 +3226,12 @@ fn publish_subcommand() -> Command { command("publish", "Publish the current working directory's package or workspace to JSR", UnstableArgsConfig::ResolutionOnly) .defer(|cmd| { cmd - .arg( - Arg::new("token") - .long("token") - .help("The API token to use when publishing. If unset, interactive authentication is be used") - .help_heading(PUBLISH_HEADING) - ) + .arg( + Arg::new("token") + .long("token") + .help("The API token to use when publishing. If unset, interactive authentication is be used") + .help_heading(PUBLISH_HEADING) + ) .arg(config_arg()) .arg(no_config_arg()) .arg( @@ -3238,29 +3239,38 @@ fn publish_subcommand() -> Command { .long("dry-run") .help("Prepare the package for publishing performing all checks and validations without uploading") .action(ArgAction::SetTrue) - .help_heading(PUBLISH_HEADING), + .help_heading(PUBLISH_HEADING), ) .arg( Arg::new("allow-slow-types") .long("allow-slow-types") .help("Allow publishing with slow types") .action(ArgAction::SetTrue) - .help_heading(PUBLISH_HEADING), + .help_heading(PUBLISH_HEADING), ) .arg( Arg::new("allow-dirty") .long("allow-dirty") .help("Allow publishing if the repository has uncommitted changed") .action(ArgAction::SetTrue) - .help_heading(PUBLISH_HEADING), - ).arg( - Arg::new("no-provenance") - .long("no-provenance") - .help(cstr!("Disable provenance attestation. + .help_heading(PUBLISH_HEADING), + ) + .arg( + Arg::new("no-provenance") + .long("no-provenance") + .help(cstr!("Disable provenance attestation. Enabled by default on Github actions, publicly links the package to where it was built and published from.")) - .action(ArgAction::SetTrue) - .help_heading(PUBLISH_HEADING) - ) + .action(ArgAction::SetTrue) + .help_heading(PUBLISH_HEADING) + ) + .arg( + Arg::new("set-version") + .long("set-version") + .help("Set version for a package to be published. + This flag can be used while publishing individual packages and cannot be used in a workspace.") + .value_name("VERSION") + .help_heading(PUBLISH_HEADING) + ) .arg(check_arg(/* type checks by default */ true)) .arg(no_check_arg()) }) @@ -5229,7 +5239,10 @@ fn vendor_parse(flags: &mut Flags, _matches: &mut ArgMatches) { flags.subcommand = DenoSubcommand::Vendor } -fn publish_parse(flags: &mut Flags, matches: &mut ArgMatches) { +fn publish_parse( + flags: &mut Flags, + matches: &mut ArgMatches, +) -> clap::error::Result<()> { flags.type_check_mode = TypeCheckMode::Local; // local by default unstable_args_parse(flags, matches, UnstableArgsConfig::ResolutionOnly); no_check_arg_parse(flags, matches); @@ -5242,7 +5255,10 @@ fn publish_parse(flags: &mut Flags, matches: &mut ArgMatches) { allow_slow_types: matches.get_flag("allow-slow-types"), allow_dirty: matches.get_flag("allow-dirty"), no_provenance: matches.get_flag("no-provenance"), + set_version: matches.remove_one::("set-version"), }); + + Ok(()) } fn compile_args_parse( @@ -10769,6 +10785,7 @@ mod tests { "--allow-slow-types", "--allow-dirty", "--token=asdf", + "--set-version=1.0.1", ]); assert_eq!( r.unwrap(), @@ -10779,6 +10796,7 @@ mod tests { allow_slow_types: true, allow_dirty: true, no_provenance: true, + set_version: Some("1.0.1".to_string()), }), type_check_mode: TypeCheckMode::Local, ..Flags::default() diff --git a/cli/tools/registry/mod.rs b/cli/tools/registry/mod.rs index dec3c1afeb..f121c4623c 100644 --- a/cli/tools/registry/mod.rs +++ b/cli/tools/registry/mod.rs @@ -90,7 +90,7 @@ pub async fn publish( let cli_options = cli_factory.cli_options()?; let directory_path = cli_options.initial_cwd(); - let publish_configs = cli_options.start_dir.jsr_packages_for_publish(); + let mut publish_configs = cli_options.start_dir.jsr_packages_for_publish(); if publish_configs.is_empty() { match cli_options.start_dir.maybe_deno_json() { Some(deno_json) => { @@ -108,6 +108,18 @@ pub async fn publish( } } } + + if let Some(version) = &publish_flags.set_version { + if publish_configs.len() > 1 { + bail!("Cannot use --set-version when publishing a workspace. Change your cwd to an individual package instead."); + } + if let Some(publish_config) = publish_configs.get_mut(0) { + let mut config_file = publish_config.config_file.as_ref().clone(); + config_file.json.version = Some(version.clone()); + publish_config.config_file = Arc::new(config_file); + } + } + let specifier_unfurler = Arc::new(SpecifierUnfurler::new( if cli_options.unstable_sloppy_imports() { Some(CliSloppyImportsResolver::new(SloppyImportsCachedFs::new( @@ -410,9 +422,12 @@ impl PublishPreparer { let deno_json = &package.config_file; let config_path = deno_json.specifier.to_file_path().unwrap(); let root_dir = config_path.parent().unwrap().to_path_buf(); - let Some(version) = deno_json.json.version.clone() else { - bail!("{} is missing 'version' field", deno_json.specifier); - }; + let version = deno_json.json.version.clone().ok_or_else(|| { + deno_core::anyhow::anyhow!( + "{} is missing 'version' field", + deno_json.specifier + ) + })?; if deno_json.json.exports.is_none() { let mut suggested_entrypoint = None; @@ -435,11 +450,11 @@ impl PublishPreparer { ); bail!( - "You did not specify an entrypoint to \"{}\" package in {}. Add `exports` mapping in the configuration file, eg:\n{}", - package.name, - deno_json.specifier, - exports_content - ); + "You did not specify an entrypoint to \"{}\" package in {}. Add `exports` mapping in the configuration file, eg:\n{}", + package.name, + deno_json.specifier, + exports_content + ); } let Some(name_no_at) = package.name.strip_prefix('@') else { bail!("Invalid package name, use '@/ format"); diff --git a/tests/specs/publish/set_version/multiple_packages/LICENSE b/tests/specs/publish/set_version/multiple_packages/LICENSE new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/specs/publish/set_version/multiple_packages/__test__.jsonc b/tests/specs/publish/set_version/multiple_packages/__test__.jsonc new file mode 100644 index 0000000000..c191386d80 --- /dev/null +++ b/tests/specs/publish/set_version/multiple_packages/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "publish --set-version 1.1.0 --token 'sadfasdf'", + "output": "error_set_version.out", + "exitCode": 1 +} diff --git a/tests/specs/publish/set_version/multiple_packages/deno.json b/tests/specs/publish/set_version/multiple_packages/deno.json new file mode 100644 index 0000000000..4b3ffe44da --- /dev/null +++ b/tests/specs/publish/set_version/multiple_packages/deno.json @@ -0,0 +1,8 @@ +{ + "workspace": { + "members": [ + "packages/package1", + "packages/package2" + ] + } +} diff --git a/tests/specs/publish/set_version/multiple_packages/error_set_version.out b/tests/specs/publish/set_version/multiple_packages/error_set_version.out new file mode 100644 index 0000000000..098692c4c6 --- /dev/null +++ b/tests/specs/publish/set_version/multiple_packages/error_set_version.out @@ -0,0 +1 @@ +error: Cannot use --set-version when publishing a workspace. Change your cwd to an individual package instead. diff --git a/tests/specs/publish/set_version/multiple_packages/packages/package1/deno.json b/tests/specs/publish/set_version/multiple_packages/packages/package1/deno.json new file mode 100644 index 0000000000..737fc9c139 --- /dev/null +++ b/tests/specs/publish/set_version/multiple_packages/packages/package1/deno.json @@ -0,0 +1,7 @@ +{ + "name": "@foo/package1", + "version": "1.0.0", + "exports": { + ".": "./mod.ts" + } +} diff --git a/tests/specs/publish/set_version/multiple_packages/packages/package1/mod.ts b/tests/specs/publish/set_version/multiple_packages/packages/package1/mod.ts new file mode 100644 index 0000000000..a2cd81bed7 --- /dev/null +++ b/tests/specs/publish/set_version/multiple_packages/packages/package1/mod.ts @@ -0,0 +1,3 @@ +export function package1() { + return "package1"; +} diff --git a/tests/specs/publish/set_version/multiple_packages/packages/package2/deno.json b/tests/specs/publish/set_version/multiple_packages/packages/package2/deno.json new file mode 100644 index 0000000000..16987e6194 --- /dev/null +++ b/tests/specs/publish/set_version/multiple_packages/packages/package2/deno.json @@ -0,0 +1,7 @@ +{ + "name": "@foo/package2", + "version": "1.0.0", + "exports": { + ".": "./mod.ts" + } +} diff --git a/tests/specs/publish/set_version/multiple_packages/packages/package2/mod.ts b/tests/specs/publish/set_version/multiple_packages/packages/package2/mod.ts new file mode 100644 index 0000000000..6c1361aa42 --- /dev/null +++ b/tests/specs/publish/set_version/multiple_packages/packages/package2/mod.ts @@ -0,0 +1,3 @@ +export function package2() { + return "package2"; +} diff --git a/tests/specs/publish/set_version/success/LICENSE b/tests/specs/publish/set_version/success/LICENSE new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/specs/publish/set_version/success/__test__.jsonc b/tests/specs/publish/set_version/success/__test__.jsonc new file mode 100644 index 0000000000..6f0bf802e7 --- /dev/null +++ b/tests/specs/publish/set_version/success/__test__.jsonc @@ -0,0 +1,4 @@ +{ + "args": "publish --set-version 1.1.0 --token 'sadfasdf'", + "output": "successful_set_version.out" +} diff --git a/tests/specs/publish/set_version/success/deno.json b/tests/specs/publish/set_version/success/deno.json new file mode 100644 index 0000000000..fefab899bd --- /dev/null +++ b/tests/specs/publish/set_version/success/deno.json @@ -0,0 +1,10 @@ +{ + "name": "@foo/bar", + "version": "1.0.0", + "exports": { + ".": "./mod.ts" + }, + "imports": { + "@std/http": "./std_http.ts" + } +} diff --git a/tests/specs/publish/set_version/success/mod.ts b/tests/specs/publish/set_version/success/mod.ts new file mode 100644 index 0000000000..6e8a61bae9 --- /dev/null +++ b/tests/specs/publish/set_version/success/mod.ts @@ -0,0 +1,7 @@ +import http from "@std/http"; + +export function foobar(): { fileServer(): void } { + return { + fileServer: http.fileServer, + }; +} diff --git a/tests/specs/publish/set_version/success/std_http.ts b/tests/specs/publish/set_version/success/std_http.ts new file mode 100644 index 0000000000..9d57b36f34 --- /dev/null +++ b/tests/specs/publish/set_version/success/std_http.ts @@ -0,0 +1,6 @@ +// temp until we get jsr:@std/http in the test server +export default { + fileServer() { + console.log("Hi"); + }, +}; diff --git a/tests/specs/publish/set_version/success/successful_set_version.out b/tests/specs/publish/set_version/success/successful_set_version.out new file mode 100644 index 0000000000..a5cb9aa821 --- /dev/null +++ b/tests/specs/publish/set_version/success/successful_set_version.out @@ -0,0 +1,6 @@ +Check file:///[WILDCARD]/success/mod.ts +Checking for slow types in the public API... +Check file:///[WILDCARD]/success/mod.ts +Publishing @foo/bar@1.1.0 ... +Successfully published @foo/bar@1.1.0 +Visit http://127.0.0.1:4250/@foo/bar@1.1.0 for details