Skip to content

Commit

Permalink
Clean up and fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
KrisChambers committed Jul 22, 2020
1 parent a433762 commit 26d240b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 46 deletions.
91 changes: 46 additions & 45 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,19 @@ use futures::Future;
use log::Level;
use log::Metadata;
use log::Record;
use module_graph::ModuleGraph;
use serde::Serialize;
use state::exit_unstable;
use std::env;
use std::io::Read;
use std::io::Write;
use std::path::PathBuf;
use std::{collections::{HashMap, HashSet}, pin::Pin};
use std::{
collections::{HashMap, HashSet},
pin::Pin,
};
use upgrade::upgrade_command;
use url::Url;
use module_graph::ModuleGraph;

static LOGGER: Logger = Logger;

Expand Down Expand Up @@ -181,41 +184,47 @@ fn print_cache_info(state: &GlobalState, json: bool) -> Result<(), ErrBox> {

/// A dependency tree of the basic module information.
///
/// Constructed from a `ModuleGraph` and `ModuleSpecifier` that
/// Constructed from a `ModuleGraph` and `ModuleSpecifier` that
/// acts as the root of the tree.
#[derive(Serialize)]
struct FileInfoDepTree {
name: String,
size: usize,
total_size: usize,
deps: Vec<FileInfoDepTree>
deps: Vec<FileInfoDepTree>,
}

impl FileInfoDepTree {
/// Create a `FileInfoDepTree` tree from a `ModuleGraph` and the root `ModuleSpecifier`.
pub fn new(module_graph: &ModuleGraph, root_specifier: &ModuleSpecifier) -> Self {
pub fn new(
module_graph: &ModuleGraph,
root_specifier: &ModuleSpecifier,
) -> Self {
let mut seen = HashSet::new();
let mut total_sizes = HashMap::new();

Self::cstr_helper(&mut seen, &mut total_sizes, module_graph, root_specifier)
}

fn cstr_helper(seen: &mut HashSet<String>, total_sizes: &mut HashMap<String, usize>, graph: &ModuleGraph, root: &ModuleSpecifier) -> Self {
fn cstr_helper(
seen: &mut HashSet<String>,
total_sizes: &mut HashMap<String, usize>,
graph: &ModuleGraph,
root: &ModuleSpecifier,
) -> Self {
let name = root.to_string();
let never_seen = seen.insert(name.clone());
let file = graph
.get(&name)
.unwrap();

let file = graph.get(&name).unwrap();

let size = file.size();

let deps = if never_seen {
file
.imports
.iter()
.map(|import| import.resolved_specifier.clone())
.map(|spec| Self::cstr_helper(seen, total_sizes, graph, &spec))
.collect::<Vec<_>>()
.imports
.iter()
.map(|import| import.resolved_specifier.clone())
.map(|spec| Self::cstr_helper(seen, total_sizes, graph, &spec))
.collect::<Vec<_>>()
} else {
vec![]
};
Expand All @@ -224,9 +233,11 @@ impl FileInfoDepTree {
if let Some(total_size) = total_sizes.get(&name) {
total_size.to_owned()
} else {
let total = size + deps.iter()
.map(|dep| dep.total_size)
.fold(0usize, |acc, v| acc + v);
let total = size
+ deps
.iter()
.map(|dep| dep.total_size)
.fold(0usize, |acc, v| acc + v);

total_sizes.insert(name.clone(), total);

Expand All @@ -240,7 +251,7 @@ impl FileInfoDepTree {
name,
size,
total_size,
deps
deps,
}
}
}
Expand Down Expand Up @@ -274,7 +285,6 @@ async fn print_file_info(
let file_info = FileInfoDepTree::new(&module_graph, &module_specifier);

if json {

let output = json!({
"local": local,
"fileType": file_type,
Expand All @@ -296,7 +306,12 @@ async fn print_file_info(
println!("{} {}", colors::bold("map:"), map);
}
println!("{} {} unique", colors::bold("deps:"), &module_graph.len());
println!("{} ({}, total = {})", file_info.name, human_size(file_info.size as f64), human_size(file_info.total_size as f64));
println!(
"{} ({}, total = {})",
file_info.name,
human_size(file_info.size as f64),
human_size(file_info.total_size as f64)
);

for (idx, dep) in file_info.deps.iter().enumerate() {
print_file_dep_info(&dep, "", idx == file_info.deps.len() - 1);
Expand All @@ -307,11 +322,7 @@ async fn print_file_info(
}

/// Prints the `FileInfoDepTree` tree to stdout.
fn print_file_dep_info(
info: &FileInfoDepTree,
prefix: &str,
is_last: bool
) {
fn print_file_dep_info(info: &FileInfoDepTree, prefix: &str, is_last: bool) {
print_dep(prefix, is_last, info);

let prefix = &get_new_prefix(prefix, is_last);
Expand All @@ -322,15 +333,12 @@ fn print_file_dep_info(
}

/// Prints a single `FileInfoDepTree` to stdout.
fn print_dep (
prefix: &str,
is_last: bool,
info: &FileInfoDepTree,
) {
fn print_dep(prefix: &str, is_last: bool, info: &FileInfoDepTree) {
let has_children = !info.deps.is_empty();
let totals = get_totals_string(info);

println!("{}{}─{} {}{}",
println!(
"{}{}─{} {}{}",
prefix,
get_sibling_connector(is_last),
get_child_connector(has_children),
Expand All @@ -339,9 +347,7 @@ fn print_dep (
);
}

fn get_totals_string(
info: &FileInfoDepTree
) -> String {
fn get_totals_string(info: &FileInfoDepTree) -> String {
if info.total_size == 0 {
"".to_string()
} else {
Expand Down Expand Up @@ -372,10 +378,7 @@ fn get_child_connector(has_children: bool) -> char {
}

/// Creates a new prefix for a dependency tree item.
fn get_new_prefix(
prefix: &str,
is_last: bool
) -> String {
fn get_new_prefix(prefix: &str, is_last: bool) -> String {
let mut prefix = prefix.to_string();
if is_last {
prefix.push(' ');
Expand Down Expand Up @@ -404,15 +407,13 @@ fn get_new_prefix_adds_a_vertial_bar_if_not_is_last() {

/// Gets the full filename of a `SourceFile`.
fn get_source_filename(file: SourceFile) -> Option<String> {
file.filename
.to_str()
.map(|s| s.to_owned())
file.filename.to_str().map(|s| s.to_owned())
}

/// Constructs a ModuleGraph for the module with the provided name.
async fn get_module_graph(
global_state: &GlobalState,
module_specifier: &ModuleSpecifier
module_specifier: &ModuleSpecifier,
) -> Result<ModuleGraph, ErrBox> {
let mut module_graph_loader = ModuleGraphLoader::new(
global_state.file_fetcher.clone(),
Expand All @@ -424,8 +425,8 @@ async fn get_module_graph(
module_graph_loader
.add_to_graph(&module_specifier, None)
.await?;
Ok(module_graph_loader.get_graph())

Ok(module_graph_loader.get_graph())
}

fn get_types(unstable: bool) -> String {
Expand Down
2 changes: 1 addition & 1 deletion cli/module_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ impl ModuleGraphLoader {
lib_directives,
types_directives,
type_headers: vec![],
}
},
);
Ok(())
}
Expand Down

0 comments on commit 26d240b

Please sign in to comment.