Skip to content

Commit

Permalink
refactor: 添加 watch 命令和相关代码
Browse files Browse the repository at this point in the history
为 typikon 添加了 watch 命令和相关代码,用于监听文件变化并重新构建书籍。

Fixes auula#10
  • Loading branch information
CHEN-Jing194 committed Sep 8, 2024
1 parent 1b82e7a commit b621dee
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The commands are:
init Initialize to working directory
serve Serve starting the static http server
build Builder static html file and output to book
watch Watch the file changes and rebuild the book
Use typikon help <command> for more information about a command.
";
Expand Down
18 changes: 16 additions & 2 deletions src/cli/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub enum Command {
Init,
Help,
Serve,
Watch,
Unknown(String),
}

Expand All @@ -30,6 +31,7 @@ impl Command {
"init" => Command::Init,
"help" => Command::Help,
"serve" => Command::Serve,
"watch" => Command::Watch,
_ => Command::Unknown(s.to_string()),
}
}
Expand All @@ -42,6 +44,7 @@ impl fmt::Display for Command {
Command::Serve => write!(f, "serve"),
Command::Init => write!(f, "init"),
Command::Help => write!(f, "help"),
Command::Watch => write!(f, "watch"),
Command::Unknown(s) => write!(f, "Unknown({})", s),
}
}
Expand Down Expand Up @@ -77,12 +80,22 @@ const SERVE_HELP_TEXT: &str = r"
";

const WATCH_HELP_TEXT: &str = r"
Example:
Watch the file changes and rebuild the book 👇
$: mkdir example && typikon watch
";

static HELP_INFO: Lazy<Mutex<HashMap<Command, colored::ColoredString>>> = Lazy::new(|| {
let mut help_info: HashMap<Command, colored::ColoredString> = HashMap::new();

help_info.insert(Command::Build, BUILD_HELP_TEXT.green());
help_info.insert(Command::Serve, SERVE_HELP_TEXT.green());
help_info.insert(Command::Init, INIT_HELP_TEXT.green());
help_info.insert(Command::Watch, WATCH_HELP_TEXT.green());

Mutex::new(help_info)
});
Expand Down Expand Up @@ -137,7 +150,7 @@ pub fn handle_serve_command(_args: &[String]) {
log.info(format_args!("HTTP server stopped."));
}

pub fn handle_live_serve_command() {
pub fn handle_watch_command() {
let mut log = Logger::console_log();
let settings = match settings::get_settings() {
Ok(settings) => settings,
Expand Down Expand Up @@ -209,13 +222,14 @@ pub fn handle_help_command(args: &[String]) {
let help = HELP_INFO.lock().unwrap();

match command {
Command::Build | Command::Init | Command::Help | Command::Serve => {
Command::Build | Command::Init | Command::Help | Command::Serve | Command::Watch => {
if let Some(help_text) = help.get(&command) {
println!("{}", help_text);
} else {
log.error(format_args!("No help available for command: {}", option));
}
}

Command::Unknown(_) => {
log.error(format_args!(
"Unknown option: {:?}. Available options: [init, serve, build]",
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn main() {
Command::Help => typikon::cli::handle_help_command(&args),
Command::Build => typikon::cli::handle_build_command(&args),
Command::Serve => typikon::cli::handle_serve_command(&args),
Command::Watch => typikon::cli::handle_watch_command(),
Command::Unknown(_) => typikon::cli::output_banner_help(),
}
}
4 changes: 2 additions & 2 deletions tests/book/live_server_test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#[cfg(test)]
mod tests {
use typikon::cli::commands::handle_live_serve_command;
use typikon::cli::commands::handle_watch_command;

#[test]
fn test_live_server() {
assert_eq!(handle_live_serve_command(), ());
assert_eq!(handle_watch_command(), ());
}
}

0 comments on commit b621dee

Please sign in to comment.