Skip to main content

cli/lib/compiler/
watch_compiler.rs

1//! Rust-native watch compiler planning.
2
3use super::base_compiler::{BaseCompiler, BaseCompilerContext};
4use super::helpers::manual_restart::display_manual_restart_tip;
5use super::helpers::tsconfig_provider::{TsConfigData, TsConfigProvider};
6use super::plugins::plugins_loader::MultiNestCompilerPlugins;
7use super::rust_toolchain_loader::RustToolchainLoader;
8pub use super::{AssetDeleteOnUnlink, WatchOptions};
9
10#[derive(Clone, Debug, PartialEq, Eq)]
11pub struct WatchCompiler {
12    pub base: BaseCompiler,
13    pub ts_config_provider: TsConfigProvider,
14    pub rust_toolchain_loader: RustToolchainLoader,
15}
16
17#[derive(Clone, Debug, PartialEq, Eq)]
18pub struct WatchCompilerRunPlan {
19    pub ts_config_path: String,
20    pub app_name: Option<String>,
21    pub preserve_watch_output: Option<bool>,
22    pub manual_restart: bool,
23    pub manual_restart_tip: Option<String>,
24    pub ts_config: TsConfigData,
25    pub plugins: MultiNestCompilerPlugins,
26}
27
28impl WatchCompiler {
29    pub fn new(
30        base: BaseCompiler,
31        ts_config_provider: TsConfigProvider,
32        rust_toolchain_loader: RustToolchainLoader,
33    ) -> Self {
34        Self {
35            base,
36            ts_config_provider,
37            rust_toolchain_loader,
38        }
39    }
40
41    pub fn plan_run(
42        &mut self,
43        context: &BaseCompilerContext,
44        ts_config_path: &str,
45        app_name: Option<&str>,
46        preserve_watch_output: Option<bool>,
47    ) -> Result<WatchCompilerRunPlan, String> {
48        self.rust_toolchain_loader.load()?;
49        let ts_config = self
50            .ts_config_provider
51            .get_by_config_filename(ts_config_path)?;
52        let compiler_options = context.compiler_options_for(app_name);
53        let plugins = self.base.load_plugins(context, ts_config_path, app_name)?;
54        Ok(WatchCompilerRunPlan {
55            ts_config_path: ts_config_path.to_string(),
56            app_name: app_name.map(ToString::to_string),
57            preserve_watch_output,
58            manual_restart: compiler_options.manual_restart,
59            manual_restart_tip: compiler_options
60                .manual_restart
61                .then(display_manual_restart_tip),
62            ts_config,
63            plugins,
64        })
65    }
66
67    pub fn is_success_status(message: &str) -> bool {
68        message.contains("0 errors")
69    }
70}