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.

109 lines
3.2KB

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