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 http-server"),
97 format!("install reveal.js serve vite-plugin-singlefile"),
98 format!("install vite@5"),
99 format!("install patch-package"),
100 ];
101
102 for c in npm_commands {
103 let args: Vec<&str> = c.split(' ').collect();
104 let output = Command::new("npm")
105 .args(&args)
106 .current_dir(&conf.title)
107 .output()?;
108
109 if !output.status.success() {
110 eprintln!(
111 "npm {} failed: {}",
112 c,
113 String::from_utf8_lossy(&output.stderr)
114 );
115 return Err(format!("npm {} failed", c).into());
116 }
117 println!("Bootstrapped npm {}", c);
118 }
119
120 println!("Saving config file...");
121
122 config::write_config(&conf.title, &conf)?;
123
124 match template {
126 Template::Markdown => {
127 fs::write(format!("{}/vite.config.js", &conf.title), MD_VITE_CONFIG_JS)?;
129 fs::write(format!("{}/index.html", &conf.title), MD_INDEX_HTML)?;
130 fs::write(format!("{}/.gitignore", &conf.title), MD_GITIGNORE)?;
131
132 std::fs::create_dir_all(format!("{}/src", &conf.title))?;
133 fs::write(format!("{}/src/main.js", &conf.title), MD_MAIN_JS)?;
134
135 std::fs::create_dir_all(format!("{}/slides", &conf.title))?;
136 fs::write(format!("{}/slides/slides.md", &conf.title), MD_SLIDES)?;
137
138 std::fs::create_dir_all(format!("{}/css", &conf.title))?;
139 fs::write(format!("{}/css/custom.css", &conf.title), MD_CUSTOM_CSS)?;
140
141 std::fs::create_dir_all(format!("{}/public", &conf.title))?;
142 fs::write(format!("{}/public/ferris.png", &conf.title), MD_FERRIS)?;
143 }
144 Template::HTML => {
145 fs::write(
147 format!("{}/vite.config.js", &conf.title),
148 HTML_VITE_CONFIG_JS,
149 )?;
150 fs::write(format!("{}/index.html", &conf.title), HTML_INDEX_HTML)?;
151 fs::write(format!("{}/.gitignore", &conf.title), HTML_GITIGNORE)?;
152
153 std::fs::create_dir_all(format!("{}/src", &conf.title))?;
154 fs::write(format!("{}/src/main.js", &conf.title), HTML_MAIN_JS)?;
155
156 std::fs::create_dir_all(format!("{}/slides", &conf.title))?;
157 fs::write(format!("{}/slides/slides.html", &conf.title), HTML_SLIDES)?;
158
159 std::fs::create_dir_all(format!("{}/css", &conf.title))?;
160 fs::write(format!("{}/css/custom.css", &conf.title), HTML_CUSTOM_CSS)?;
161
162 std::fs::create_dir_all(format!("{}/public", &conf.title))?;
163 fs::write(format!("{}/public/ferris.png", &conf.title), HTML_FERRIS)?
164 }
165 }
166
167 Ok(())
168}
169
170fn prompt_template() -> Result<Template, Box<dyn Error>> {
171 let template_opts: Vec<Template> = Template::iter().collect();
172
173 let chosen_template = inquire::Select::new("Select a template:", template_opts).prompt()?;
174
175 Ok(chosen_template)
176}