From d72b3873fcbc1dec1e09557d517dcd0203c8b5a3 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Fri, 15 Nov 2024 18:22:53 -0500 Subject: [PATCH] optional command patterns, improved default config examples (#17) --- src/config.rs | 40 +++++++++++++++++++++++++++------------- src/tasks.rs | 22 +++++++++++++--------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/config.rs b/src/config.rs index 01080a3..ac9bbfc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -24,18 +24,32 @@ impl Default for Config { fn default() -> Self { Config { base: Some("main".to_string()), - tasks: Some(vec![Task { - name: "lint".to_string(), - description: Some("Runs eslint for all affected files".to_string()), - patterns: vec![ - "*.ts".to_string(), - "*.tsx".to_string(), - "*.js".to_string(), - "*.jsx".to_string(), - ], - commands: vec!["echo {files}".to_string()], - ..Default::default() - }]), + tasks: Some(vec![ + Task { + name: "lint".to_string(), + description: Some("Runs eslint for all affected files (example)".to_string()), + patterns: Some(vec![ + "*.ts".to_string(), + "*.tsx".to_string(), + "*.js".to_string(), + "*.jsx".to_string(), + ]), + commands: vec!["eslint {files}".to_string()], + ..Default::default() + }, + Task { + name: "prettier".to_string(), + description: Some("Runs prettier for all affected files (example)".to_string()), + patterns: Some(vec![ + "*.ts".to_string(), + "*.tsx".to_string(), + "*.js".to_string(), + "*.jsx".to_string(), + ]), + commands: vec!["prettier --check {files}".to_string()], + ..Default::default() + }, + ]), } } } @@ -45,7 +59,7 @@ impl Default for Config { pub struct Task { pub name: String, pub description: Option, - pub patterns: Vec, + pub patterns: Option>, pub separator: Option, pub commands: Vec, } diff --git a/src/tasks.rs b/src/tasks.rs index d8adef8..9cb6bb5 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -30,16 +30,20 @@ pub async fn run_task_by_name( return Ok(()); } - let filtered_paths: Vec<_> = file_paths - .into_iter() - .filter(|path| { - task.patterns.iter().any(|pattern| { - Pattern::new(pattern) - .map(|p| p.matches(path)) - .unwrap_or(false) + let filtered_paths: Vec<_> = if let Some(patterns) = &task.patterns { + file_paths + .into_iter() + .filter(|path| { + patterns.iter().any(|pattern| { + Pattern::new(pattern) + .map(|p| p.matches(path)) + .unwrap_or(false) + }) }) - }) - .collect(); + .collect() + } else { + file_paths + }; if filtered_paths.is_empty() { println!("No files matched the patterns");