Fix zola init

This commit is contained in:
Vincent Prouillet 2022-03-18 17:45:10 +01:00
parent 1f5a524c12
commit 75d9e5a1e1

View File

@ -1,4 +1,4 @@
use std::path::PathBuf; use std::path::{Path, PathBuf};
use std::time::Instant; use std::time::Instant;
use cli::{Cli, Command}; use cli::{Cli, Command};
@ -11,20 +11,24 @@ mod cmd;
mod console; mod console;
mod prompt; mod prompt;
fn get_config_file_path(dir: &PathBuf, config_path: &Path) -> (PathBuf, PathBuf) {
let root_dir = dir
.ancestors()
.find_map(|a| if a.join(&config_path).exists() { Some(a) } else { None })
.unwrap_or_else(|| panic!("could not find directory containing config file"));
// 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() { fn main() {
let cli = Cli::parse(); let cli = Cli::parse();
let cli_dir: PathBuf = cli.root.canonicalize().unwrap_or_else(|_| { let cli_dir: PathBuf = cli.root.canonicalize().unwrap_or_else(|_| {
panic!("Could not find canonical path of root dir: {}", cli.root.display()) panic!("Could not find canonical path of root dir: {}", cli.root.display())
}); });
let root_dir = cli_dir
.ancestors()
.find_map(|a| if a.join(&cli.config).exists() { Some(a) } else { None })
.unwrap_or_else(|| panic!("could not find directory containing config file"));
// if we got here we found root_dir so config file should exist so we can unwrap safely
let config_file = root_dir.join(&cli.config).canonicalize().unwrap();
match cli.command { match cli.command {
Command::Init { name, force } => { Command::Init { name, force } => {
if let Err(e) = cmd::create_new_project(&name, force) { if let Err(e) = cmd::create_new_project(&name, force) {
@ -35,8 +39,9 @@ fn main() {
Command::Build { base_url, output_dir, drafts } => { Command::Build { base_url, output_dir, drafts } => {
console::info("Building site..."); console::info("Building site...");
let start = Instant::now(); let start = Instant::now();
let (root_dir, config_file) = get_config_file_path(&cli_dir, &cli.config);
match cmd::build( match cmd::build(
root_dir, &root_dir,
&config_file, &config_file,
base_url.as_deref(), base_url.as_deref(),
output_dir.as_deref(), output_dir.as_deref(),
@ -62,9 +67,10 @@ fn main() {
}); });
} }
let (root_dir, config_file) = get_config_file_path(&cli_dir, &cli.config);
console::info("Building site..."); console::info("Building site...");
if let Err(e) = cmd::serve( if let Err(e) = cmd::serve(
root_dir, &root_dir,
&interface, &interface,
port, port,
output_dir.as_deref(), output_dir.as_deref(),
@ -81,7 +87,8 @@ fn main() {
Command::Check { drafts } => { Command::Check { drafts } => {
console::info("Checking site..."); console::info("Checking site...");
let start = Instant::now(); let start = Instant::now();
match cmd::check(root_dir, &config_file, None, None, drafts) { let (root_dir, config_file) = get_config_file_path(&cli_dir, &cli.config);
match cmd::check(&root_dir, &config_file, None, None, drafts) {
Ok(()) => console::report_elapsed_time(start), Ok(()) => console::report_elapsed_time(start),
Err(e) => { Err(e) => {
console::unravel_errors("Failed to check the site", &e); console::unravel_errors("Failed to check the site", &e);