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
use std::env;
use std::fs;
use std::path::PathBuf;

use serde::{Deserialize, Serialize};

use crate::commands::generate::run_generate;

const SITE_ENTRY_POINT: &str = "workers-site";

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Site {
    pub bucket: PathBuf,
    #[serde(rename = "entry-point")]
    entry_point: Option<PathBuf>,
    pub include: Option<Vec<String>>,
    pub exclude: Option<Vec<String>>,
}

impl Site {
    pub fn new(bucket: &str) -> Site {
        let mut site = Site::default();
        site.bucket = PathBuf::from(bucket);

        site
    }

    // if the user has configured `site.entry-point`, use that
    // as the build directory. Otherwise use the default const
    // SITE_ENTRY_POINT
    pub fn entry_point(&self) -> Result<PathBuf, std::io::Error> {
        let current_dir = env::current_dir()?;
        Ok(current_dir.join(
            self.entry_point
                .to_owned()
                .unwrap_or_else(|| PathBuf::from(SITE_ENTRY_POINT)),
        ))
    }

    pub fn scaffold_worker(&self) -> Result<(), failure::Error> {
        let entry_point = &self.entry_point()?;
        let template = "https://github.com/cloudflare/worker-sites-init";

        if !entry_point.exists() {
            log::info!("Generating a new workers site project");
            run_generate(entry_point.file_name().unwrap().to_str().unwrap(), template)?;

            // This step is to prevent having a git repo within a git repo after
            // generating the scaffold into an existing project.
            fs::remove_dir_all(&entry_point.join(".git"))?;
        }

        Ok(())
    }
}

impl Default for Site {
    fn default() -> Site {
        Site {
            bucket: PathBuf::new(),
            entry_point: Some(PathBuf::from(SITE_ENTRY_POINT)),
            include: None,
            exclude: None,
        }
    }
}