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

refactor(unstable): deno info --json output #7417

Merged
merged 20 commits into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from 17 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
2 changes: 1 addition & 1 deletion cli/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

//! This module provides file formating utilities using
//! This module provides file formatting utilities using
//! [`dprint-plugin-typescript`](https://github.com/dsherret/dprint-plugin-typescript).
//!
//! At the moment it is only consumed using CLI but in
Expand Down
62 changes: 59 additions & 3 deletions cli/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use crate::msg;
use crate::ModuleSpecifier;
use crate::Permissions;
use deno_core::error::AnyError;
use serde::ser::Serializer;
use serde::Serialize;
use std::collections::{HashMap, HashSet};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::sync::Arc;

// TODO(bartlomieju): rename
Expand All @@ -19,15 +20,18 @@ pub struct ModuleDepInfo {
compiled: Option<String>,
map: Option<String>,
dep_count: usize,
#[serde(skip_serializing)]
deps: FileInfoDepTree,
total_size: Option<usize>,
files: FileInfoDepFlatGraph,
}

impl ModuleDepInfo {
/// Creates a new `ModuleDepInfo` struct for the module with the provided `ModuleSpecifier`.
pub async fn new(
global_state: &Arc<GlobalState>,
module_specifier: ModuleSpecifier,
) -> Result<ModuleDepInfo, AnyError> {
) -> Result<Self, AnyError> {
// First load module as if it was to be executed by worker
// including compilation step
let mut module_graph_loader = ModuleGraphLoader::new(
Expand Down Expand Up @@ -59,7 +63,9 @@ impl ModuleDepInfo {
let file_type = msg::enum_name_media_type(out.media_type).to_string();

let deps = FileInfoDepTree::new(&module_graph, &module_specifier);
let total_size = deps.total_size;
let dep_count = get_unique_dep_count(&module_graph) - 1;
let files = FileInfoDepFlatGraph::new(&module_graph);

let info = Self {
local: local_filename,
Expand All @@ -68,6 +74,8 @@ impl ModuleDepInfo {
map: map_filename,
dep_count,
deps,
total_size,
files,
};

Ok(info)
Expand Down Expand Up @@ -223,6 +231,54 @@ impl FileInfoDepTree {
}
}

/// Flat graph vertex with all shallow dependencies
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct FileInfoVertex {
size: usize,
deps: Vec<String>,
}

impl FileInfoVertex {
/// Creates new `FileInfoVertex` that is a single vertex dependency module
fn new(size: usize, deps: Vec<String>) -> Self {
Self { size, deps }
}
}

struct FileInfoDepFlatGraph(HashMap<String, FileInfoVertex>);

impl FileInfoDepFlatGraph {
/// Creates new `FileInfoDepFlatGraph`, flat graph of a shallow module dependencies
///
/// Each graph vertex represents unique dependency with its all shallow dependencies
fn new(module_graph: &ModuleGraph) -> Self {
let mut inner = HashMap::new();
module_graph
.iter()
.for_each(|(module_name, module_graph_file)| {
let size = module_graph_file.size();
let mut deps = Vec::new();
module_graph_file.imports.iter().for_each(|import| {
deps.push(import.resolved_specifier.to_string());
});
inner.insert(module_name.clone(), FileInfoVertex::new(size, deps));
});
Self(inner)
}
}

impl Serialize for FileInfoDepFlatGraph {
/// Serializes inner hash map which is ordered by the key
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let ordered: BTreeMap<_, _> = self.0.iter().collect();
ordered.serialize(serializer)
}
}

/// Returns a `ModuleGraphFile` associated to the provided `ModuleSpecifier`.
///
/// If the `specifier` is associated with a file that has a populated redirect field,
Expand Down Expand Up @@ -374,7 +430,7 @@ mod test {
}

#[test]
fn get_new_prefix_adds_a_vertial_bar_if_not_is_last() {
fn get_new_prefix_adds_a_vertical_bar_if_not_is_last() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

spelling correction , not part of this feature

let prefix = get_new_prefix("", false);

assert_eq!(prefix, "│ ".to_string());
Expand Down
4 changes: 2 additions & 2 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ async fn info_command(
if json {
write_json_to_stdout(&json!(info))
} else {
print!("{}", info);
Ok(())
write_to_stdout_ignore_sigpipe(format!("{}", info).as_bytes())
.map_err(AnyError::from)
}
}
}
Expand Down
50 changes: 24 additions & 26 deletions cli/tests/055_info_file_json.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,29 @@
"compiled": null,
"map": null,
"depCount": 3,
"deps": {
"name": "file://[WILDCARD]/005_more_imports.ts",
"size": 211,
"totalSize": 757,
"deps": [
{
"name": "file://[WILDCARD]/subdir/mod1.ts",
"size": 320,
"totalSize": 546,
"deps": [
{
"name": "file://[WILDCARD]/subdir/subdir2/mod2.ts",
"size": 163,
"totalSize": 226,
"deps": [
{
"name": "file://[WILDCARD]/subdir/print_hello.ts",
"size": 63,
"totalSize": 63,
"deps": []
}
]
}
]
}
]
"totalSize": 757,
"files": {
"file://[WILDCARD]005_more_imports.ts": {
"size": 211,
"deps": [
"file://[WILDCARD]/subdir/mod1.ts"
]
},
"file://[WILDCARD]/subdir/mod1.ts": {
"size": 320,
"deps": [
"file://[WILDCARD]/subdir/subdir2/mod2.ts"
]
},
"file://[WILDCARD]/subdir/print_hello.ts": {
"size": 63,
"deps": []
},
"file://[WILDCARD]/subdir/subdir2/mod2.ts": {
"size": 163,
"deps": [
"file://[WILDCARD]/subdir/print_hello.ts"
]
}
}
}