Skip to main content

next_butler/commands/init_command/
mod.rs

1use std::{fs, path::PathBuf};
2
3use clap::Command;
4
5use crate::{helpers::file_helper, template::{create_pages_templates, create_components_templates, create_stylesheets_templates}, user_config::UserConfig};
6use colored::Colorize;
7
8/// Sets the subcommand and the corresponding arguments
9pub fn set_subcommand(app: Command) -> Command {
10    // Set the subcommand 'new'
11    let new_subcommand = Command::new("init")
12        .about("Creates the tool's configuration structure")
13        .after_help(
14            "This command does the following: \n\
15                    - Creates the tool's directory (nextbutler) inside the root dir \n\
16                    - Creates the configuration file (nextbutler/nextbutler.json) \n\
17                    - Creates the default templates as custom ones, as examples",
18        );
19
20    app.subcommand(new_subcommand)
21}
22
23/// Executes the command
24pub fn exec_command() -> Result<(), String> {
25    println!("Creating basic configuration...");
26
27    let nextbutler_path = PathBuf::from("nextbutler/");
28    fs::create_dir_all(nextbutler_path.clone())
29        .map_err(|err| format!("Error creating nextbutler directory: {}", err))?;
30
31    // Create configuration file
32    let user_config_path = nextbutler_path.join("nextbutler.json");
33    file_helper::create(&user_config_path, UserConfig::get_default_as_vec()?)
34        .map_err(|err| format!("Error creating configuration file: {}", err))?;
35
36    // Create page templates folder
37    println!("Creating templates...");
38    create_pages_templates(nextbutler_path.join("templates/pages/"))?;
39    create_components_templates(nextbutler_path.join("templates/components/"))?;
40    create_stylesheets_templates(nextbutler_path.join("templates/styles/"))?;
41
42    println!("{}", "Configuration structure created successfuly".green());
43
44    Ok(())
45}