Skip to content

Commit

Permalink
feat: add --skip-lockfile flag (#9)
Browse files Browse the repository at this point in the history
* feat: add --skip-lockfile flag

* add flag to readme
  • Loading branch information
DerTimonius authored Apr 4, 2024
1 parent 224f52d commit 4e64bb4
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 17 deletions.
5 changes: 3 additions & 2 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 @@ -13,4 +13,5 @@ clap = { version = "4.5.4", features = ["cargo"] }
colored = "2.1.0"
ignore = "0.4.22"
nom = "7.1.3"
regex = "1.10.4"
term-table = "1.3.2"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ code-peek [FLAGS] [OPTIONS] [ARGS]
- _-a, --all_: Display all available information.
- _-g, --group_: Group the results by file extension or programming language.
- _-t, --git_: Get Git information (number of commits) for each file.
- _--skip-lockfiles_: Skips lockfiles in analysis.

### Options

Expand Down
13 changes: 12 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct DisplayOptions {
pub group: bool,
pub git: bool,
pub all: bool,
pub skip_lockfiles: bool,
}

pub fn run_cli() -> Result<Cli> {
Expand All @@ -37,6 +38,7 @@ pub fn run_cli() -> Result<Cli> {
)
.arg(arg!([all] -a --all "Display all available information").required(false))
.arg(arg!([group] -g --group "Group the results by its extension").required(false))
.arg(arg!(--"skip-lockfiles" "Skips lockfiles in analysis").long("skip-lockfiles").required(false))
.arg(arg!([git] -t --git "Get git info - how many commits were made to each file").required(false))
.arg(
arg!([match]
Expand All @@ -59,6 +61,10 @@ pub fn run_cli() -> Result<Cli> {
let all = matches.get_one::<bool>("all").unwrap().to_owned();
let group = all || matches.get_one::<bool>("group").unwrap().to_owned();
let git = all || matches.get_one::<bool>("git").unwrap().to_owned();
let skip_lockfiles = matches
.get_one::<bool>("skip-lockfiles")
.unwrap()
.to_owned();

let exclude = if let Some(globs) = matches.get_one::<String>("exclude") {
globs
Expand All @@ -81,7 +87,12 @@ pub fn run_cli() -> Result<Cli> {
let cli = Cli {
dir: dir.to_string(),
num,
display_options: DisplayOptions { all, group, git },
display_options: DisplayOptions {
all,
group,
git,
skip_lockfiles,
},
exclude,
matches,
};
Expand Down
9 changes: 4 additions & 5 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ pub fn display_info(
let mut grouped_files: HashMap<FileType, Vec<File>> = HashMap::new();

for file in files.iter() {
if let Some(x) = grouped_files.get_mut(&file.clone().get_file_type()) {
x.push(file.clone());
} else {
grouped_files.insert(file.clone().get_file_type(), vec![file.clone()]);
}
grouped_files
.entry(file.file_type)
.or_insert_with(|| Vec::new())
.push(file.to_owned());
}
println!(
"{} {}\n",
Expand Down
62 changes: 54 additions & 8 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
};

use ignore::{DirEntry, WalkBuilder};
use regex::Regex;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub enum FileType {
Expand All @@ -27,15 +28,16 @@ pub enum FileType {
JavaScript,
Julia,
JupyterNotebook,
Lockfile,
Lua,
Markdown,
Mojo,
Prisma,
Python,
Rust,
SQL,
Svelte,
SVG,
Svelte,
Swift,
TOML,
TypeScript,
Expand All @@ -52,16 +54,24 @@ pub struct File {
pub path: String,
pub loc: usize,
pub extension: OsString,
pub file_type: FileType,
pub commits: Option<usize>,
}

impl File {
pub fn get_file_type(&self) -> FileType {
pub fn add_file_type(&mut self) {
if self.name.ends_with("svelte.ts") || self.name.ends_with("svelte.js") {
return FileType::Svelte;
self.file_type = FileType::Svelte;
return;
}

let lockfile_regex = Regex::new("(pnpm-lock|package-lock|project.assets|packages.lock|npm-shrinkwrap|go|elm-package).(json|yaml|yml|sum)").unwrap();
if lockfile_regex.is_match(self.name.as_str()) {
self.file_type = FileType::Lockfile;
return;
}

match self.extension.to_str() {
self.file_type = match self.extension.to_str() {
Some("js" | "cjs" | "mjs" | "jsx") => FileType::JavaScript,
Some("ts" | "cts" | "mts" | "tsx") => FileType::TypeScript,
Some("json" | "jsonb" | "jsonc") => FileType::JSON,
Expand All @@ -78,6 +88,7 @@ impl File {
Some("gql" | "graphql" | "graphqls") => FileType::GraphQL,
Some("ex" | "exs") => FileType::Elixir,
Some("zig") => FileType::Zig,
Some("lock") => FileType::Lockfile,
Some("gleam") => FileType::Gleam,
Some("swift") => FileType::Swift,
Some("c" | "ec" | "idc" | "pdc") => FileType::C,
Expand Down Expand Up @@ -115,7 +126,11 @@ impl Display for FileType {
}
}

pub fn get_files(dir: &str, overrides: ignore::overrides::Override) -> Vec<File> {
pub fn get_files(
dir: &str,
overrides: ignore::overrides::Override,
skip_lockfiles: &bool,
) -> Vec<File> {
let mut files: Vec<File> = Vec::new();

for result in WalkBuilder::new(dir)
Expand All @@ -128,7 +143,11 @@ pub fn get_files(dir: &str, overrides: ignore::overrides::Override) -> Vec<File>
if entry.path().to_str().unwrap().contains(".git/") {
continue;
}
if let Some(file) = get_file_info(&entry, dir) {
if let Some(mut file) = get_file_info(&entry, dir) {
file.add_file_type();
if *skip_lockfiles && file.file_type == FileType::Lockfile {
continue;
}
files.push(file)
}
}
Expand Down Expand Up @@ -162,6 +181,7 @@ fn get_file_info(entry: &DirEntry, dir: &str) -> Option<File> {
extension,
loc: lines,
commits: None,
file_type: FileType::Other,
});
}

Expand All @@ -174,14 +194,40 @@ mod tests {

#[test]
fn file_type() {
let file = File {
let mut file = File {
name: String::from("foo.rs"),
path: String::from("foo.rs"),
loc: 12,
extension: OsString::from("rs"),
commits: None,
file_type: FileType::Other,
};
file.add_file_type();

assert_eq!(file.file_type, FileType::Rust);

let mut file = File {
name: String::from("Cargo.lock"),
path: String::from("Cargo.lock"),
loc: 12,
extension: OsString::from("lock"),
commits: None,
file_type: FileType::Other,
};
file.add_file_type();

assert_eq!(file.file_type, FileType::Lockfile);

let mut file = File {
name: String::from("pnpm-lock.yaml"),
path: String::from("pnpm-lock.yaml"),
loc: 12,
extension: OsString::from("yaml"),
commits: None,
file_type: FileType::Other,
};
file.add_file_type();

assert_eq!(file.get_file_type(), FileType::Rust)
assert_eq!(file.file_type, FileType::Lockfile)
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() {
}
let overrides = builder.build().unwrap();

let mut files = get_files(dir, overrides);
let mut files = get_files(dir, overrides, &cli.display_options.skip_lockfiles);

let total_commits = if cli.display_options.git || cli.display_options.all {
add_git_info(&mut files, dir)
Expand Down

0 comments on commit 4e64bb4

Please sign in to comment.