1use std::{
10 error::Error,
11 fs::{self},
12 process::Command,
13 str::FromStr,
14};
15
16use clap::Args;
17use strum::IntoEnumIterator;
18
19use crate::{config, template::Template};
20
21#[derive(Args, Debug)]
23pub struct InitOptions {
24 #[arg(long)]
25 pub title: Option<String>,
26
27 #[arg(long, num_args = 1.., value_delimiter=' ')]
28 pub tags: Option<Vec<String>>,
29
30 #[arg(long)]
31 pub color: Option<String>,
32
33 #[arg(long)]
34 pub template: Option<String>,
35}
36
37const MD_VITE_CONFIG_JS: &str = include_str!("../static/md-templates/vite.config.js");
39const MD_INDEX_HTML: &str = include_str!("../static/md-templates/index.html");
40const MD_MAIN_JS: &str = include_str!("../static/md-templates/main.js");
41const MD_SLIDES: &str = include_str!("../static/md-templates/slides.md");
42const MD_CUSTOM_CSS: &str = include_str!("../static/md-templates/custom.css");
43const MD_FERRIS: &[u8] = include_bytes!("../static/md-templates/ferris.png");
44
45const MD_GITIGNORE: &str = include_str!("../static/md-templates/.gitignore");
46
47const HTML_VITE_CONFIG_JS: &str = include_str!("../static/html-templates/vite.config.js");
49const HTML_INDEX_HTML: &str = include_str!("../static/html-templates/index.html");
50const HTML_MAIN_JS: &str = include_str!("../static/html-templates/main.js");
51const HTML_SLIDES: &str = include_str!("../static/html-templates/slides.html");
52const HTML_CUSTOM_CSS: &str = include_str!("../static/html-templates/custom.css");
53const HTML_FERRIS: &[u8] = include_bytes!("../static/html-templates/ferris.png");
54const HTML_GITIGNORE: &str = include_str!("../static/html-templates/.gitignore");
55
56pub fn init(opts: InitOptions) -> Result<(), Box<dyn Error>> {
70 let template = match opts.template {
71 Some(ref arg_template) => {
72 Template::from_str(arg_template).map_err(|_| "Invalid template".to_string())?
73 }
74 None => prompt_template()?,
75 };
76
77 let conf = config::create_conf(opts)?;
78
79 std::fs::create_dir_all(&conf.title)?;
80
81 let output = Command::new("npm")
82 .args(["init", "-y", "--prefix", &conf.title])
83 .current_dir(&conf.title)
84 .output()?;
85
86 if !output.status.success() {
88 eprintln!(
89 "npm init failed: {}",
90 String::from_utf8_lossy(&output.stderr)
91 );
92 return Err("npm init failed".into());
93 }
94
95 let npm_commands = vec![
96 format!("install --save-dev vite@5.4.21 http-server@14.1.1"),
97 format!("install reveal.js@5.2.1 serve@14.2.6 vite-plugin-singlefile@2.3.0"),
98 format!("install patch-package@8.0.1"),
99 ];
100
101 for c in npm_commands {
102 let args: Vec<&str> = c.split(' ').collect();
103 let output = Command::new("npm")
104 .args(&args)
105 .current_dir(&conf.title)
106 .output()?;
107
108 if !output.status.success() {
109 eprintln!(
110 "npm {} failed: {}",
111 c,
112 String::from_utf8_lossy(&output.stderr)
113 );
114 return Err(format!("npm {} failed", c).into());
115 }
116 println!("Bootstrapped npm {}", c);
117 }
118
119 println!("Saving config file...");
120
121 config::write_config(&conf.title, &conf)?;
122
123 match template {
125 Template::Markdown => {
126 fs::write(format!("{}/vite.config.js", &conf.title), MD_VITE_CONFIG_JS)?;
128 fs::write(format!("{}/index.html", &conf.title), MD_INDEX_HTML)?;
129 fs::write(format!("{}/.gitignore", &conf.title), MD_GITIGNORE)?;
130
131 std::fs::create_dir_all(format!("{}/src", &conf.title))?;
132 fs::write(format!("{}/src/main.js", &conf.title), MD_MAIN_JS)?;
133
134 std::fs::create_dir_all(format!("{}/slides", &conf.title))?;
135 fs::write(format!("{}/slides/slides.md", &conf.title), MD_SLIDES)?;
136
137 std::fs::create_dir_all(format!("{}/css", &conf.title))?;
138 fs::write(format!("{}/css/custom.css", &conf.title), MD_CUSTOM_CSS)?;
139
140 std::fs::create_dir_all(format!("{}/public", &conf.title))?;
141 fs::write(format!("{}/public/ferris.png", &conf.title), MD_FERRIS)?;
142 }
143 Template::HTML => {
144 fs::write(
146 format!("{}/vite.config.js", &conf.title),
147 HTML_VITE_CONFIG_JS,
148 )?;
149 fs::write(format!("{}/index.html", &conf.title), HTML_INDEX_HTML)?;
150 fs::write(format!("{}/.gitignore", &conf.title), HTML_GITIGNORE)?;
151
152 std::fs::create_dir_all(format!("{}/src", &conf.title))?;
153 fs::write(format!("{}/src/main.js", &conf.title), HTML_MAIN_JS)?;
154
155 std::fs::create_dir_all(format!("{}/slides", &conf.title))?;
156 fs::write(format!("{}/slides/slides.html", &conf.title), HTML_SLIDES)?;
157
158 std::fs::create_dir_all(format!("{}/css", &conf.title))?;
159 fs::write(format!("{}/css/custom.css", &conf.title), HTML_CUSTOM_CSS)?;
160
161 std::fs::create_dir_all(format!("{}/public", &conf.title))?;
162 fs::write(format!("{}/public/ferris.png", &conf.title), HTML_FERRIS)?
163 }
164 }
165
166 Ok(())
167}
168
169fn prompt_template() -> Result<Template, Box<dyn Error>> {
170 let template_opts: Vec<Template> = Template::iter().collect();
171
172 let chosen_template = inquire::Select::new("Select a template:", template_opts).prompt()?;
173
174 Ok(chosen_template)
175}