diff options
Diffstat (limited to 'cli/tools')
-rw-r--r-- | cli/tools/fmt.rs | 42 | ||||
-rw-r--r-- | cli/tools/lint.rs | 2 | ||||
-rw-r--r-- | cli/tools/test_runner.rs | 3 |
3 files changed, 30 insertions, 17 deletions
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs index 0036436c1..883ebc45b 100644 --- a/cli/tools/fmt.rs +++ b/cli/tools/fmt.rs @@ -9,11 +9,13 @@ use crate::colors; use crate::diff::diff; +use crate::file_watcher; use crate::fs_util::{collect_files, is_supported_ext}; use crate::text_encoding; use deno_core::error::generic_error; use deno_core::error::AnyError; use deno_core::futures; +use deno_core::futures::FutureExt; use dprint_plugin_typescript as dprint; use std::fs; use std::io::stdin; @@ -28,25 +30,37 @@ use std::sync::{Arc, Mutex}; const BOM_CHAR: char = '\u{FEFF}'; /// Format JavaScript/TypeScript files. -/// -/// First argument and ignore supports globs, and if it is `None` -/// then the current directory is recursively walked. pub async fn format( args: Vec<PathBuf>, + ignore: Vec<PathBuf>, check: bool, - exclude: Vec<PathBuf>, + watch: bool, ) -> Result<(), AnyError> { - if args.len() == 1 && args[0].to_string_lossy() == "-" { - return format_stdin(check); - } - // collect the files that are to be formatted - let target_files = collect_files(args, exclude, is_supported_ext)?; - let config = get_config(); - if check { - check_source_files(config, target_files).await + let target_file_resolver = || { + // collect the files that are to be formatted + collect_files(&args, &ignore, is_supported_ext) + }; + + let operation = |paths: Vec<PathBuf>| { + let config = get_config(); + async move { + if check { + check_source_files(config, paths).await?; + } else { + format_source_files(config, paths).await?; + } + Ok(()) + } + .boxed_local() + }; + + if watch { + file_watcher::watch_func(target_file_resolver, operation, "Fmt").await?; } else { - format_source_files(config, target_files).await + operation(target_file_resolver()?).await?; } + + Ok(()) } async fn check_source_files( @@ -166,7 +180,7 @@ async fn format_source_files( /// Format stdin and write result to stdout. /// Treats input as TypeScript. /// Compatible with `--check` flag. -fn format_stdin(check: bool) -> Result<(), AnyError> { +pub fn format_stdin(check: bool) -> Result<(), AnyError> { let mut source = String::new(); if stdin().read_to_string(&mut source).is_err() { return Err(generic_error("Failed to read from stdin")); diff --git a/cli/tools/lint.rs b/cli/tools/lint.rs index f17709c8b..c40dcfd54 100644 --- a/cli/tools/lint.rs +++ b/cli/tools/lint.rs @@ -47,7 +47,7 @@ pub async fn lint_files( if args.len() == 1 && args[0].to_string_lossy() == "-" { return lint_stdin(json); } - let target_files = collect_files(args, ignore, is_supported_ext)?; + let target_files = collect_files(&args, &ignore, is_supported_ext)?; debug!("Found {} files", target_files.len()); let target_files_len = target_files.len(); diff --git a/cli/tools/test_runner.rs b/cli/tools/test_runner.rs index 599a95059..64cff7e0f 100644 --- a/cli/tools/test_runner.rs +++ b/cli/tools/test_runner.rs @@ -44,8 +44,7 @@ pub fn prepare_test_modules_urls( for path in include_paths { let p = fs_util::normalize_path(&root_path.join(path)); if p.is_dir() { - let test_files = - crate::fs_util::collect_files(vec![p], vec![], is_supported).unwrap(); + let test_files = fs_util::collect_files(&[p], &[], is_supported).unwrap(); let test_files_as_urls = test_files .iter() .map(|f| Url::from_file_path(f).unwrap()) |