diff options
author | bartOssh <lenart.consulting@gmail.com> | 2020-09-16 15:38:38 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-16 15:38:38 +0200 |
commit | 81ca7096c5e8830cf8707c77fd244672e7e67c79 (patch) | |
tree | ff73340eb97b2601b41903a641b4cb99911f8aa5 /cli/info.rs | |
parent | d4a24c870e87b55dab425bc2c320aa88a6224030 (diff) |
refactor(unstable): deno info --json output (#7417)
Provide flat JSON structured output.
Fix BrokenPipe error when piping out to "head".
Diffstat (limited to 'cli/info.rs')
-rw-r--r-- | cli/info.rs | 62 |
1 files changed, 59 insertions, 3 deletions
diff --git a/cli/info.rs b/cli/info.rs index fdc021eff..7b5c0151b 100644 --- a/cli/info.rs +++ b/cli/info.rs @@ -4,8 +4,9 @@ use crate::module_graph::{ModuleGraph, ModuleGraphFile, ModuleGraphLoader}; 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 @@ -18,7 +19,10 @@ 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 { @@ -26,7 +30,7 @@ impl ModuleDepInfo { 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( @@ -59,7 +63,9 @@ impl ModuleDepInfo { crate::media_type::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, @@ -68,6 +74,8 @@ impl ModuleDepInfo { map: map_filename, dep_count, deps, + total_size, + files, }; Ok(info) @@ -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, @@ -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() { let prefix = get_new_prefix("", false); assert_eq!(prefix, "│ ".to_string()); |