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.

114 lines
3.6KB

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