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::colors;
|
||||||
use crate::diff::diff;
|
use crate::diff::diff;
|
||||||
use crate::fs::canonicalize_path;
|
|
||||||
use crate::fs::files_in_subtree;
|
|
||||||
use crate::text_encoding;
|
use crate::text_encoding;
|
||||||
use deno_core::error::generic_error;
|
use deno_core::error::generic_error;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
|
@ -25,6 +23,7 @@ use std::path::Path;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
const BOM_CHAR: char = '\u{FEFF}';
|
const BOM_CHAR: char = '\u{FEFF}';
|
||||||
|
|
||||||
|
@ -40,14 +39,8 @@ pub async fn format(
|
||||||
if args.len() == 1 && args[0].to_string_lossy() == "-" {
|
if args.len() == 1 && args[0].to_string_lossy() == "-" {
|
||||||
return format_stdin(check);
|
return format_stdin(check);
|
||||||
}
|
}
|
||||||
// collect all files provided.
|
// collect the files that are to be formatted
|
||||||
let mut target_files = collect_files(args)?;
|
let target_files = collect_files(args, exclude)?;
|
||||||
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));
|
|
||||||
}
|
|
||||||
let config = get_config();
|
let config = get_config();
|
||||||
if check {
|
if check {
|
||||||
check_source_files(config, target_files).await
|
check_source_files(config, target_files).await
|
||||||
|
@ -234,22 +227,41 @@ fn is_supported(path: &Path) -> bool {
|
||||||
|
|
||||||
pub fn collect_files(
|
pub fn collect_files(
|
||||||
files: Vec<PathBuf>,
|
files: Vec<PathBuf>,
|
||||||
|
mut ignore: Vec<PathBuf>,
|
||||||
) -> Result<Vec<PathBuf>, std::io::Error> {
|
) -> Result<Vec<PathBuf>, std::io::Error> {
|
||||||
let mut target_files: Vec<PathBuf> = vec![];
|
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() {
|
if files.is_empty() {
|
||||||
target_files.extend(files_in_subtree(
|
for entry in WalkDir::new(std::env::current_dir()?)
|
||||||
canonicalize_path(&std::env::current_dir()?)?,
|
.into_iter()
|
||||||
is_supported,
|
.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 {
|
} else {
|
||||||
for file in files {
|
for file in files {
|
||||||
if file.is_dir() {
|
for entry in WalkDir::new(file)
|
||||||
target_files
|
.into_iter()
|
||||||
.extend(files_in_subtree(canonicalize_path(&file)?, is_supported));
|
.filter_entry(|e| !ignore.iter().any(|i| e.path().starts_with(i)))
|
||||||
} else {
|
{
|
||||||
target_files.push(canonicalize_path(&file)?);
|
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() == "-" {
|
if args.len() == 1 && args[0].to_string_lossy() == "-" {
|
||||||
return lint_stdin(json);
|
return lint_stdin(json);
|
||||||
}
|
}
|
||||||
let mut target_files = collect_files(args)?;
|
let target_files = collect_files(args, ignore)?;
|
||||||
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));
|
|
||||||
}
|
|
||||||
debug!("Found {} files", target_files.len());
|
debug!("Found {} files", target_files.len());
|
||||||
let target_files_len = target_files.len();
|
let target_files_len = target_files.len();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue