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: Include version number in all --json based outputs #25335

Merged
merged 4 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions cli/tools/bench/reporters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ pub trait BenchReporter {
fn report_uncaught_error(&mut self, origin: &str, error: Box<JsError>);
}

const JSON_SCHEMA_VERSION: u8 = 1;

#[derive(Debug, Serialize)]
struct JsonReporterOutput {
version: u8,
runtime: String,
cpu: String,
benches: Vec<JsonReporterBench>,
Expand All @@ -28,6 +31,7 @@ struct JsonReporterOutput {
impl Default for JsonReporterOutput {
fn default() -> Self {
Self {
version: JSON_SCHEMA_VERSION,
runtime: format!(
"{} {}",
version::DENO_VERSION_INFO.user_agent,
Expand Down
15 changes: 11 additions & 4 deletions cli/tools/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use crate::args::DocHtmlFlag;
use crate::args::DocSourceFileFlag;
use crate::args::Flags;
use crate::colors;
use crate::display::write_json_to_stdout;
use crate::display::write_to_stdout_ignore_sigpipe;
use crate::display;
use crate::factory::CliFactory;
use crate::graph_util::graph_exit_lock_errors;
use crate::tsc::get_types_declaration_file_text;
Expand All @@ -17,6 +16,7 @@ use deno_config::glob::PathOrPatternSet;
use deno_core::anyhow::bail;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_core::serde_json;
use deno_doc as doc;
use deno_doc::html::UrlResolveKind;
use deno_graph::source::NullFileSystem;
Expand All @@ -31,6 +31,8 @@ use std::collections::BTreeMap;
use std::rc::Rc;
use std::sync::Arc;

const JSON_SCHEMA_VERSION: u8 = 1;

async fn generate_doc_nodes_for_builtin_types(
doc_flags: DocFlags,
parser: &dyn ModuleParser,
Expand Down Expand Up @@ -228,7 +230,11 @@ pub async fn doc(
doc_nodes_by_url.into_values().flatten().collect::<Vec<_>>();

if doc_flags.json {
write_json_to_stdout(&doc_nodes)
let json_output = serde_json::json!({
"version": JSON_SCHEMA_VERSION,
"nodes": &doc_nodes
});
display::write_json_to_stdout(&json_output)
} else if doc_flags.lint {
// don't output docs if running with only the --lint flag
log::info!(
Expand Down Expand Up @@ -553,7 +559,8 @@ fn print_docs_to_stdout(
)
};

write_to_stdout_ignore_sigpipe(details.as_bytes()).map_err(AnyError::from)
display::write_to_stdout_ignore_sigpipe(details.as_bytes())
.map_err(AnyError::from)
}

fn check_diagnostics(diagnostics: &[DocDiagnostic]) -> Result<(), AnyError> {
Expand Down
15 changes: 10 additions & 5 deletions cli/tools/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use deno_core::anyhow::bail;
use deno_core::error::AnyError;
use deno_core::resolve_url_or_path;
use deno_core::serde_json;
use deno_core::serde_json::json;
use deno_graph::Dependency;
use deno_graph::GraphKind;
use deno_graph::Module;
Expand All @@ -35,6 +34,8 @@ use crate::npm::CliNpmResolver;
use crate::npm::ManagedCliNpmResolver;
use crate::util::checksum;

const JSON_SCHEMA_VERSION: u8 = 1;

pub async fn info(
flags: Arc<Flags>,
info_flags: InfoFlags,
Expand Down Expand Up @@ -79,7 +80,10 @@ pub async fn info(
}

if info_flags.json {
let mut json_graph = json!(graph);
let mut json_graph = serde_json::json!(graph);
if let Some(output) = json_graph.as_object_mut() {
output.insert("version".to_string(), JSON_SCHEMA_VERSION.into());
}
add_npm_packages_to_json(&mut json_graph, npm_resolver.as_ref());
display::write_json_to_stdout(&json_graph)?;
} else {
Expand Down Expand Up @@ -121,7 +125,8 @@ fn print_cache_info(
let local_storage_dir = origin_dir.join("local_storage");

if json {
let mut output = json!({
let mut json_output = serde_json::json!({
"version": JSON_SCHEMA_VERSION,
"denoDir": deno_dir,
"modulesCache": modules_cache,
"npmCache": npm_cache,
Expand All @@ -131,10 +136,10 @@ fn print_cache_info(
});

if location.is_some() {
output["localStorage"] = serde_json::to_value(local_storage_dir)?;
json_output["localStorage"] = serde_json::to_value(local_storage_dir)?;
}

display::write_json_to_stdout(&output)
display::write_json_to_stdout(&json_output)
} else {
println!("{} {}", colors::bold("DENO_DIR location:"), deno_dir);
println!(
Expand Down
27 changes: 16 additions & 11 deletions cli/tools/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use crate::colors;
use crate::factory::CliFactory;
use crate::graph_util::ModuleGraphCreator;
use crate::tools::fmt::run_parallelized;
use crate::util::display;
use crate::util::file_watcher;
use crate::util::fs::canonicalize_path;
use crate::util::path::is_script_ext;
Expand All @@ -60,6 +61,8 @@ pub use rules::collect_no_slow_type_diagnostics;
pub use rules::ConfiguredRules;
pub use rules::LintRuleProvider;

const JSON_SCHEMA_VERSION: u8 = 1;

static STDIN_FILE_NAME: &str = "$deno$stdin.ts";

pub async fn lint(
Expand Down Expand Up @@ -440,18 +443,20 @@ pub fn print_rules_list(json: bool, maybe_rules_tags: Option<Vec<String>>) {
.rules;

if json {
let json_rules: Vec<serde_json::Value> = lint_rules
.iter()
.map(|rule| {
serde_json::json!({
"code": rule.code(),
"tags": rule.tags(),
"docs": rule.docs(),
let json_output = serde_json::json!({
"version": JSON_SCHEMA_VERSION,
"rules": lint_rules
.iter()
.map(|rule| {
serde_json::json!({
"code": rule.code(),
"tags": rule.tags(),
"docs": rule.docs(),
})
})
})
.collect();
let json_str = serde_json::to_string_pretty(&json_rules).unwrap();
println!("{json_str}");
.collect::<Vec<serde_json::Value>>(),
});
display::write_json_to_stdout(&json_output).unwrap();
} else {
// The rules should still be printed even if `--quiet` option is enabled,
// so use `println!` here instead of `info!`.
Expand Down
4 changes: 4 additions & 0 deletions cli/tools/lint/reporters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use crate::args::LintReporterKind;

use super::LintError;

const JSON_SCHEMA_VERSION: u8 = 1;

pub fn create_reporter(kind: LintReporterKind) -> Box<dyn LintReporter + Send> {
match kind {
LintReporterKind::Pretty => Box::new(PrettyLintReporter::new()),
Expand Down Expand Up @@ -170,13 +172,15 @@ struct JsonLintDiagnostic {

#[derive(Serialize)]
struct JsonLintReporter {
version: u8,
diagnostics: Vec<JsonLintDiagnostic>,
errors: Vec<LintError>,
}

impl JsonLintReporter {
fn new() -> JsonLintReporter {
JsonLintReporter {
version: JSON_SCHEMA_VERSION,
diagnostics: Vec::new(),
errors: Vec::new(),
}
Expand Down
1 change: 1 addition & 0 deletions tests/specs/bench/json_output/pass.json.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Check file:///[WILDCARD]/pass.ts
{
"version": 1,
"runtime": "Deno/[WILDCARD]",
"cpu": "[WILDCARD]",
"benches": [
Expand Down
4 changes: 4 additions & 0 deletions tests/specs/doc/json/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"args": "doc --json json.js",
"output": "json.out"
}
2 changes: 2 additions & 0 deletions tests/specs/doc/json/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types="./types.d.ts" />
export const foo = "foo";
28 changes: 28 additions & 0 deletions tests/specs/doc/json/json.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"version": 1,
"nodes": [
{
"name": "foo",
"isDefault": false,
"location": {
"filename": "file:///[WILDCARD]/types.d.ts",
"line": 2,
"col": 13,
"byteIndex": 39
},
"declarationKind": "export",
"jsDoc": {
"doc": "An exported value."
},
"kind": "variable",
"variableDef": {
"tsType": {
"repr": "string",
"kind": "keyword",
"keyword": "string"
},
"kind": "const"
}
}
]
}
2 changes: 2 additions & 0 deletions tests/specs/doc/json/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** An exported value. */
export const foo: string;
101 changes: 52 additions & 49 deletions tests/specs/doc/lint_json_success/lint_success_json.out
Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
[
{
"name": "Test",
"isDefault": false,
"location": {
"filename": "file:///[WILDCARD]/lint_success.ts",
"line": 2,
"col": 0,
"byteIndex": 22
},
"declarationKind": "export",
"jsDoc": {
"doc": "My test class."
},
"kind": "class",
"classDef": {
"isAbstract": false,
"constructors": [],
"properties": [
{
"jsDoc": {
"doc": "My property."
},
"tsType": {
"repr": "string",
"kind": "keyword",
"keyword": "string"
},
"readonly": false,
"accessibility": null,
"optional": false,
"isAbstract": false,
"isStatic": false,
"name": "prop",
"location": {
"filename": "file:///[WILDCARD]/lint_success.ts",
"line": 4,
"col": 2,
"byteIndex": 66
{
"version": 1,
"nodes": [
{
"name": "Test",
"isDefault": false,
"location": {
"filename": "file:///[WILDCARD]/lint_success.ts",
"line": 2,
"col": 0,
"byteIndex": 22
},
"declarationKind": "export",
"jsDoc": {
"doc": "My test class."
},
"kind": "class",
"classDef": {
"isAbstract": false,
"constructors": [],
"properties": [
{
"jsDoc": {
"doc": "My property."
},
"tsType": {
"repr": "string",
"kind": "keyword",
"keyword": "string"
},
"readonly": false,
"accessibility": null,
"optional": false,
"isAbstract": false,
"isStatic": false,
"name": "prop",
"location": {
"filename": "file:///[WILDCARD]/lint_success.ts",
"line": 4,
"col": 2,
"byteIndex": 66
}
}
}
],
"indexSignatures": [],
"methods": [],
"extends": null,
"implements": [],
"typeParams": [],
"superTypeParams": []
],
"indexSignatures": [],
"methods": [],
"extends": null,
"implements": [],
"typeParams": [],
"superTypeParams": []
}
}
}
]
]
}
1 change: 1 addition & 0 deletions tests/specs/info/multiple_redirects/main.out
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ Download http://localhost:4545/subdir/redirects/redirect1.js
"http://localhost:4546/subdir/redirects/redirect1.js": "http://localhost:4545/subdir/redirects/redirect1.js",
"http://localhost:4548/subdir/redirects/redirect1.js": "http://localhost:4546/subdir/redirects/redirect1.js"
},
"version": 1,
"npmPackages": {}
}
1 change: 1 addition & 0 deletions tests/testdata/info/076_info_json_deps_order.out
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,6 @@
}
],
"redirects": {},
"version": 1,
"npmPackages": {}
}
1 change: 1 addition & 0 deletions tests/testdata/info/info_json.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"version": 1,
"denoDir": "[WILDCARD]",
"modulesCache": "[WILDCARD]deps",
"npmCache": "[WILDCARD]npm",
Expand Down
1 change: 1 addition & 0 deletions tests/testdata/info/info_json_location.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"version": 1,
"denoDir": "[WILDCARD]",
"modulesCache": "[WILDCARD]deps",
"npmCache": "[WILDCARD]npm",
Expand Down
1 change: 1 addition & 0 deletions tests/testdata/info/json_output/main.out
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@
}
],
"redirects": {},
"version": 1,
"npmPackages": {}
}
1 change: 1 addition & 0 deletions tests/testdata/lint/expected_from_stdin_json.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"version": 1,
"diagnostics": [
{
"filename": "[WILDCARD]$deno$stdin.ts",
Expand Down
1 change: 1 addition & 0 deletions tests/testdata/lint/expected_json.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"version": 1,
"diagnostics": [
{
"filename": "[WILDCARD]file1.js",
Expand Down
1 change: 1 addition & 0 deletions tests/testdata/lint/with_report_config_override.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"version": 1,
"diagnostics": [
{
"filename": "[WILDCARD]a.ts",
Expand Down
Loading