You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.5KB

  1. extern crate actix_files;
  2. extern crate actix_web;
  3. extern crate atty;
  4. #[macro_use]
  5. extern crate clap;
  6. extern crate chrono;
  7. #[macro_use]
  8. extern crate lazy_static;
  9. extern crate ctrlc;
  10. extern crate notify;
  11. extern crate termcolor;
  12. extern crate url;
  13. extern crate ws;
  14. extern crate site;
  15. #[macro_use]
  16. extern crate errors;
  17. extern crate front_matter;
  18. extern crate rebuild;
  19. extern crate utils;
  20. use std::time::Instant;
  21. use utils::net::{get_available_port, port_is_available};
  22. mod cli;
  23. mod cmd;
  24. mod console;
  25. mod prompt;
  26. fn main() {
  27. let matches = cli::build_cli().get_matches();
  28. let config_file = matches.value_of("config").unwrap();
  29. match matches.subcommand() {
  30. ("init", Some(matches)) => {
  31. match cmd::create_new_project(matches.value_of("name").unwrap()) {
  32. Ok(()) => (),
  33. Err(e) => {
  34. console::unravel_errors("Failed to create the project", &e);
  35. ::std::process::exit(1);
  36. }
  37. };
  38. }
  39. ("build", Some(matches)) => {
  40. console::info("Building site...");
  41. let start = Instant::now();
  42. let output_dir = matches.value_of("output_dir").unwrap();
  43. match cmd::build(config_file, matches.value_of("base_url"), output_dir) {
  44. Ok(()) => console::report_elapsed_time(start),
  45. Err(e) => {
  46. console::unravel_errors("Failed to build the site", &e);
  47. ::std::process::exit(1);
  48. }
  49. };
  50. }
  51. ("serve", Some(matches)) => {
  52. let interface = matches.value_of("interface").unwrap_or("127.0.0.1");
  53. let mut port: u16 = match matches.value_of("port").unwrap().parse() {
  54. Ok(x) => x,
  55. Err(_) => {
  56. console::error("The request port needs to be an integer");
  57. ::std::process::exit(1);
  58. }
  59. };
  60. // Default one
  61. if port != 1111 && !port_is_available(port) {
  62. console::error("The requested port is not available");
  63. ::std::process::exit(1);
  64. }
  65. if !port_is_available(port) {
  66. port = if let Some(p) = get_available_port(1111) {
  67. p
  68. } else {
  69. console::error("No port available.");
  70. ::std::process::exit(1);
  71. }
  72. }
  73. let watch_only = matches.is_present("watch_only");
  74. let output_dir = matches.value_of("output_dir").unwrap();
  75. let base_url = matches.value_of("base_url").unwrap();
  76. console::info("Building site...");
  77. match cmd::serve(interface, port, output_dir, base_url, config_file, watch_only) {
  78. Ok(()) => (),
  79. Err(e) => {
  80. console::unravel_errors("", &e);
  81. ::std::process::exit(1);
  82. }
  83. };
  84. }
  85. ("check", Some(matches)) => {
  86. console::info("Checking site...");
  87. let start = Instant::now();
  88. match cmd::check(
  89. config_file,
  90. matches.value_of("base_path"),
  91. matches.value_of("base_url"),
  92. ) {
  93. Ok(()) => console::report_elapsed_time(start),
  94. Err(e) => {
  95. console::unravel_errors("Failed to check the site", &e);
  96. ::std::process::exit(1);
  97. }
  98. };
  99. }
  100. _ => unreachable!(),
  101. }
  102. }