Skip to content
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

feat(publish): add --set-version <version> flag #26141

Merged
merged 13 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ pub struct PublishFlags {
pub allow_slow_types: bool,
pub allow_dirty: bool,
pub no_provenance: bool,
pub override_version: Option<String>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -3189,42 +3190,50 @@ 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(
Arg::new("dry-run")
.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.
<p(245)>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("override-version")
.long("override-version")
.help("Override the version specified in the configuration file")
dsherret marked this conversation as resolved.
Show resolved Hide resolved
.value_name("VERSION")
.help_heading(PUBLISH_HEADING)
)
.arg(check_arg(/* type checks by default */ true))
.arg(no_check_arg())
})
Expand Down Expand Up @@ -5154,6 +5163,7 @@ 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"),
override_version: matches.remove_one("override-version"),
});
}

Expand Down Expand Up @@ -10611,6 +10621,7 @@ mod tests {
"--allow-slow-types",
"--allow-dirty",
"--token=asdf",
"--override-version=1.0.1",
]);
assert_eq!(
r.unwrap(),
Expand All @@ -10621,6 +10632,7 @@ mod tests {
allow_slow_types: true,
allow_dirty: true,
no_provenance: true,
override_version: Some("1.0.1".to_string()),
}),
type_check_mode: TypeCheckMode::Local,
..Flags::default()
Expand Down
42 changes: 29 additions & 13 deletions cli/tools/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,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();
dsherret marked this conversation as resolved.
Show resolved Hide resolved
if publish_configs.is_empty() {
match cli_options.start_dir.maybe_deno_json() {
Some(deno_json) => {
Expand All @@ -107,6 +107,19 @@ pub async fn publish(
}
}
}

if let Some(version) = &publish_flags.override_version {
publish_configs = publish_configs
.into_iter()
.map(|mut config| {
let mut config_file = config.config_file.as_ref().clone();
config_file.json.version = Some(version.clone());
config.config_file = Arc::new(config_file);
config
})
.collect();
dsherret marked this conversation as resolved.
Show resolved Hide resolved
}

let specifier_unfurler = Arc::new(SpecifierUnfurler::new(
if cli_options.unstable_sloppy_imports() {
Some(CliSloppyImportsResolver::new(SloppyImportsCachedFs::new(
Expand Down Expand Up @@ -409,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;

Expand All @@ -424,21 +440,21 @@ impl PublishPreparer {

let exports_content = format!(
r#"{{
"name": "{}",
"version": "{}",
"exports": "{}"
}}"#,
"name": "{}",
"version": "{}",
"exports": "{}"
}}"#,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is introducing a change in the message. Surprisingly we don't have a test for this to have caught this.

package.name,
version,
suggested_entrypoint.unwrap_or("<path_to_entrypoint>")
);

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 '@<scope_name>/<package_name> format");
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"args": "publish --override-version 1.1.0 --token 'sadfasdf'",
"output": "successful_override_version.out"
}
10 changes: 10 additions & 0 deletions tests/specs/publish/successful_override_version/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@foo/bar",
"version": "1.0.0",
"exports": {
".": "./mod.ts"
},
"imports": {
"@std/http": "./std_http.ts"
}
}
7 changes: 7 additions & 0 deletions tests/specs/publish/successful_override_version/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import http from "@std/http";

export function foobar(): { fileServer(): void } {
return {
fileServer: http.fileServer,
};
}
6 changes: 6 additions & 0 deletions tests/specs/publish/successful_override_version/std_http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// temp until we get jsr:@std/http in the test server
export default {
fileServer() {
console.log("Hi");
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Check file:///[WILDCARD]/publish/successful_override_version/mod.ts
Checking for slow types in the public API...
Check file:///[WILDCARD]/publish/successful_override_version/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