stellar_scaffold_cli/commands/
init.rs

1use clap::Parser;
2use degit::degit;
3use std::fs::{metadata, read_dir};
4use std::io;
5
6const FRONTEND_TEMPLATE: &str = "https://github.com/AhaLabs/scaffold-stellar-frontend";
7
8/// A command to initialize a new project
9#[derive(Parser, Debug, Clone)]
10pub struct Cmd {
11    /// The path to the project must be provided
12    pub project_path: String,
13}
14
15/// Errors that can occur during initialization
16#[derive(thiserror::Error, Debug)]
17pub enum Error {
18    #[error("Failed to clone template: {0}")]
19    DegitError(String),
20    #[error("IO error: {0}")]
21    IoError(#[from] io::Error),
22}
23
24impl Cmd {
25    /// Run the initialization command
26    ///
27    /// # Example:
28    ///
29    /// ```
30    /// /// From the command line
31    /// stellar-scaffold init /path/to/project
32    /// ```
33    #[allow(clippy::unused_self)]
34    pub fn run(&self) -> Result<(), Error> {
35        eprintln!("ℹ️  Creating new Stellar project in {}", self.project_path);
36        degit(FRONTEND_TEMPLATE, &self.project_path);
37
38        // Verify that the project directory exists and is not empty
39        if metadata(&self.project_path).is_err() || read_dir(&self.project_path)?.next().is_none() {
40            return Err(Error::DegitError(format!(
41                "Failed to clone template into {}: directory is empty or missing",
42                self.project_path
43            )));
44        }
45
46        eprintln!("✅ Project successfully created at {}", self.project_path);
47        Ok(())
48    }
49}