-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathmain.rs
127 lines (118 loc) · 4.21 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use std::path::{Path, PathBuf};
use std::time::Instant;
use cli::{Cli, Command};
use errors::anyhow;
use utils::net::{get_available_port, port_is_available};
use clap::{CommandFactory, Parser};
use time::UtcOffset;
mod cli;
mod cmd;
mod messages;
mod prompt;
fn get_config_file_path(dir: &Path, config_path: &Path) -> (PathBuf, PathBuf) {
let root_dir = dir.ancestors().find(|a| a.join(config_path).exists()).unwrap_or_else(|| {
messages::unravel_errors(
"",
&anyhow!(
"{} not found in current directory or ancestors, current_dir is {}",
config_path.display(),
dir.display()
),
);
std::process::exit(1);
});
// if we got here we found root_dir so config file should exist so we can unwrap safely
let config_file = root_dir
.join(config_path)
.canonicalize()
.unwrap_or_else(|_| panic!("could not find directory containing config file"));
(root_dir.to_path_buf(), config_file)
}
fn main() {
let cli = Cli::parse();
let cli_dir: PathBuf = cli.root.canonicalize().unwrap_or_else(|_| {
panic!("Could not find canonical path of root dir: {}", cli.root.display())
});
match cli.command {
Command::Init { name, force } => {
if let Err(e) = cmd::create_new_project(&name, force) {
messages::unravel_errors("Failed to create the project", &e);
std::process::exit(1);
}
}
Command::Build { base_url, output_dir, force, drafts } => {
console::info("Building site...");
let start = Instant::now();
let (root_dir, config_file) = get_config_file_path(&cli_dir, &cli.config);
match cmd::build(
&root_dir,
&config_file,
base_url.as_deref(),
output_dir.as_deref(),
force,
drafts,
) {
Ok(()) => messages::report_elapsed_time(start),
Err(e) => {
messages::unravel_errors("Failed to build the site", &e);
std::process::exit(1);
}
}
}
Command::Serve {
interface,
mut port,
output_dir,
base_url,
drafts,
open,
fast,
no_port_append,
} => {
if port != 1111 && !port_is_available(port) {
console::error("The requested port is not available");
std::process::exit(1);
}
if !port_is_available(port) {
port = get_available_port(1111).unwrap_or_else(|| {
console::error("No port available");
std::process::exit(1);
});
}
let (root_dir, config_file) = get_config_file_path(&cli_dir, &cli.config);
console::info("Building site...");
if let Err(e) = cmd::serve(
&root_dir,
&interface,
port,
output_dir.as_deref(),
&base_url,
&config_file,
open,
drafts,
fast,
no_port_append,
UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC),
) {
messages::unravel_errors("Failed to serve the site", &e);
std::process::exit(1);
}
}
Command::Check { drafts } => {
console::info("Checking site...");
let start = Instant::now();
let (root_dir, config_file) = get_config_file_path(&cli_dir, &cli.config);
match cmd::check(&root_dir, &config_file, None, None, drafts) {
Ok(()) => messages::report_elapsed_time(start),
Err(e) => {
messages::unravel_errors("Failed to check the site", &e);
std::process::exit(1);
}
}
}
Command::Completion { shell } => {
let cmd = &mut Cli::command();
clap_complete::generate(shell, cmd, cmd.get_name().to_string(), &mut std::io::stdout());
}
}
}