mirror of
https://github.com/denoland/deno.git
synced 2024-12-24 08:09:08 -05:00
fix(cli): don't walk the subdirectory twice when using the --ignore
flag (#8040)
This commit reworks "collect_files" utility to accept "ignore" parameter which allows to filter out files in a single iteration instead of walking file tree second time to excude "ignored" files.
This commit is contained in:
parent
a55e689e38
commit
c744ee2756
2 changed files with 33 additions and 27 deletions
52
cli/fmt.rs
52
cli/fmt.rs
|
@ -9,8 +9,6 @@
|
|||
|
||||
use crate::colors;
|
||||
use crate::diff::diff;
|
||||
use crate::fs::canonicalize_path;
|
||||
use crate::fs::files_in_subtree;
|
||||
use crate::text_encoding;
|
||||
use deno_core::error::generic_error;
|
||||
use deno_core::error::AnyError;
|
||||
|
@ -25,6 +23,7 @@ use std::path::Path;
|
|||
use std::path::PathBuf;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use walkdir::WalkDir;
|
||||
|
||||
const BOM_CHAR: char = '\u{FEFF}';
|
||||
|
||||
|
@ -40,14 +39,8 @@ pub async fn format(
|
|||
if args.len() == 1 && args[0].to_string_lossy() == "-" {
|
||||
return format_stdin(check);
|
||||
}
|
||||
// collect all files provided.
|
||||
let mut target_files = collect_files(args)?;
|
||||
if !exclude.is_empty() {
|
||||
// collect all files to be ignored
|
||||
// and retain only files that should be formatted.
|
||||
let ignore_files = collect_files(exclude)?;
|
||||
target_files.retain(|f| !ignore_files.contains(&f));
|
||||
}
|
||||
// collect the files that are to be formatted
|
||||
let target_files = collect_files(args, exclude)?;
|
||||
let config = get_config();
|
||||
if check {
|
||||
check_source_files(config, target_files).await
|
||||
|
@ -234,22 +227,41 @@ fn is_supported(path: &Path) -> bool {
|
|||
|
||||
pub fn collect_files(
|
||||
files: Vec<PathBuf>,
|
||||
mut ignore: Vec<PathBuf>,
|
||||
) -> Result<Vec<PathBuf>, std::io::Error> {
|
||||
let mut target_files: Vec<PathBuf> = vec![];
|
||||
|
||||
// retain only the paths which exist and ignore the rest
|
||||
ignore.retain(|i| i.exists());
|
||||
|
||||
if files.is_empty() {
|
||||
target_files.extend(files_in_subtree(
|
||||
canonicalize_path(&std::env::current_dir()?)?,
|
||||
is_supported,
|
||||
));
|
||||
for entry in WalkDir::new(std::env::current_dir()?)
|
||||
.into_iter()
|
||||
.filter_entry(|e| {
|
||||
!ignore.iter().any(|i| {
|
||||
e.path()
|
||||
.canonicalize()
|
||||
.unwrap()
|
||||
.starts_with(i.canonicalize().unwrap())
|
||||
})
|
||||
})
|
||||
{
|
||||
let entry_clone = entry?.clone();
|
||||
if is_supported(entry_clone.path()) {
|
||||
target_files.push(entry_clone.path().canonicalize()?)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for file in files {
|
||||
if file.is_dir() {
|
||||
target_files
|
||||
.extend(files_in_subtree(canonicalize_path(&file)?, is_supported));
|
||||
} else {
|
||||
target_files.push(canonicalize_path(&file)?);
|
||||
};
|
||||
for entry in WalkDir::new(file)
|
||||
.into_iter()
|
||||
.filter_entry(|e| !ignore.iter().any(|i| e.path().starts_with(i)))
|
||||
{
|
||||
let entry_clone = entry?.clone();
|
||||
if is_supported(entry_clone.path()) {
|
||||
target_files.push(entry_clone.into_path().canonicalize()?)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -47,13 +47,7 @@ pub async fn lint_files(
|
|||
if args.len() == 1 && args[0].to_string_lossy() == "-" {
|
||||
return lint_stdin(json);
|
||||
}
|
||||
let mut target_files = collect_files(args)?;
|
||||
if !ignore.is_empty() {
|
||||
// collect all files to be ignored
|
||||
// and retain only files that should be linted.
|
||||
let ignore_files = collect_files(ignore)?;
|
||||
target_files.retain(|f| !ignore_files.contains(&f));
|
||||
}
|
||||
let target_files = collect_files(args, ignore)?;
|
||||
debug!("Found {} files", target_files.len());
|
||||
let target_files_len = target_files.len();
|
||||
|
||||
|
|
Loading…
Reference in a new issue