1use std::{
10 error::Error,
11 fs::{self},
12 process::Command,
13};
14
15use clap::Args;
16
17use crate::config;
18
19#[derive(Args, Debug)]
20pub struct InitOptions {
21 #[arg(long, required = false)]
22 presentation_only: bool,
23}
24
25const PACKAGE_JSON: &str = include_str!("../static/package.json");
26const VITE_CONFIG_JS: &str = include_str!("../static/vite.config.js");
27const INDEX_HTML: &str = include_str!("../static/index.html");
28const MAIN_JS: &str = include_str!("../static/main.js");
29const SLIDES_MD: &str = include_str!("../static/slides.md");
30const CUSTOM_CSS: &str = include_str!("../static/custom.css");
31
32pub fn init(_opts: InitOptions) -> Result<(), Box<dyn Error>> {
33 let conf = config::create_conf()?;
36
37 std::fs::create_dir_all(&conf.title)?;
39 let output = Command::new("npm")
42 .args(["init", "-y", "--prefix", &conf.title])
43 .current_dir(&conf.title)
44 .output()?;
45
46 if !output.status.success() {
48 eprintln!(
49 "npm init failed: {}",
50 String::from_utf8_lossy(&output.stderr)
51 );
52 return Err("npm init failed".into());
53 }
54
55 let npm_commands = vec![
56 format!("install --save-dev vite http-server"),
57 format!("install reveal.js serve vite-plugin-singlefile"),
58 ];
59
60 for c in npm_commands {
61 let args: Vec<&str> = c.split(' ').collect();
62 let output = Command::new("npm")
63 .args(&args)
64 .current_dir(&conf.title)
65 .output()?;
66
67 if !output.status.success() {
68 eprintln!(
69 "npm {} failed: {}",
70 c,
71 String::from_utf8_lossy(&output.stderr)
72 );
73 return Err(format!("npm {} failed", c).into());
74 }
75 println!("Bootstrapped npm {}", c);
76
77 println!("Saving config file...");
78
79 config::write_config(&conf.title, &conf)?;
80 }
81
82 fs::write(format!("{}/package.json", &conf.title), PACKAGE_JSON)?;
83 fs::write(format!("{}/vite.config.js", &conf.title), VITE_CONFIG_JS)?;
84 fs::write(format!("{}/index.html", &conf.title), INDEX_HTML)?;
85
86 std::fs::create_dir_all(format!("{}/src", &conf.title))?;
87 fs::write(format!("{}/src/main.js", &conf.title), MAIN_JS)?;
88
89 std::fs::create_dir_all(format!("{}/slides", &conf.title))?;
90 fs::write(format!("{}/slides/slides.md", &conf.title), SLIDES_MD)?;
91
92 std::fs::create_dir_all(format!("{}/css", &conf.title))?;
93 fs::write(format!("{}/css/custom.css", &conf.title), CUSTOM_CSS)?;
94
95 Ok(())
96}