Skip to content

Commit

Permalink
feat: prettify output and specify import line of code
Browse files Browse the repository at this point in the history
Merge request #60 

Author: Thibault Ruby
  • Loading branch information
MarioArnt authored Jan 30, 2024
2 parents d7182bb + cf71bd6 commit dd710b6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ansi_term = "0.12.1"
anyhow = "^1.0.79"
clap = { version = "^4.4.18", features = ["derive"] }
env_logger = "~0.11.0"
Expand Down
48 changes: 41 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ::clap::Parser;
use std::collections::HashMap;
#[derive(Parser)]
#[command(
author,
Expand All @@ -10,6 +11,13 @@ use ::clap::Parser;
struct Args {
#[arg(short, long, default_value_t = false, help = "Output a JSON report")]
report: bool,
#[arg(
short,
long,
default_value_t = false,
help = "Display output in JSON format"
)]
json: bool,
#[arg(short, long, default_value_t = false, help = "Verbose mode")]
verbose: bool,
}
Expand All @@ -18,8 +26,11 @@ mod ast_browser;
mod manifest;
mod write_report;

use crate::ast_browser::ImportStatement;
use ansi_term::Style;
use env_logger::{Builder, Target};
use log::{error, info};
use serde_json::{json, to_string_pretty};
use std::env;
use std::io::Write;
use std::string::String;
Expand Down Expand Up @@ -49,19 +60,42 @@ fn main() {
write_report::write_json_report(extraneous.clone(), implicit.clone());
}

info!("Extraneous dependencies");
for dep in extraneous {
info!("{:?}", dep);
}
info!("Implicit dependencies");
for dep in implicit {
info!("{:?}", dep);
if args.json {
print_result_in_json_format(extraneous, implicit)
} else {
print_result(actual_imports_map, extraneous, implicit);
}
}
Err(err) => error!("{:?}", err),
}
}

fn print_result_in_json_format(extraneous: Vec<&String>, implicit: Vec<&String>) {
let json_output = json!({
"extraneous_dependencies": extraneous,
"implicit_dependencies": implicit,
});
let json_to_print = to_string_pretty(&json_output).unwrap();
info!("{}", json_to_print);
}

fn print_result(
actual_imports_map: HashMap<String, ImportStatement>,
extraneous: Vec<&String>,
implicit: Vec<&String>,
) {
info!("{}", Style::new().bold().paint("Extraneous dependencies:"));
for dep in extraneous {
info!("├── {}", Style::new().underline().paint(dep));
}
info!("{}", Style::new().bold().paint("Implicit dependencies:"));
for dep in implicit {
let details = actual_imports_map.get(dep).unwrap();
info!("├── {}", Style::new().underline().paint(dep));
info!("│ └── file://{}:{}", details.file, details.line);
}
}

fn configure_logging(args: &Args) {
let mut logging_builder = Builder::from_default_env();

Expand Down
4 changes: 2 additions & 2 deletions src/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use log::info;
use log::debug;
use std::collections::HashMap;
use std::collections::HashSet;
use std::env;
Expand Down Expand Up @@ -41,7 +41,7 @@ fn find_closest_parent_manifest(path: &Path) -> Option<&Path> {

pub fn read_manifest_dependencies(project_root: PathBuf) -> Result<HashSet<String>> {
let manifest_path = project_root.join("package.json");
info!("Found manifest path at {}", manifest_path.display());
debug!("Found manifest path at {}", manifest_path.display());
let raw =
fs::read_to_string(manifest_path).expect("Should have been able to read the manifest");
let manifest: Manifest = serde_json::from_str(&raw).expect("Cannot parse manifest");
Expand Down

0 comments on commit dd710b6

Please sign in to comment.