Skip to content

Commit 9c8dbbf

Browse files
committed
support --no-watch
1 parent 20c3f1e commit 9c8dbbf

File tree

5 files changed

+46
-8
lines changed

5 files changed

+46
-8
lines changed

crates/bsnext_input/src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ pub struct InputConfig {
129129
}
130130

131131
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
132+
#[serde(untagged)]
132133
pub enum WatchGlobalConfig {
133134
Enabled { infer: InferWatchers },
134135
Disabled,
@@ -384,3 +385,32 @@ impl Display for PathDefinition {
384385
pub fn rand_word() -> String {
385386
random_word::gen(random_word::Lang::En).to_string()
386387
}
388+
389+
#[cfg(test)]
390+
mod watch_global_config_test {
391+
use super::*;
392+
#[test]
393+
fn test_deserialize_watch_global_config_disabled() {
394+
let yaml_routes = r#"
395+
watchers: null
396+
"#;
397+
398+
let config: InputConfig = serde_yaml::from_str(yaml_routes).unwrap();
399+
assert!(matches!(config.watchers, WatchGlobalConfig::Disabled));
400+
}
401+
#[test]
402+
fn test_deserialize_watch_global_config_infer_variants() {
403+
let yaml_routes = r#"
404+
watchers:
405+
infer: Routes
406+
"#;
407+
408+
let config: InputConfig = serde_yaml::from_str(yaml_routes).unwrap();
409+
assert!(matches!(
410+
config.watchers,
411+
WatchGlobalConfig::Enabled {
412+
infer: InferWatchers::Routes
413+
}
414+
));
415+
}
416+
}

crates/bsnext_system/src/args.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub struct Args {
3333
#[clap(flatten)]
3434
pub watch_opts: WatchSubOpts,
3535

36+
/// pass '--no-watch' to prevent auto watchers. can't be used with 'input'
3637
#[clap(long)]
3738
pub no_watch: bool,
3839

crates/bsnext_system/src/start/start_kind.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ impl StartKind {
7676
StartKind::FromInputPaths(StartFromInputPaths {
7777
input_paths: input_opts.input.clone(),
7878
port: cmd.port,
79+
no_watch: cmd.no_watch,
7980
})
8081
}
8182
}

crates/bsnext_system/src/start/start_kind/snapshots/bsnext_system__start__start_kind__start_from_paths__test__test-2.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ watchers: []
2424
run: {}
2525
config:
2626
watchers:
27-
Enabled:
28-
infer: RoutesAndServers
27+
infer: RoutesAndServers
2928
global_fs_ignore: ~
3029
global_fs_only: ~
3130
global_fs_debounce: ~

crates/bsnext_system/src/start/start_kind/start_from_inputs.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
use bsnext_input::startup::{StartupContext, SystemStart, SystemStartArgs};
22

33
use crate::input_fs::from_input_path;
4-
use bsnext_input::{Input, InputArgs, InputCtx, InputError};
4+
use bsnext_input::{Input, InputArgs, InputCtx, InputError, WatchGlobalConfig};
55
use std::path::{Path, PathBuf};
66

77
#[derive(Debug)]
88
// todo: must ensure StartFromInputPaths
99
pub struct StartFromInputPaths {
1010
pub input_paths: Vec<String>,
1111
pub port: Option<u16>,
12+
pub no_watch: bool,
1213
}
1314

1415
impl SystemStart for StartFromInputPaths {
1516
fn resolve_input(&self, ctx: &StartupContext) -> Result<SystemStartArgs, Box<InputError>> {
16-
from_input_paths(ctx, &self.input_paths, &self.port)
17+
from_input_paths(ctx, &self.input_paths, &self.port, self.no_watch)
1718
}
1819
}
1920

@@ -34,6 +35,7 @@ fn from_input_paths<T: AsRef<str>>(
3435
ctx: &StartupContext,
3536
inputs: &[T],
3637
port: &Option<u16>,
38+
no_watch: bool,
3739
) -> Result<SystemStartArgs, Box<InputError>> {
3840
let cwd = &ctx.cwd;
3941
let input_candidates = inputs
@@ -86,10 +88,15 @@ fn from_input_paths<T: AsRef<str>>(
8688
let initial_ctx = InputCtx::new(&[], Some(input_args), ctx, Some(input_path));
8789
let result = from_input_path(input_path, &initial_ctx);
8890
match result {
89-
Ok(input) => Ok(SystemStartArgs::PathWithInput {
90-
path: input_path.to_path_buf(),
91-
input,
92-
}),
91+
Ok(mut input) => {
92+
if no_watch {
93+
input.config.watchers = WatchGlobalConfig::Disabled;
94+
}
95+
Ok(SystemStartArgs::PathWithInput {
96+
path: input_path.to_path_buf(),
97+
input,
98+
})
99+
}
93100
Err(e) => match *e {
94101
InputError::YamlError(yaml_error) => Ok(SystemStartArgs::PathWithInvalidInput {
95102
path: input_path.to_path_buf(),

0 commit comments

Comments
 (0)