-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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: support json output for outdated command #27271
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,15 +4,19 @@ use std::collections::HashSet; | |
use std::sync::Arc; | ||
|
||
use deno_core::error::AnyError; | ||
use deno_core::serde_json; | ||
use deno_semver::package::PackageNv; | ||
use deno_semver::package::PackageReq; | ||
use deno_semver::VersionReq; | ||
use deno_terminal::colors; | ||
use serde::Serialize; | ||
use serde::Serializer; | ||
|
||
use crate::args::CacheSetting; | ||
use crate::args::CliOptions; | ||
use crate::args::Flags; | ||
use crate::args::OutdatedFlags; | ||
use crate::args::OutdatedOutputFmt; | ||
use crate::factory::CliFactory; | ||
use crate::file_fetcher::FileFetcher; | ||
use crate::jsr::JsrFetchResolver; | ||
|
@@ -24,15 +28,29 @@ use super::deps::DepManager; | |
use super::deps::DepManagerArgs; | ||
use super::deps::PackageLatestVersion; | ||
|
||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] | ||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)] | ||
struct OutdatedPackage { | ||
#[serde(rename = "specifier", serialize_with = "lowercase_serializer")] | ||
kind: DepKind, | ||
#[serde(rename = "latest")] | ||
latest: String, | ||
#[serde(rename = "update")] | ||
semver_compatible: String, | ||
current: String, | ||
#[serde(rename = "package")] | ||
name: String, | ||
} | ||
|
||
fn lowercase_serializer<S>(kind: &DepKind, s: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
match kind { | ||
DepKind::Npm => s.serialize_str("npm"), | ||
DepKind::Jsr => s.serialize_str("jsr"), | ||
} | ||
} | ||
|
||
#[allow(clippy::print_stdout)] | ||
fn print_outdated_table(packages: &[OutdatedPackage]) { | ||
const HEADINGS: &[&str] = &["Package", "Current", "Update", "Latest"]; | ||
|
@@ -103,6 +121,7 @@ fn print_outdated_table(packages: &[OutdatedPackage]) { | |
fn print_outdated( | ||
deps: &mut DepManager, | ||
compatible: bool, | ||
output_fmt: OutdatedOutputFmt, | ||
) -> Result<(), AnyError> { | ||
let mut outdated = Vec::new(); | ||
let mut seen = std::collections::BTreeSet::new(); | ||
|
@@ -147,7 +166,13 @@ fn print_outdated( | |
|
||
if !outdated.is_empty() { | ||
outdated.sort(); | ||
print_outdated_table(&outdated); | ||
match output_fmt { | ||
OutdatedOutputFmt::Table => print_outdated_table(&outdated), | ||
OutdatedOutputFmt::Json => { | ||
let json = serde_json::to_string_pretty(&outdated)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this feature is added, we should use object notation and then include a version number (ex. "1"), similar to the other There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1. Let's hammer out the format first (see above) and then we can add a version field. Typically semantic version I guess (like 1.0.0)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a single integer is fine. Additions fall under the same version, but breaking changes cause a version bump. Otherwise people need to parse the version with something like |
||
println!("{json}"); | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
|
@@ -214,8 +239,11 @@ pub async fn outdated( | |
crate::args::OutdatedKind::Update { latest } => { | ||
update(deps, latest, &filter_set, flags).await?; | ||
} | ||
crate::args::OutdatedKind::PrintOutdated { compatible } => { | ||
print_outdated(&mut deps, compatible)?; | ||
crate::args::OutdatedKind::PrintOutdated { | ||
compatible, | ||
output_fmt, | ||
} => { | ||
print_outdated(&mut deps, compatible, output_fmt)?; | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the other
--json
flags, we've wanted to require people to specify the version of json output that they want similar to what Rust requires. This allows us to break the output over time while maintaining backwards compatibility.If adding this flag, we should probably proactively do that here, but I'm not sure what that would look like. There's an open issue for this somewhere I think, but it's hard to search for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean that it would be specified on the cmd line, something like
--json v1
?I just checked what
npm outdated --json
does and here is an example of the output it uses:Do you think we should align with that format? It's a bit different, particularly that the package is the key.